I keep getting an error in my MoneyDemo class, it reads, "Method checkMoney in class Money cannot be applied to given types; required: no arguments; found int, int; reason:actual and formal argument lists differ in length"
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:
- 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.
- A default constructor that initializes the dollars to 0 and cents to 1. It should call the two-parameter constructor.
- 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.
- Accessors for dollars and cents
- The standard toString method
- The standard equals method that compares if two Money objects have the same state.
- 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.
public class Money
{
private int dollars, cents;
private static final int CENTS_PER_DOLLAR = 100;
public void checkMoney(int dollars, int cents)
{
while (cents<0)
{
cents += 100;
dollars--;
}
while (cents>99)
{
cents -= 100;
dollars++;
}
}
public Money()
{
this.cents = 1;
this.dollars = 0;
}
public Money(int dollars,int cents)
{
this.cents = dollars;
this.dollars = cents;
}
public void initializeCents(int cents)
{
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;
dollars = dollars + cents / CENTS_PER_DOLLAR;
cents = cents % CENTS_PER_DOLLAR;
str = "Your total amount is: " + "$" + dollars + "." + cents +"";
return str;
}
public Money plus(Money y)
{
return new Money(y.dollars, y.cents);
}
}
public class MoneyDemo
{
public static void main(String[] args)
{
int dollars = 5;
int cents = 80;
Money x = new Money(dollars, cents);
x.checkMoney();
dollars = 7;
cents = 70;
Money y = new Money( dollars, cents);
y.checkMoney();
Money z = x.plus(y);
System.out.println( "x: $" + x.dollars + "." + x.cents + "c");
System.out.println( "y: $" + y.dollars + "." + y.cents + "c");
System.out.println( "x.plus(y): $" + z.dollars + "." + z.cents + "c");
}
}