I am having trouble with my Java assignment. I cannot figure out how to write the plus and equals methods. Any help would be appreciated.
Also here is my list of objectives for the program which I'll be graded on, If you notice any other errors in my code which I may run into later please feel free to point them out for me!
Use BlueJ to create a Money class with two integer instance variables, dollars and cents. Provide the following methods:
1. A two-parameter constructor that initializes the instance variables. The constructor should check that the cents value is between 0 and 99, and if not, transfer some of the cents to the dollars variable to make it between 0 and 99.
2. A default constructor that initializes the dollars to 0 and cents to 1. It should call the two-parameter constructor.
3. A one parameter constructor that initializes the cents value while setting the dollars value to 0. This constructor should also check that the cents value is between 0 and 99, and if not, transfer some of the cents to the dollars variable to make it between 0 and 99.
4. Accessors for dollars and cents
5. The standard toString method
6. The standard equals method that compares if two Money objects have the same state.
7. The plus method that takes a Money object as its parameter. It creates and returns a new Money object representing the sum of the object whose plus() method is being called and the parameter. It does NOT modify the values of the two existing objects.
Finally, create a MoneyDemo class that creates multiple Money objects. This demo class should test all constructors and methods that you have implemented.
Heres my code:
public class Money
{
int dollars, cents;
public Money(int dollars, int cents)
{
this.cents = dollars;
this.dollars = cents;
while (cents<0)
{
cents += 100;
dollars--;
}
while (cents>99)
{
cents -= 100;
dollars++;
}
}
public Money()
{
this(0,0);
this.cents = 1;
this.dollars = 0;
}
public Money(int cents)
{
this.cents = cents;
this.dollars = 0;
while (cents<0)
{
cents += 100;
dollars--;
}
while (cents>99)
{
cents -= 100;
dollars++;
}
}
public int getDollars()
{
return dollars;
}
public int getCents()
{
return cents;
}
public String toString()
{
String str;
str = "Your total amount is: " + "$" + dollars + "." + cents +"";
return str;
}
public boolean equals(Object obj)
{
if (money1.dollars == money2.dollars && money1.cents == money2.cents)
{
return true;
}
else
{
return false;
}
}
public Money plus(int money3)
{
return money3;
}
}
public class MoneyDemo
{
public static void main(String [] args)
{
Money money1 = new Money(13,57);
Money money2 = new Money(540,143);
System.out.print("Money 2 data: ");
System.out.print(money2.getDollars() + " dollars and ");
System.out.println(money2.getCents() + " cents");
Money money3;
money3 = money1.plus(money2);
System.out.print("Money1 data: ");
System.out.print(money1.getDollars() + " dollars and ");
System.out.println(money1.getCents() + " cents" );
System.out.print("Money2 data: ");
System.out.print(money2.getDollars() + " dollars and ");
System.out.println(money2.getCents() + " cents" );
System.out.print("Money 3 data (sum): ");
System.out.print(money3.getDollars() + " dollars and ");
System.out.println(money3.getCents() + " cents" );
}
}