Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiler error and Money class assignment  RSS feed

 
Kyle Wettengel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Here is my code:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Wettengel wrote: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"

This seems pretty straightforward. It is telling you what the exact problem is.

Here is where you declare your checkMoney method:

You have told it that when you call this method, you will pass it two ints.

Then, when you actually DO call it, you have this:


How many arguments have you passed here? None.

The compiler is telling you that something doesn't match up.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Kyle Wettengel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a little more digging and I changed a few things around including getting rid of the checkMoney methods, replacing the while statements, and changing around my toString, equals, and plus methods...Everything is compiling, let me know what you think.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks pretty good. A few points.

1. Your no-arg constructor does not match the specification

2. It's not wrong, but I would simplify your toString method to


3. If you're feeling brave, I would also tell your teacher that the specification is sloppy
2. A default constructor that initializes the dollars to 0 and cents to 1. It should call the two-parameter constructor.

A default constructor is an implicit constructor created by the compiler. If you write a constructor that takes no arguments then it is simply a no-arg constructor.
 
Kyle Wettengel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so rewriting the no arg constructor I would get




correct?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You simply pass the two arguments to the other constructor with the this() call.
As well as Joanne telling you it is sloppy to call that a default constructor, I shall tell you it is iffy design to have a no‑arguments constructor at all in those circumstances. Why should money default to $0.01.

I shall disagree with you both about the String to return:-
return String.format("The amount is $%d.%02d", dollars + cents / 100, cents % 100);
You can find out about the % tags for formatting here.

If the question says to write a no‑args constructor, you will have to write one, iffy or not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!