Java SavingsAccount program help

Saver1 and saver2 interest output

Page 1 of 1

5 Replies - 8764 Views - Last Post: 27 June 2009 - 10:17 PM Rate Topic: -----

#1 shane123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-June 09

Java SavingsAccount program help

Posted 27 June 2009 - 05:09 PM

To all,
here is the program overview:

Write a program that establishes two savings accounts with saver1 having account number 10002 with an initial balance of $2,000, and saver2 having account 10003 with an initial balance of $3,000. Set a common rate of interest at 5% per year. At the end of each month, update the balance by adding one month’s interest to the balance, so that the balance compounds monthly. Print an output that displays the month number and the account number and the balance for each of the two accounts. Use month 0 to display the initial balances, month 1 to display the balances after the first month’s interest, and so on. At the end of the year, display the total balance for both accounts combined, like this:



Output:

Monthly balances for one year with 0.05 annual interest:

Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2008.33 10003 3012.50
2 10002 2016.70 10003 3025.05
3 10002 2025.10 10003 3037.66
4 10002 2033.54 10003 3050.31
5 10002 2042.02 10003 3063.02
6 10002 2050.52 10003 3075.79
7 10002 2059.07 10003 3088.60
8 10002 2067.65 10003 3101.47
9 10002 2076.26 10003 3114.39
10 10002 2084.91 10003 3127.37
11 10002 2093.60 10003 3140.40
12 10002 2102.32 10003 3153.49

Final balance of both accounts combined: 5255.81

Use two classes, a SavingsAccount class and a YourNameProg4 as your Driver class.


And here is what I have so far.....any help is greatly appreciated

public class SavingsAcct {
	// define class variables using static keyword.
	private static double annualInterestRate;
 
	/**
	 * Set the value of the annual interest rate for all SavingsAccount instances.
	 *  
	 * @param annualInterestRate
	 */
	public static void setAnnualInterestRate(double annualInterestRate) {
		// set value of annualInterestRate variable
	}
	
	// define instance variables/constants
	private final String ACCOUNT_NUMBER; // constants defined using final keyword.
	private double balance;
 
	/**
	 * Two parameter constructor!
	 * 
	 * @param accountNumber
	 * @param beginningBalance
	 */
	public SavingsAcct(String account_Number, double beginningBalance) {
		// TODO: set values of instance variables here!
	}
 
	/**
	 * @return current balance.
	 */
	public double getBalance() {
		
	}
 
	/**
	 * Set current balance.
	 * 
	 * @param balance 
	 */
	public void setBalance(double balance) {
		
	}
 
	/**
	 * Get the account number of current instance of SavingsAccount.
	 */
	public String getACCOUNT_NUMBER() {
		
	}
	
	/**
	 * update the balance, by adding (balance * annualInterestRate / 12) to the current balance. 
	 */
	public void addMonthlyInterest() {
		
	}
}



public class Prog4 {
 
	public static void main(String[] args) {
		// Set the annual interest rate to 0.05
		SavingsAccount.setAnnualInterestRate(0.05);
 
		// Instantiate two SavingsAccount objects
		SavingsAccount saver1 = new SavingsAccount("10002", 2000);
		SavingsAccount saver2 = new SavingsAccount("10003", 3000);
		
		// Optionally, use NumberFormat to format output of balance.
 
		// Prints the table heading lines.
		
		System.out.printf("%2s %14s \t %8s %1s \t %8s \n", "Month",
				"Account #", "Balance", "Account #", "Balance");
		System.out.printf("%2s %14s \t %8s %1s \t %8s \n", "-----",
				"---------", "-------", "---------", "-------");
		
		// Print initial balances of both accounts for month 0.
		
		for (int month=1; month<=12; month++) {
			// Compound interest for current month for each saver object
			
			// Print monthly balances
		}
		
		// Display final total.
 
		// Exit program
		System.exit(0);
	}// End Main
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java SavingsAccount program help

#2 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Java SavingsAccount program help

Posted 27 June 2009 - 05:48 PM

Is it working how you want it ? If not tell us what is not working correctly
Was This Post Helpful? 0
  • +
  • -

#3 shane123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-June 09

Re: Java SavingsAccount program help

Posted 27 June 2009 - 05:54 PM

View Postbbq, on 27 Jun, 2009 - 04:48 PM, said:

Is it working how you want it ? If not tell us what is not working correctly



Well if you look at the comments I am having trouble with the rest of the code..I have been up all night just trying to get this far...I am willing to learn but i cant find a definitive example in reference to my code.

This post has been edited by shane123: 27 June 2009 - 05:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Java SavingsAccount program help

Posted 27 June 2009 - 06:47 PM

Ok well With your class SavingsAccount you need to declare some variables to store data in. For instance you have a function void getBalance()

All you have to do is declare a variable to store balance in so i will do a small example to show you
see the code below
public class SavingsAcct {
    // define class variables using static keyword.
    private static double annualInterestRate;
    private double p_balance;

 
    /**
     * Set the value of the annual interest rate for all SavingsAccount instances.
     *  
     * @param annualInterestRate
     */
    public static void setAnnualInterestRate(double annualInterestRate) {
        // set value of annualInterestRate variable
    }
   
