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:

  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.

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");
    }
}

Recommended Answers

All 6 Replies

I'm impressed by how gentle BlueJ is with its error messages. It tells you the problem twice in two distinct ways. I'm not sure I can add much to that, but you should probably look at lines 6, 79, and 84. Look at them carefully and think about the error message until you see it.

I notice that you have a CENTS_PER_DOLLAR constant which is admirable. It's always better to have a constant field than to use unexplained numbers, but I see in some places you also use the number 100 instead of CENTS_PER_DOLLAR. Of course there is probably no chance that you'd ever want to change the value of CENTS_PER_DOLLAR, but if it ever happened you'd wish you'd used CENTS_PER_DOLLAR everywhere. It would be good practice.

Normally the constructor is responsible for making sure that the object's value is stored correctly. I see you calling checkMoney every time you make a new Money. I think the constructor should probably be calling checkMoney itself so there is no chance of forgetting it and then checkMoney could be private.

You have no one-parameter constructor, but your instructions ask for one. Also, your instructions say that your no-parameter constructor should call the two-parameter constructor, but you don't do that either.

I see your plus method doesn't do what it is supposed to do at all.

Your toString method is pretty chaotic. These two lines don't do anything, but they look really bad at first glance, and upon reflection they just make you look like you don't know what you are doing.

dollars = dollars + cents / CENTS_PER_DOLLAR;
cents = cents % CENTS_PER_DOLLAR;

You should never change the values of the fields of your object in a toString method. Fortunately you're not changing the values of those fields, but it looks like you are trying to change them and making a mess of it, which is probably even worse from the perspective of someone evaluating your project.

If you add the final keyword to the declaration of dollars and cents then you will be protected by the compiler from that sort of mistake. Unfortunately you would also have to change the way checkMoney works since it would become impossible to change the values of dollars and cents once set. You could put the entire body of checkMoney into the two-parameter constructor before you set the fields.

commented: You were faster ;) +13

The method signature of your checkMoney method looks like this:

public void checkMoney(int dollars, int cents)

On lines 79 (x.checkMoney();) and 84 (y.checkMoney();) you didn't pass in arguments.
Hence you'll get a compilation error actual and formal argument lists differ in length.
The actual argument list in this context is an empty one, since you didn't pass in anything.
The formal argument list is specified in the method signature. In this context it has length 2.
However I should note that merely having matching lengths is not sufficient, the types in the argument lists should be of a correct type too.

There's another error on lines 88-90 you refer to dollars and cents, you declared them as private on line 3:

private int dollars, cents;

Lines 88-90 should therefore use the getters getDollars() and getCents().

After asking around a bit I decided to try and clean up my code. I believe I solved my problems with my constructor and toString methods, however I am still having trouble understanding the equals and plus method. Any help would be appriciated.

Heres my new 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" );  
    }  
} 

Your no-parameter constructor calls your two-parameter constructor, but it does so in a way that makes a mockery of your instructions. You may be technically following your instructions, but I'm sure when they told you to call your two-parameter constructor they actually meant for you use your two-parameter constructor, not just call it and then immediately reset dollars and cents to something else.

Your plus method is supposed to take a Money as its parameter, not an int. You can add two Moneys together by adding their dollars together and adding their cents together.

Your equals method needs to first check that the obj is a Money using obj instanceof Money. Once you know that, you can do something like

Money money2 = (Money)obj;

You should never do anything of the form if(x) return true; else return false; because that makes it look like you don't know what you are doing. Just do return x instead.

I understand that and I think thats where im getting confused, the instructions ask me to set the value of dollars to 0 and cents to 1 and then it says to call the 2-parameter constructor? How would I do this? Would I set the no-parameter to this(0,1);

Hi awfootball7,

I tweaked your code and here it is, let me know your questions.

class Money
{
    int dollars, cents;
    Money(int dollars, int cents)
    {
        this.cents = cents%100;
        this.dollars = dollars + (int)cents/100;
    }

    Money(int cents)
    {
        this(0,cents);
    }

    Money()
    {
        this(0,0);
    }

    int getDollars()
    {
        return dollars;
    }
    int getCents()
    {
        return cents;
    }
    public String toString()
    {
        return "Your total amount is: " + "$" + dollars + "." + cents +"";
    }
    boolean equals(Money money) 
    {
        return this.dollars == money.dollars && this.cents == money.cents;
    }
    Money plus(Money money) 
    {
        return new Money(
                (this.dollars+money.dollars + (int)(money.cents+this.cents)/100),
                ((money.cents + this.cents)%100)
            );
    }
}

class MoneyDemo
{  
    public static void main(String [] args) 
    {  
        Money money1 = new Money(57);  
        Money money2 = new Money(540,143); 

        System.out.println("Money 1" );  
        System.out.println(money1); 
        System.out.println("Money 2" );  
        System.out.println(money2);

        System.out.println("Money 3: Sum of Money 1 and 2" );  
        Money money3 = money1.plus(money2); 
        System.out.println(money3); 
        System.out.println("Money 1" );  
        System.out.println(money1); 
        System.out.println("Money 2" );  
        System.out.println(money2);

    }  
} 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.