//Generalization of example from Page 17, Big Java //Calculate the number of years needed for a given initial amount // to compound to a given target amount at a given interest rate // import javax.swing.JOptionPane; public class ExamplePage17B { public static void main(String[] args) { //Input the starting and target amounts, and interest rate String input; //Input Buffer input = JOptionPane.showInputDialog(null, "Starting Amount?"); final double amount = Double.parseDouble(input); input = JOptionPane.showInputDialog(null, "Target Amount?"); final double target = Double.parseDouble(input); input = JOptionPane.showInputDialog(null, "Interest Rate?"); final double rate = Double.parseDouble(input); //Initialize Current Balance, Interest Amount, and Year Count double balance = amount; //Current balance (amount) double interest; //Interest amount each year int years = 0; //#years to achieve the target //Calculate the # years to achieve the target amount while ( balance < target ) { years = years + 1; //Count another year interest = balance * rate/100; //Calculate the interest balance = balance + interest; //Update current balance } //Display the # years to achieve the target balance String info = "It took " + years + " years at " + rate + "% to increase $" + amount + " to $" + target; JOptionPane.showMessageDialog(null, info); } }