-1

Include one additional user input, the percentage by which the extra contribution should be increased each year. For example, suppose the user wants tocontribute$1000 the first year, with a 2% annual increase. At the end of year 1,$1000 would be added to the account. At the end of year 2, the extra amount grows by 2%, so$1020 would be added to the account. At the end of year 3, the extra amount grows by another 2% so $1040.40 would be added to the account, etc. I'm having trouble with the attached code, can anyone help me out?

import java.util.Scanner;

public class GettingRichSlowly {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        // Get user input for initial deposit, annual interest rate,
        //  number of years, amount of yearly extra contribution
        System.out.println("Enter initial deposit: ");
        double initialDeposit = scnr.nextDouble();
        System.out.println("Enter annual interest rate (as a percentage): ");
        double interestRate = scnr.nextDouble();
        System.out.println("Enter number of years: ");
        int years = scnr.nextInt();
        System.out.println("How much extra would you like to contribute at the end of each year? ");
        double extraAmount = scnr.nextDouble();
        System.out.println("How much should the annual contribution increase each year (as a percentage)?");
        double annualIncrease = scnr.nextDouble();
        annualIncrease = annualIncrease / 100 + 1;

        // Amount in the account at the beginning of the current year
        double yearStart = initialDeposit;

        // Repeat years number of times
        int i = 1;
        while (i <= years) {
            // Dollar amount of interest earned during the current year
            double interest = yearStart * interestRate / 100;

            if (i == 1) {
            double yearEnd = yearStart + interest + extraAmount;
            }
            else {
                 extraAmount = extraAmount * annualIncrease;
                double yearEnd = yearStart + interest + extraAmount;
            }

            // Show the results for the current year
            System.out.format("%3d\t$%12.2f\t$%12.2f\t$%12.2f\t$%12.2f\n", i, yearStart, interest, extraAmount, yearEnd);

            // Update yearStart for the following year
            yearStart = yearEnd;

            i++;
        }
    }
}

edit: there are several error messages I get when running this code with the input 100, 7.5, 10, 1000, 5. GettingRichSlowly.java:44: cannot find symbol symbol : variable yearEnd location: class GettingRichSlowly System.out.format("%3d\t$%12.2f\t$%12.2f\t$%12.2f\t$%12.2f\n", i, yearStart, interest, extraAmount, yearEnd); ^ GettingRichSlowly.java:47: cannot find symbol symbol : variable yearEnd location: class GettingRichSlowly yearStart = yearEnd; What do these mean?

  • 1
    By the way, never use `double`/`Double` or `float`/`Float` for money, where you need [accurate results](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems). Use only `BigDecimal` or integers. – Basil Bourque Jun 25 '20 at 21:07
  • 1
    Or a `long` representing the cents. – assylias Jun 25 '20 at 21:08
  • I do not see what is wrong - what do you expect? please show figures, did you think of interest of interest in 2nd year? E. g. interestRate 5 [%] initialDeposit 1000 [e. g. $] extraAmount 500 [e. g. $] annualIncrease 2 [%] what would you expect and what you get? – BitLauncher Jun 25 '20 at 21:14
  • The body within the `if (i == 1)` statement is duplicated within the `else` clause. This if-else can be rewritten to `if (i != 1) { extraAmount = extraAmount * annualIncrease; } double yearEnd = yearStart + interest + extraAmount;`. – MC Emperor Jun 25 '20 at 22:27

1 Answers1

1

You have to declare "double yearEnd" beyond if body.

       double yearEnd;
        if (i == 1) {
            yearEnd = yearStart + interest + extraAmount;
        }
        else {
            extraAmount = extraAmount * annualIncrease;
            yearEnd = yearStart + interest + extraAmount;
        }

        // Show the results for the current year
        System.out.format("%3d\t$%12.2f\t$%12.2f\t$%12.2f\t$%12.2f\n", i, yearStart, interest, extraAmount, yearEnd);
xszym
  • 928
  • 1
  • 5
  • 11
  • Thanks! When I inputted this, it gave me this error: GettingRichSlowly.java:2: class, interface, or enum expected Simulates the growth of money in an account that pays interest. Do you know what this means? – jenna crist Jun 25 '20 at 21:22
  • is it your whole program or do you use GettingRichSlowly class in other program? – xszym Jun 25 '20 at 21:29