// HowRich.java // Solution to CS 5 Short Assignment #2 by THC. // Computes the balance in year n for $1.00 deposited in year 0, where // the interest rate compounds at 5% per year. Also computes how many Berry // Libraries we can build with that amount, where each Berry Library costs // $50,000,000. import java.util.Scanner; public class HowRich { public static void main(String[] args) { // 5% interest per year. final double INTEREST_RATE = 0.05; // The starting balance in year 0; final double INITIAL_BALANCE = 1.0; double balance = INITIAL_BALANCE; // current balance // The year in which we want the balance. int targetYear; // Get the year from the user. System.out.print("In what year would you like to know the balance? "); Scanner input = new Scanner(System.in); targetYear = input.nextInt(); // Compound the balance once per year. for (int year = 1; year <= targetYear; year++) balance = balance * (1.0 + INTEREST_RATE); System.out.println("In the year " + targetYear + ", the balance is $" + balance + "."); final double BERRY_COST = 50000000; // cost of Berry Library System.out.println("You could build " + (balance / BERRY_COST) + " Berry Libraries with that."); } }