Results 1 to 7 of 7
  1. #1
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Question Savings Account - Need to list months

    Hello,
    I am working on an assignment for a class. It involves creating a custom class to create a savings account, modify the annual interest rate for all savings accounts, and calculating the monthly interest. Then I created a test with two new accounts (saver1 and saver2). I was to set the interest rate to 4% then "calculate the monthly interest for each of 12 months and print the new balances for both savers". To me, this means I need to print something like:

    January - (balance for that month)
    February - (balance for that month)
    ...etc.

    I know how to calculate the balance after 1 month, and I think I could figure out using loops to print out the balance for each month, but I have a feeling that my professor wants me to use arrays and loops to print it out like above.

    The exact instructions for this assignment are:

    Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12-this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest for each of 12 months and print the new balances for both savers. Next, set the annualInterestRate to 5%, calculate the next month's interest and print the new balances for both savers.

    Here is the code I have right now:

    Java Code:
    class SavingsAccount
    {
    	private static double annualInterestRate;
    	private double savingsBalance;
    	
    	public SavingsAccount(double savingsBalance)
    	{
    		setBalance( savingsBalance );
    	}
    	
    	public static void modifyInterestRate(double i)
    	{
    		annualInterestRate = i;
    	}
    	 
    	public void setBalance(double b)
    	{
    		savingsBalance = b;
    	}
    	 	 
    	public double getBalance()
    	{
    		return savingsBalance;
    	}
    	
    	public double calculateMonthlyInterest() 
    	{
    		savingsBalance += savingsBalance * ((annualInterestRate/100)/12);
    		return savingsBalance;
    	}
    		
    	
    }

    And my test (a little haphazard right now):

    Java Code:
    public class SavingsAccountTest
    {
    	
    	public static void main (String [] args)
    	{
    		SavingsAccount saver1 = new SavingsAccount(2000.00);
    		
    		System.out.println("Balance for account 'saver1': " + saver1.getBalance());
    		
    		saver1.modifyInterestRate(4);
    		
    		saver1.calculateMonthlyInterest();
    		
    		System.out.println("New balance for account 'saver1': " + saver1.getBalance());
    		
    		SavingsAccount saver2 = new SavingsAccount(3000.00);
    		
    		System.out.println("Balance for account 'saver2': " + saver2.getBalance());
    		
    		saver2.modifyInterestRate(4);
    		
    		saver2.calculateMonthlyInterest();
    		
    		System.out.println("New balance for account 'saver2': " + saver2.getBalance());
    		
    	}
    	
    }
    Any help is greatly appreciated. Thank you!

  2. #2
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Also, is there a way to suppress the output so there is only 2 digits after the decimal?
    Thanks again

  3. #3
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    I was able to use a for loop to have the program output 12 values (the balance for each month). I'm still having the issue of rounding though.

    Here is the loop I used:

    Java Code:
    for (int m = 1; m <= 12; m++)
    	{
    				
    		saver1.calculateMonthlyInterest();
    		
    		System.out.println("Balance for 'saver1': " + saver1.getBalance());
    		
    	}

  4. #4
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Cool :P

    Guess I figured it out on my own...I didn't use any arrays, so hopefully this will still be ok.

  5. #5
    delgeezee is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default

    How did you solve the rounding issue? I am working on this same assignment.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You shouldn't use double or float to represent money due to the inherent inaccuracies. You can use BigDecimal or use an int multiplied by a factor of 100. That is $5.67 becomes 567.

    If you do/must use double then you can use DecimalFormat, the printf method or the String.format method. NOTE: you only need to round to 2 decimals when displaying the amount. Do not change the value stored in your double variable.

  7. #7
    Moka is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    @Junky : Yes, I'm aware of BigDecimal and how it should be used with monitary values, but I'm still not 100% clear on how it works and I needed to get this assignment done quick.

    @delgeezee : I landed out using DecimalFormat. It actually worked nicely.

Similar Threads

  1. printing out months
    By droidus in forum New To Java
    Replies: 2
    Last Post: 04-12-2011, 09:14 PM
  2. Savings account balance comparison code.
    By Rhyssa6 in forum New To Java
    Replies: 2
    Last Post: 04-10-2011, 12:16 PM
  3. Replies: 2
    Last Post: 12-29-2010, 07:30 AM
  4. Savings Account Comparison
    By shivam kush in forum Networking
    Replies: 0
    Last Post: 10-19-2010, 11:03 AM
  5. Displaying 12 calendar months?
    By jaydubya in forum New To Java
    Replies: 3
    Last Post: 09-16-2008, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •