This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Objects?  RSS feed

 
Tanner Cole
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have all but one line from the main class completed, I am just running into a dead end when working on the "plus" method.

"Create a USMoney class with two integer instance variables named dollars and cents. The class should have two accessor methods called getDollars and getCents. The class should have a constructor with two parameters for initializing a USMoney object. 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. (Note: You need not worry, though, for the purposes of this assignment, about reacting to and handling negative inputs to either of the two variables.)
Include in your class a method called plus that takes a USMoney object as an input parameter. This function should create and return a new USMoney object representing the sum of the calling object and the parameter. It does not modify the values of the two existing objects. Like the constructor, this method should ensure that the value of the cents instance variable of the returned object is between 0 and 99.
Provide Javadoc-style documentation (comments) for the 3 methods and the constructor of your USMoney class.
When you are finished, you should be able to run the following main class:



with expected output as follows:

Account 2 data: 541 dollars and 43 cents
Account 1 data: 13 dollars and 57 cents
Account 3 data (sum): 555 dollars and 0 cents"

Any help would surely be appreciated

Thanks,
Cole


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

What specific problem are you having? Please TellTheDetails(←click). What have you tried and what specific problems are you encountering?

Also, when posting code, please UseCodeTags(←click) so it will be readable. I have done so in your original post, and you can see how much easier it is to read.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do your coding bit by bit. Do one tiny step, then compile and run the code, then go on to the next step. Get yourself your Money class, and give it a toString() method which returns something like "$123.45". Then you can test it like this:-And add more methods and try them out similarly.
Note the behaviour your assignment describes is that of an immutable class. That never allows the dollar and cent fields to be changed after they have been initialised. So adding creates a new Money object, rather like here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!