Question: Java I have attached person and money class public class Person{ public String name; public Money money; public Person(String name, Money money){ this.name = name;

Java I have attached person and money class
 Java I have attached person and money class public class Person{
public class Person{
public String name;
public Money money;
public Person(String name, Money money){
this.name = name;
this.money = money;
}
public Person(String name){
this(name, new Money());
}
}
Next is money class
/**
* A Money object models money as dollars and cents
**/
public class Money{
/* instance attributes */
private int dollars = -1;
private byte cents = -1; // Once created we must ensure that 0
public Money(){
// purpose: create an object with zero dollars and cents.
}
public Money(int c){
// purpose: create an object with c cents
// precondition: c >= 0
// side effects: object is created with total value of c cents
// (adjusting dollars and cents so that 0
}
public Money(int d, int c){
// purpose: create an object with d dollars and c cents
// preconditions: d >= 0 and c >= 0
// side effects: object is created with total value d dollars + c cents
// (adjusting this.dollars and this.cents so that 0
}
/** getter for dollars
*
* @return the current number of dollars this has
*/
public int getDollars(){ return this.dollars; }
/** getter for cents
*
* @return the current number of cents this has
*/
public int getCents(){ return this.cents; }
// Task #2 is to finish these three instance methods
// public void add(int c){...}
// adds c cents to the current value
// public void add(int d, int c){...}
// adds d dollars and c cents to the current value
// public int remove(int c){...}
// removes c cents from current value if current
// value is large enough. Otherwise, removes as much as it can.
// Returns the actual amount of cents removed (may be > 100)
/**
* Returns a String representation of the value of the current object.
*
* @return The value of the current object is returned as the String"$D.cc"
* where D is the number of dollars and cc is the cents of the value. Uses the format()
* method from the String class to ensure that the cents are displayed properly (2 spaces
* with leading zeros if needed).
**/
@Override
public String toString(){
return "$" + String.format("%01d", dollars) + "." + String.format("%02d", cents);
}
}
Interacting Objects Complete the provided Person class. A person object has a name and some money. Write the body of the compare method. This method allows us to compare two person objects based on how much money they have. When you call person1.compare(person2) the method will return a negative number of person1 has less money than person2 (it does not matter what negative int you return), return zero if person1 and person2 have the same amount of money, or return a ositive number if person1 has more money then person2. Complete the shareMoney method that shares the money of a given person object with another. For the shareMoney method, be sure that each person has their own money. That is, even though they have the same money value they should each have their own money object they they are referencing

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!