• 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 Two Objects of the Same Class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my java class my instructor is wanting us to create a program that calculates the amount of interest accrued on an account per year and displays the amount that is in that account every month.

So a sample runtime of this is as follows:

In the main method I create two classes, one for the balance account and the other for the monthly deposit.

So I have two objects of the same class.

Then I'm asked to enter the amount for the first object which I'll call the "balance."
Then I enter the dollar amount separated by a space and the cent amount for the balance.
The balance object then takes these two values and stores them to two separate variables, one for dollars and one for cents.

Then I enter the deposit amount.
and it does the same as above.

Then i enter the percentage of the interest rate.

Then I enter how many years I want it to run for.

Then it goes into a for-loop structure.

In the body of the loop, it first calculates the amount of interest that will be added to the balance.

then it adds the new balance to the deposit object

and then it outputs the total amount of the balance, and then loops through it again.

My issue is adding the balance object to the deposit object.
I have a method in this account class called "add" and so far I know that the structure should look something like this:

public void add(MonetaryValue addend)
{
(But I have no idea what goes here or how to get this to work.)
}

Can anyone help me? Thanks!
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, read this WhatNotHow.

Next, I'm curious, why not just use doubles?

That way, you don't have to deal with multiple numbers. All you'd do is manipulate one value rather than two.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch Chris. In addition to what Aj said, whenever referring to code , use code tags like :



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Maske wrote:I have a method in this account class called "add" and so far I know that the structure should look something like this:
public void add(MonetaryValue addend) {
(But I have no idea what goes here or how to get this to work.)...


Well, first: I agree completely with AJ Prieto on his first point (but I would, 'cause I wrote the page he pointed you to ).

Second: avoid writing methods that return void. There are obvious exceptions to this rule (eg, setters) but, in general, methods work best when they return something.

Third: I can certainly see a need for an Account class (not so sure about Deposit), so why not have a method called monthlyInterest() that simply returns the amount of interest for a month based on its current balance? (in the real world, you'd probably actually want it on the minimum balance for the month; but that's beyond the scope of your instructions). And just to get you started, it might look something like this:
And finally, just to confuse you completely: I probably wouldn't use doubles (or floats) - the above was just for illustration - because they are not good for storing decimal values.

The reason is that doubles:
(a) Hold a lot more than just 2 decimal places.
(b) Often don't hold decimal amounts exactly (for more information, read this page).
This may be why your instructor told you to enter the dollars and cents separately.

You basically have two alternatives:
1. Use BigDecimal (←click) instead. This is actually probably the easiest, but it does involve learning the API for a new class (and it's quite big).
2. Keep all your internal amounts as integers (I'd suggest longs) in cents; and only convert them for display (and there you could use a double). Obviously, for most calculations, this means that you have to remember to round any result to the nearest cent.

HIH

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, first: I agree completely with AJ Prieto on his first point (but I would, 'cause I wrote the page he pointed you to ).



Spreading the word of GOD.
 
Chris Maske
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aj Prieto wrote:First off, read this WhatNotHow.

Next, I'm curious, why not just use doubles?

That way, you don't have to deal with multiple numbers. All you'd do is manipulate one value rather than two.




Well I actually did try to use doubles, but then when I got to class our instructor told us we couldn't

Pseudo code solution (main method of application class):

Prompt user for input values
Read input values

Create MonetaryValue object for the balance of the account
Set the value of the object with the start dollar and cents values ..........................setValue()

Create MonetaryValue object for the amount to deposit each month
Set the value of the object with the dollar and cents values for monthly deposit .....setValue()

Repeat for correct number of years
Repeat for the correct number of months

Add interest to the balance ..............................................................................addInterest()

Add the monthly deposit amount to the deposit ..................................................add()

Display the new balance



So above is the solution that he wants us to use in order to solve the problem. And I also should have mentioned that I do have an addinterest method in my class. I've pretty well got the whole code finished except for that one add method... and he wants us to do it like this:


"add the addend dollars and the addend cents to the dollars and cents of the object"

All thoughts, ideas, comments, are much appreciated up to this point and after.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Maske wrote:So above is the solution that he wants us to use in order to solve the problem. And I also should have mentioned that I do have an addinterest method in my class. I've pretty well got the whole code finished except for that one add method... and he wants us to do it like this:


"add the addend dollars and the addend cents to the dollars and cents of the object"


OK, so what have you tried?

It would be really useful to see what you've put together for this MonetaryValue class, even if it was already given to you, because that's likely to have a big bearing on how you code the method. I'm also not quite certain whether this method is supposed to be in Account or MonetaryValue, or both.

Winston
 
Chris Maske
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so I used "account" and "MonetaryValue" synonymously.

This MonetaryValue class is what stores the dollar and cent amounts, it's what calculates the added interest, it's what adds the monthly deposit to the main account, and it's what outputs the final dollar value for the account each month.

So here's what I have coded in MonetaryValue so far:



My whole problem is with trying to figure out how to write the code for the method on line 44.

So in the main method class I have it coded to create two MonetaryValue objects, one named balance and one named deposit:



And then later in the main method I have it set up to where it takes the deposit object and adds it to the balance object:



So basically... I just need to figure out how to write the add method in my MonetaryValue class. I'm really sorry for all the confusion on this.
 
Chris Maske
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just solved my own problem:

 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just solved my own problem



When you start thinking clear and define the problem precisely and solve it yourself, it is actually the best sense of accomplishment. Well done.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Maske wrote:So here's what I have coded in MonetaryValue so far:


Seems reasonable, but how about:Do you see how that might make life a lot easier? It might need a check or two to make bulletproof, but hopefully you get the idea.

You might also consider a constructor that takes dollars and cents directly.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic