File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Adding Two Objects of the Same Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding Two Objects of the Same Class" Watch "Adding Two Objects of the Same Class" New topic
Author

Adding Two Objects of the Same Class

Chris Maske
Greenhorn

Joined: Mar 29, 2013
Posts: 4
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!
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

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.

Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the ranch Chris. In addition to what Aj said, whenever referring to code , use code tags like :




~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7809
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Mar 29, 2013
Posts: 4
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

Joined: Mar 17, 2011
Posts: 7809
    
  21

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

Joined: Mar 29, 2013
Posts: 4
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

Joined: Mar 29, 2013
Posts: 4
I just solved my own problem:

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Mar 17, 2011
Posts: 7809
    
  21

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding Two Objects of the Same Class