    // define instance variables/constants
    private final String ACCOUNT_NUMBER; // constants defined using final keyword.
    
 
    /**
     * Two parameter constructor!
     *
     * @param accountNumber
     * @param beginningBalance
     */
    public SavingsAcct(String account_Number, double beginningBalance) {
        // TODO: set values of instance variables here!
    }
 
    /**
     * @return current balance.
     */
    public double getBalance() {
       return p_balance;
    }
 
    /**
     * Set current balance.
     *
     * @param balance
     */
    public void setBalance(double balance) {
       // set the balance here
       p_balance = balance;
    }
 
    /**
     * Get the account number of current instance of SavingsAccount.
     */
    public String getACCOUNT_NUMBER() {
       
    }
   
    /**
     * update the balance, by adding (balance * annualInterestRate / 12) to the current balance.
     */
    public void addMonthlyInterest() {
       
    }
}


So in that we declared a variable balance and we did some work on two funtions void setBalance(double balance) and void getBalance(). Have a look and let me know how you go with that.

Also i did write a tutorial about this a while ago you might be worth having a read over it :) http://www.dreaminco...topic100940.htm

edit ~ just noticed you declared a variable balance in the code note ~ for clarity you should declare them at the top of the class :)

This post has been edited by bbq: 27 June 2009 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 shane123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-June 09

Re: Java SavingsAccount program help

Posted 27 June 2009 - 07:57 PM

View Postbbq, on 27 Jun, 2009 - 05:47 PM, said:

Ok well With your class SavingsAccount you need to declare some variables to store data in. For instance you have a function void getBalance()

All you have to do is declare a variable to store balance in so i will do a small example to show you
see the code below
public class SavingsAcct {
    // define class variables using static keyword.
    private static double annualInterestRate;
    private double p_balance;

 
    /**
     * Set the value of the annual interest rate for all SavingsAccount instances.
     *  
     * @param annualInterestRate
     */
    public static void setAnnualInterestRate(double annualInterestRate) {
        // set value of annualInterestRate variable
    }
   
    // define instance variables/constants
    private final String ACCOUNT_NUMBER; // constants defined using final keyword.
    
 
    /**
     * Two parameter constructor!
     *
     * @param accountNumber
     * @param beginningBalance
     */
    public SavingsAcct(String account_Number, double beginningBalance) {
        // TODO: set values of instance variables here!
    }
 
    /**
     * @return current balance.
     */
    public double getBalance() {
       return p_balance;
    }
 
    /**
     * Set current balance.
     *
     * @param balance
     */
    public void setBalance(double balance) {
       // set the balance here
       p_balance = balance;
    }
 
    /**
     * Get the account number of current instance of SavingsAccount.
     */
    public String getACCOUNT_NUMBER() {
       
    }
   
    /**
     * update the balance, by adding (balance * annualInterestRate / 12) to the current balance.
     */
    public void addMonthlyInterest() {
       
    }
}


So in that we declared a variable balance and we did some work on two funtions void setBalance(double balance) and void getBalance(). Have a look and let me know how you go with that.

Also i did write a tutorial about this a while ago you might be worth having a read over it :) http://www.dreaminco...topic100940.htm

edit ~ just noticed you declared a variable balance in the code note ~ for clarity you should declare them at the top of the class :)



Thanks for the link!
Was This Post Helpful? 0
  • +
  • -

#6 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Java SavingsAccount program help

Posted 27 June 2009 - 10:17 PM

You're welcome
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1