• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding Objects?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic