We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Java Savings Program help

Medium Priority
1,510 Views
Last Modified: 2013-11-23
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 months 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 months 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.
 8
 
SavingsAccount.java class

In the SavingsAccount class, declare a class variable called annualInterestRate, an instance constant called ACCOUNT_NUMBER, and an instance variable called balance. Provide a two-parameter constructor to initialize the instance constant and instance variable, and provide accessors for the instance constant and instance variable. Provide an addMonthlyInterest method to update the balance, by adding (balance * annualInterestRate / 12) to the current balance.

 

You are also required to provide a class method that sets the annual interest rate.

 
 32
 
YourNameProg4.java class

In the YourNameProg4 Driver class, instantiate the saver1 and saver2 objects. Set the annual interest rate at 0.05. Print the table heading lines. Use a for loop to print the initial account numbers and balances and the account numbers and balances each month after that months interest has been added. After the last months printout, compute and display the total of both balances.    
 24
 
Your driver should generate the following outputs:

 

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
 
Comment
Watch Question

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
What is the question?  Are you getting an error in your classes compiling or are the results not coming out as they should?  Please post what you have already and describe what you are stuck on.

Author

Commented:
Here is what I have...It works but it does not go inline with what the teacher is requesting
/******************************************************************************
 * SavingsAccount.java
 * 
 * Shane Bartholomew
 * 
 * This program works with ShaneBartholomewProg4.java to establish 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. 
 * Settting a common rate of interest at 5% per year. At the end of each month, 
 * it will update the balance by adding one months interest to the balance, 
 * so that the balance compounds monthly. Finally it will print an output that 
 * displays the month number and the account number and the balance for each 
 * of the two accounts.
 ******************************************************************************/
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.math.MathContext;
 
public class SavingsAccount {
 
    public static void main(String[] argv) throws Exception{
    }
 
    public static BigDecimal Calculate(BigDecimal balance, BigDecimal interest){
        //Declare variables for the math calculations
        BigDecimal numMonth = new BigDecimal(12);
        BigDecimal monthlyAPR = new BigDecimal(0.00);
        BigDecimal newbalance = new BigDecimal(0.00);
        MathContext size = new MathContext(7);
 
        //Perform math calculations to determine monthly interest
        interest = interest.setScale(2, RoundingMode.HALF_UP);
        monthlyAPR = balance.multiply(interest).divide(numMonth, size).setScale(2, RoundingMode.HALF_UP);
        newbalance = balance.add(monthlyAPR).setScale(2, RoundingMode.HALF_UP);
 
        //Return the new balance back to Main Prog for output
        return newbalance;
    }//End Calculate
}
 
 
AND the driver
 
/******************************************************************************
 * ShaneBartholomewProg4.java
 * 
 * Shane Bartholomew
 * 
 * This program 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's a common rate of interest at 
 * 5% per year. At the end of each month, it will update the balance by adding 
 * one months interest to the balance, so that the balance compounds monthly. 
 * Finally it will print an output that displays the month number and the 
 * account number and the balance for each of the two accounts.
 ******************************************************************************/
 
import java.math.BigDecimal;
import java.math.RoundingMode;
 
 
public class ShaneBartholomewProg4 {
 
    public static void main(String[] argv) throws Exception{
        //Prints the table heading lines.
        System.out.println("Monthly balances for one year with 0.05 annual interest:" + "\r\n");
        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","-----","---------","-------","---------","-------");
 
      /* Declare variables for saver1 and saver2 accounts, also using BigDecimal
       * rather than double will allow the programmmer access to math functions 
       * embedded in java */
        int Months = 12;
        String saver1 = "10002";
        String saver2 = "10003";
        BigDecimal sav1rnd = new BigDecimal(2000.00);
        BigDecimal sav2rnd = new BigDecimal(3000.00);
 
        //Interest rate and total variables with setscale for 2 decimal places
        BigDecimal apr = new BigDecimal(0.05);
        BigDecimal Total = new BigDecimal(0.00);
        sav1rnd = sav1rnd.setScale(2, RoundingMode.HALF_UP);
        sav2rnd = sav2rnd.setScale(2, RoundingMode.HALF_UP);
 
        /* A for loop to print the initial account numbers and balances and 
         * the account numbers and balances each month after that months 
         * interest has been added.*/
         
        for(int i = 0;i < Months; i++){
            System.out.printf("%5s %14s \t %8s %8s \t %8s \n",i,saver1,sav1rnd,saver2,sav2rnd);
            sav1rnd = (SavingsAccount.Calculate(sav1rnd,apr));
            sav2rnd = (SavingsAccount.Calculate(sav2rnd,apr));
            Total = sav1rnd.add(sav2rnd);
        }
 
        //Last months printout, compute and display the total of both balances.
        System.out.println("\n" + "Final balance of both accounts combined: " + Total);
 
        //Exit program
        System.exit(0);
    }//End Main
}

Open in new window

Commented:
I see you're using a 'main' in both classes.

Did you have another class to drive both these classes?

If so, drop the main in both and use the starting class to make object to these two.
None of the two provided classes is communicating, so please provide the rest of the code if possible

Author

Commented:
this is all I have...It prints out right, but I need help curtailing it to the above requirements.
Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Firstly start with the structure of your SavingsAccount per the instructions of your assignment.  Please find below a stub that you will need to fill in with code.  I have made comments to help guide you.  Feel free to use BigDecimal instead of double, it is just what I typed off hand.
public class SavingsAccount {
	// 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 SavingsAccount(String accountNumber, 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() {
		
	}
}

Open in new window

Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for all the help...I can manage from here.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.