2 Replies - 726 Views - Last Post: 13 April 2013 - 07:30 PM Rate Topic: -----

#1 Jaset100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-April 13

my Math is not working in my code...

Posted 13 April 2013 - 04:32 PM

Hello, I'm new to the forums here. I am a student taking Java Fundamentals. I have been working on my Saving Account program this week and I think I have everything I need but my interest will not compound. My program is compiling without error but I'm unable to determine why my math is not working. I'm looking for advice that will help me determine what the issue is. My guess is it has to do with my variables within my SavingsAccount class. I've tried changing up my private and public Balance variables from using both to using one private variable but either way yields the same output which is below.

Thank you for any assistance you may offer.

Here is my current 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 $2000.00 10003 $3000.00
2 10002 $2000.00 10003 $3000.00
3 10002 $2000.00 10003 $3000.00
4 10002 $2000.00 10003 $3000.00
5 10002 $2000.00 10003 $3000.00
6 10002 $2000.00 10003 $3000.00
7 10002 $2000.00 10003 $3000.00
8 10002 $2000.00 10003 $3000.00
9 10002 $2000.00 10003 $3000.00
10 10002 $2000.00 10003 $3000.00
11 10002 $2000.00 10003 $3000.00
12 10002 $2000.00 10003 $3000.00

Final balance of both accounts combined: 5000.0
Press any key to continue . . .

Here is my SavingsAccount class:
/*************************************************************************************************************
*SavingsAccount.java
*Jason Reynolds
*
*SavingsAccount class (For my week 4 program)
*************************************************************************************************************/

public class SavingsAccount
{
	private static double annualInterestRate = 0;
	private double Balance;
	final String ACCOUNT_NUMBER;
	public double monthlyBalance;


	public SavingsAccount(String account, double Balance)
	{
		this.ACCOUNT_NUMBER = account;
		this.Balance = Balance;
		this.monthlyBalance = this.Balance;
	}


	// Set the value of the annual interest rate for all SavingsAccount instances.
	public static void setAnnualInterestRate(double annualInterestRate)
	{
		annualInterestRate = .05;
	}


	//@return current balance
	public double getBalance()
	{
		return this.Balance;
	}


	//Get the account number of current instance of SavingsAccount.
	public String getACCOUNT_NUMBER()
	{
		return this.ACCOUNT_NUMBER;
	}


	//update the balance, by adding (balance * annualInterestRate / 12) to the current balance.
	public void addMonthlyInterest()
	{
		this.Balance += ((this.Balance * annualInterestRate) / 12);
	}


	//This method makes the format to be printed
	public String toString()
	{
		return String.format("$%.2f", Balance);

	}//end toString
}



Here is my driver program:

/*************************************************************************************************************
*JasonReynoldsProg4.java
*Jason Reynolds
*
*This is my Saving Account Driver Program
*************************************************************************************************************/
import java.util.Scanner;

public class JasonReynoldsProg4
{
    public static void main(String[] args)
    {

		SavingsAccount saver1 = new SavingsAccount("10002", 2000.00);
		SavingsAccount saver2 = new SavingsAccount("10003", 3000.00);
		SavingsAccount.setAnnualInterestRate(0.05);  // Instantiate two SavingsAccount objects


    	System.out.println( "\nMonthly balances for one year with 0.05 annual interest:\n" );
		//Table heading
	    System.out.printf("%10s%12s%10s%12s%10s\n", "Month", "Account#", "Balance", "Account#", "Balance");
		System.out.printf("%10s%12s%11s%11s%11s\n", "-----", "--------", "--------", "--------", "--------");
		System.out.printf("%10s%12s%11s%11s%11s\n", "0",saver1.getACCOUNT_NUMBER(), saver1.toString(), saver2.getACCOUNT_NUMBER(), saver2.toString());


		// 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
			//This outputs the incremented amounts monthly
			saver1.addMonthlyInterest();
			saver1.getBalance();
			saver1.getACCOUNT_NUMBER();
			saver2.addMonthlyInterest();
			saver2.getBalance();
			saver2.getACCOUNT_NUMBER();

			System.out.printf("%10s%12s%11s%11s%11s\n", month, saver1.getACCOUNT_NUMBER(), saver1.toString(), saver2.getACCOUNT_NUMBER(), saver2.toString());
		}//end for

	// After the last month’s printout, compute and display the total of both balances.
	double total = saver1.getBalance() + saver2.getBalance();
	System.out.printf( "%10s%10s\n", "\nFinal balance of both accounts combined:", total);

	}// End Main
}



Is This A Good Question/Topic? 0
  • +

Replies To: my Math is not working in my code...

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6377
  • View blog
  • Posts: 25,768
  • Joined: 12-December 12

Re: my Math is not working in my code...

Posted 13 April 2013 - 04:59 PM

    // Set the value of the annual interest rate for all SavingsAccount instances.
    public static void setAnnualInterestRate(double annualInterestRate) {
        SavingsAccount.annualInterestRate = .05; // .05 should be the the passed parameter
    }

Without the class-name qualifier then only the parameter's (local) value is changed, not that static variable's value. It would work if you changed the name of your parameter:

    // Set the value of the annual interest rate for all SavingsAccount instances.
    public static void setAnnualInterestRate(double newAnnualIR) {
        annualInterestRate = newAnnualIR;
    }

This post has been edited by andrewsw: 13 April 2013 - 05:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 Jaset100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-April 13

Re: my Math is not working in my code...

Posted 13 April 2013 - 07:30 PM

andrewsw,

Spot on. After making the change, it works as intended. Thank you so much for the help and quick response.

Jason
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1