0

Greetings,
Can anyone tell me why my loop does not actually add anything for interest? I'm thinking it is because of the way my interestRate variable is setup as a class variable (static). Here is the code snippets

/****************************************************************************************
HNUnit4Ch13Investor.java
xx

All variables should be declared private
Declare a class variable called interestRate (This will hold the annual interest rate)
Declare a constant called ACCOUNT_NUMBER
Declare an instance variable called balance
Provide a class method that will be used to set the annual interest rate
Provide a two parameter constructor to initialize the constant account
number and balance
Provide an addInterest() method to update the balance based on the
interestRate entered in the driver
Add the interest using (balance * interestRate / 12)
****************************************************************************************/

public class HNUnit4Ch13Investor 
{
    private static double interestRate;
    private int ACCOUNT_NUMBER;
    private double balance;

    public static double getInterestRate()
    {
        return interestRate;
    }

    public static void setInterestRate(double apr)
    {
        interestRate = apr;
    }

    public HNUnit4Ch13Investor(int ACCOUNT_NUMBER, double balance)
    {
        this.ACCOUNT_NUMBER = ACCOUNT_NUMBER;
        this.balance = balance; 
    }

    public int getACCOUNT_NUMBER()
    {
        return ACCOUNT_NUMBER;
    }

    public double getBalance()
    {
        return balance;
    }

    public void addInterest()
    {
        balance = balance + balance * interestRate / 12;
    }

}

main

import java.util.*;

/****************************************************************************************
HNUnit4Ch13.java
xx

Instantiate an investor1 object using a two parameter constructor passing 
the account number 1001 and the initial balance of $2,000
Instantiate an investor2 object using a two parameter constructor passing 
the account number 2001 and the initial balance of $4,000
Get input for interest rate (in the format .08 for 8%)
Output header as per sample showing interest rate (Use class variable to set interest rate)
Print the table using a for loop displaying the current month and calling:
addInterest() to add the monthly interest for each iteration
getBalance() to display the current balance
Output results for 15 months
Print the interest earned as shown in the sample for each investor
utilizing a printf statement. Don’t hard code the interest earned, or interest rate.
****************************************************************************************/

public class HNUnit4Ch13
{
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);
        double apr = 0;

        HNUnit4Ch13Investor investor1 = new HNUnit4Ch13Investor(1001, 2000);
        HNUnit4Ch13Investor investor2 = new HNUnit4Ch13Investor(2001, 4000);
        HNUnit4Ch13Investor.setInterestRate(apr);

        System.out.print("Please enter the APR in the form of .05 for 5%: ");
        apr = input.nextDouble();  

        System.out.printf("Monthly balances for one year with %.2f annual interest:\n\n", apr);
        System.out.println("Month Account #    Balance  Account #    Balance");
        System.out.println("----- ---------    -------  ---------    -------");

        for(int Month = 0; Month <= 15; Month ++)
        {
            System.out.printf("%5d%10d%11.2f%11d%11.2f",Month,investor1.getACCOUNT_NUMBER(),investor1.getBalance(),investor2.getACCOUNT_NUMBER(),investor2.getBalance());

            System.out.println();

            if (Month < 15)
            {
                investor1.addInterest();
                investor2.addInterest();
            }   
        }

        //System.out.println("\nInvestor1 earned : " + investor1.getBalance();

    }
}

My output:

Please enter the APR in the form of .05 for 5%: .05
Monthly balances for one year with 0.05 annual interest:

Month Account #    Balance  Account #    Balance
----- ---------    -------  ---------    -------
    0      1001    2000.00       2001    4000.00
    1      1001    2000.00       2001    4000.00
    2      1001    2000.00       2001    4000.00
    3      1001    2000.00       2001    4000.00
    4      1001    2000.00       2001    4000.00
    5      1001    2000.00       2001    4000.00
    6      1001    2000.00       2001    4000.00
    7      1001    2000.00       2001    4000.00
    8      1001    2000.00       2001    4000.00
    9      1001    2000.00       2001    4000.00
   10      1001    2000.00       2001    4000.00
   11      1001    2000.00       2001    4000.00
   12      1001    2000.00       2001    4000.00
   13      1001    2000.00       2001    4000.00
   14      1001    2000.00       2001    4000.00
   15      1001    2000.00       2001    4000.00
2
Contributors
2
Replies
23
Views
1 Month
Discussion Span
Last Post by SkateX
2

26: double apr = 0;
30: HNUnit4Ch13Investor.setInterestRate(apr); // sets rate to zero
33: apr = input.nextDouble(); // this is the real rate, but too late. You never use it

Edited by JamesCherrill: Formatting

0

I ended up swapping my code around and it works like a charm. For your situational awareness the modified code is below:

        System.out.print("Please enter the APR in the form of .05 for 5%: ");
        double apr = input.nextDouble();  
        HNUnit4Ch13Investor.setInterestRate(apr);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.