Question: Exercise 1 In Java create a file named A8.java. Place all your code in this file. Write an interface called IPerson. Add a method signature

Exercise 1 In Java create a file named A8.java. Place all your code in this file. Write an interface called IPerson. Add a method signature for payMoney. It should return an int and accept an int as a parameter.

Exercise 2 Write a class called Person that implements IPerson. You should define a private field named money of type int. You must implement the payMoney method. Given input integer toPay, your method should decrease money by toPay and return the new value of money. Add a default constructor that sets money to 10000 (10000 = $100.00). Override toString and display your money (e.g. if money = 12345, display $123.45). In this assignment, you should always use the override annotation when overriding a method. Note: Make sure the money is always displayed with 2 decimal places (so 10000 should show as $100.00, not $100.0 and not $100). You can use the String.format method as follows: String.format(%2f, amount) Override equals to compare the values in money. Write a getter for money. Exercise 3 Write a class called Employee that extends Person. Add a private integer field called employeeID. Write a new value constructor that accepts as input an integer called employeeID. Your constructor should call Persons constructor so that money is still initialized (how can you do this?). Set the field employeeID to the parameter employeeID. These should have the same name. Override the payMoney method. Make sure you use the override annotation. Employees only need to pay half. Before subtracting the amount to pay from money, divide toPay by 2, rounding up. Hint: How can you access the parents payMoney? Hint: The Math class has a method that can help with rounding up. Override toString and display your employeeID and money (e.g. if employeedID = 12 and money = 100, print out Employee 12 has $1.00. Override equals and compare both money and employeeID. Implement a static method addMoney(IPerson person, int amount). This method should add amount to the money of the given person. Hint: Calling payMoney with a negative value will pay the person. However, if person is an employee, only half will get added. You can check if a person is an Employee with: person instanceof Employee. In this case, you should double the value to make sure the correct amount is paid. Write a getter for employeeID.

Exercise 4 Implement the following code in main to test your code. IPerson person = new Employee(12345); Employee.addMoney(person, 200); System.out.println(((Employee)person).getEmployeeID()); System.out.println(((Person)person).getMoney()); person.payMoney(300); System.out.println(((Person)person).getMoney());

Code so far:

public class A8{ public static void main(String[] args) { //Exercise 4 main code... if code below is correct, main should execute w/o issue IPerson person = new Employee(12345); Employee.addMoney(person, 200); System.out.println(((Employee)person).getEmployeeID()); System.out.println(((Person)person).getMoney()); person.payMoney(300); System.out.println(((Person)person).getMoney());

} }

interface IPerson { //interface called IPerson public int payMoney(int toPay); //method called payMoney that returns an int // and accepts an int }

class Person implements IPerson{ //class called Person that implements IPerson

private int money; // private field named int public int getMoney(){ //getter for money return this.money; } public int payMoney(int toPay){ //Given input integer toPay, your method should decrease money by toPay and return the new money = this.money - toPay; //value of money. return money; } public Person(){ //Add a default constructor that sets money to 10000 this.money = 10000; } @Override public String toString(){ //toString where if for example money = 12345, it will display as $123.45 return String.format("%2f", (double) money); // **Question 1: having issues with this.** } @Override public boolean equals(Object obj) { //equals boolean to compare values of money if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (money != other.money) return false; return true; } }

class Employee extends Person { //class extending Person private int employeeID; //create private int public Employee(){ super(); } public Employee(int employeeID){ //call Persons' constructor so money is still initialized super(); // **Question 2 seems like i'm having issues here...compiling code always still says 'money is // private'** this.employeeID = employeeID; //Set the field employeeID to the parameter employeeID. These should have the same name. } @Override public int payMoney(int toPay){ //Override the payMoneymethod. Make sure you use the override super(toPay); //annotation.Employees only need to pay half. Before subtracting the amount to pay from double temp = Math.round(toPay / 2); // money, divide toPay by 2, rounding up. this.toPay-= temp; } @Override public String toString() { return "Employee [employeeID=" +employeeID+ "money =" +money+ "]"; //override toString and display your employeeID and money } @Override //equals statement compare both money and employeeID. **Question 3: maybe having issues? public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (employeeID != other.employeeID) return false; if (this.getMoney() != other.getMoney()) return false; return true; } public static void addMoney(Person person, int amount){ //implement a static method addMoney(IPerson person, int amount). This person.getMoney += amount; // method should add amount to the money of the given person. } //**Question 4...issues here as well public int getEmployeeID() { //getter for employeeid return employeeID; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!