• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compound Interest on an object

 
King Do
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very new to Java programming and I'm having some trouble with an assignment. The assignment requires the creation of two objects, each representing a savings account (saver1 and saver2), a private instance variable (savingsBalance), and a static method that sets the interest rate (modifyInterestRate).

I've compiled two classes (one containing the constructors for the objects and one as a tester ) that can do everything required, but only based on a set beginning balance. I need some advice. I'm trying to figure out how to take the output balance (saver1.toOutput and saver2.toOutput) from the first calculation and plug it in as an argument to modify the object.

This is the source code that I have so far:

Constructor class:

//import class to format decimals
import java.text.DecimalFormat;

//assign name to class that inherits attributes and behaviors from class Object
public class SavingsAccount extends Object
{
//define static variable to be used in class
private static double
annualInterestRate;
public double
balanceOutput;

//constructors
public SavingsAccount( double balance, double rate)
{
this.balanceOutput = balance + ( calculateMonthlyInterest( balance, rate ));
}

//start method to calculate the monthly interest
public double calculateMonthlyInterest( double balance, double rate)
{
return balance * modifyInterestRate( rate ) / 12;

//end method calculateMonthlyInterest
}

//start method to change annualInterestRate
public static double modifyInterestRate ( double rate )
{
annualInterestRate = rate;
return annualInterestRate;

//end method modifyInterestRate
}

//convert object to output
public String toOutput()
{
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
return twoDigits.format( balanceOutput );
}

//end class SavingsAccount
}

Tester class:

import javax.swing.*;

//assign name to class
public class SavingsTest
{
//start method main
public static void main( String args[] )
{
//create objects
SavingsAccount saver1 = new SavingsAccount( 2000.00, .04 );
SavingsAccount saver2 = new SavingsAccount( 3000.00, .04 );

//display results
JOptionPane.showMessageDialog( null, saver1.toOutput(),
"Saver1", JOptionPane.INFORMATION_MESSAGE );

JOptionPane.showMessageDialog( null, saver2.toOutput(),
"saver2", JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );

//end method main
}

//end class SavingsTest
}

I think that if I can make method toOutput in the constructor class return a double to the tester class, I can use it as an argument to modify the objects (in place of the set balances of 2000 and 3000 ) In other words, I would add:

//modify objects
saver1 = SavingsAccount( saver1.toOutput(), .04 );
saver2 = SavingsAccount( saver2.toOutput(), .04 );

//display results
JOptionPane.showMessageDialog( null, saver1.toOutput(),
"Saver1", JOptionPane.INFORMATION_MESSAGE );

JOptionPane.showMessageDialog( null, saver2.toOutput(),
"saver2", JOptionPane.INFORMATION_MESSAGE );

When I add this now, I get a "cannot resolve symbol" error on the two objects. So, I have to resolve this issue before I can even try to return the double so I can use it as an argument to modify the object.

Any advice would be appreciated...I'm stumped.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by King Do:
...I think that if I can make method toOutput in the constructor class return a double to the tester class, I can use it as an argument to modify the objects (in place of the set balances of 2000 and 3000 ) In other words, I would add:

//modify objects
saver1 = SavingsAccount( saver1.toOutput(), .04 );
saver2 = SavingsAccount( saver2.toOutput(), .04 );

... When I add this now, I get a "cannot resolve symbol" error...

The error is because the toOutput() method returns a String, and the constructor is expecting a double. You could either overload the constructor to accept a String argument, or you could pass a double (instead of a String) to the existing constructor.

Please use Code Tags to keep your code formatting intact. This will make it easier for others to read and reply.
[ March 06, 2006: Message edited by: marc weber ]
 
Casey Buresh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey man, I'm talking to the dude who wrote the topic, yer program worked perfect on my computer, I'm a sophmore and that was the same program I had to write so I hope you don't mind I used yours, except that my class had to make it so that i also had a 5% interest rate, but thanks because I only had to change a bit, so you helped me alot!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where can I find someone to do MY homework for me?

If anyone has a good essay on Geopolitical Stife in Newfoundland, please let me know.

-Cameron
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic