Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Savings Program help

Posted on 2009-06-26
7
Medium Priority
?
1,403 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
 
0
Comment
Question by:bigshane50
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24726372
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.
0
 

Author Comment

by:bigshane50
ID: 24726399
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

0
 
LVL 2

Expert Comment

by:sirtam
ID: 24726544
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:bigshane50
ID: 24726566
this is all I have...It prints out right, but I need help curtailing it to the above requirements.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24729046
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

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 24729131
Then you use your SavingsAccount class in your driver program's main method like this (again, left comments where you can fill in your own code so you learn the concept versus just getting answer).
public class ShaneBartholomewProg4 {
 
	public static void main(String[] argv) throws Exception {
		// 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
}

Open in new window

0
 

Author Closing Comment

by:bigshane50
ID: 31597086
Thanks for all the help...I can manage from here.
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question