Sunteți pe pagina 1din 11

Financial application:

package q1.pkgfinal;
import java.util.*;
public class Q1Final {
public static void main(String[] args) {

double amount;
double interest;
int months;
double Total =0;

Scanner input = new Scanner(System.in);


System.out.println("Please enter the amount");
amount = input.nextDouble();
System.out.println("Please enter the interest");
interest = input.nextDouble();
System.out.println("Please enter the number of months");
months = input.nextInt();

double intPerMonth = (interest/12);

for (int i=1; i <= months; i++)


{
Total = ((Total + amount) * (1+intPerMonth));
}
System.out.printf("Your total saving after %d months is: %.2f\n", months,
Total);

Inheritance
Main page
public class InheritanceExercise {
public static void main(String[] args) {
employee a = new employee("phat","2206",1000);
graduateStudent b = new graduateStudent("phat","2206",4.00);
undergraduateStudent c = new undergraduateStudent("phat","2208",3.99);

people[] People = new people[3];


People[0] = a;
People[1] = b;
People[2] = c;
for (people currentPeople: People)
{
System.out.println(currentPeople);
}
System.out.println("Change the graduate student thesis status");

for (people currentPeople: People)


{
if (currentPeople instanceof graduateStudent)
{
graduateStudent graStudent = (graduateStudent)currentPeople;
graStudent.setThesisGrade("Complete");
}
System.out.println(currentPeople);
}

People
package inheritanceexercise;
public abstract class people {
private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public String toString()
{
return String.format("Name: %s\n", getName());
}

public people(String n)
{
name = n;
}

Employee
public class employee extends people {
private String employeeID;
private double salary;
public employee(String n, String ID, double s)
{
super(n);
employeeID = ID;
salary = s;
}
@Override
public String toString()
{
return String.format("Employee:\n %s EmployeeID: %s \n Salary:
%.2f\n",super.toString(), getEmployeeID(), getSalary());
}
public String getEmployeeID() {
return employeeID;
}
public void setEmployeeID(String employeeID) {
this.employeeID = employeeID;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}

Student
public class student extends people {
private String studentID;
private double GPA;
public student(String n, String ID, double G)
{
super(n);
studentID = ID;
GPA = G;
}
@Override
public String toString()
{
return String.format("%s StudentID: %s\n GPA: %.2f\n",super.toString(),
getStudentID(), getGPA());
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public double getGPA() {
return GPA;
}
public void setGPA(double GPA) {
this.GPA = GPA;
}
}

Graduate Student
public class graduateStudent extends student {
private String Status = "Graduate";
private String thesisGrade = "Incomplete";
public graduateStudent(String n, String ID , double G)
{
super(n,ID,G);
}
@Override
public String toString()
{
return String.format("Graduate Student:\n %s Status: %s\n Thesis Grade:
%s\n",super.toString() , getStatus(), getThesisGrade());
}
public String getThesisGrade() {
return thesisGrade;
}
public void setThesisGrade(String thesisGrade) {
this.thesisGrade = thesisGrade;
}
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}

Undergraduate Student

public class undergraduateStudent extends student {


private String status = "undergraduate";
public undergraduateStudent(String n, String ID , double G)
{
super(n,ID,G);
}
@Override
public String toString()
{
return String.format("Undergraduate Student:\n %s Status:
%s\n",super.toString(), getStatus());
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}

Interface
package q4.pkgfinal;
public interface Payable {
public void Pay();
}
@Override
public void Pay()
{
System.out.println("Employeee needs to pay tax");
}

@Override
public void Pay()
{
System.out.println("Student needs to pay tuition");
}

Mod & loop


public static void main(String[] args) {
int today;
int future;
String[] Day =
{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
Scanner input = new Scanner(System.in);
System.out.println("Please enter today day");
today = input.nextInt();
System.out.println("please enter the number of days elapsed since today");
future = (today + input.nextInt())%7;

for (int i=0; i < Day.length; i++)


{
if(i==today)
{
for (int j=0; j < Day.length; j++)
{
if (j==future)
{
System.out.println("Today is " + Day[i] + " and the future day is " +
Day[j]);
}
}
}
}

Array & Sorting


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

double num1, num2, num3;

System.out.println("Please enter the first number");


num1 = input.nextDouble();
System.out.println("Please enter the second number");
num2 = input.nextDouble();
System.out.println("Please enter the third number");
num3 = input.nextDouble();

displaySortedNumbers(num1, num2, num3);

displayDescendingNumbers(num1, num2, num3);

public static void displaySortedNumbers(double num1, double num2, double


num3)
{
double[] num = new double[3];
num[0] = num1;
num[1] = num2;
num[2] = num3;

Arrays.sort(num);

System.out.println("The increasing order of the number is:");


for (int i = 0; i < num.length; i ++)
{
System.out.println(num[i]);
}
System.out.println();
}

public static void displayDescendingNumbers(double num1, double num2, double


num3)
{
double[] num = new double[3];
num[0] = num1;
num[1] = num2;
num[2] = num3;

Arrays.sort(num);
System.out.println("The decreasing order of the number is:");
for (int j = 2; j >= 0; j --)
{
System.out.println(num[j]);
}
}

S-ar putea să vă placă și