Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This answers Exercise 3 of Worksheet one, please read and understand the code
- * and check it against resources available online, or ask your tutor as I think
- * that there might be some refinement needed here.
- *
- * @author Shaun B
- * @version 2012-10-30
- **/
- import java.util.Scanner;
- public class exerciseThree
- {
- // Here is our fixed interest rate expressed as a final (ie, cannot be modified):
- private static final double fixedInterestRate = (double)2.3;
- private static int numberOfYears = 0;
- private static Scanner keyboardInput = new Scanner(System.in);
- private static boolean run = true;
- public static void main(String [] elephants)
- {
- // Local variables available in this scope only:
- int i = 0;
- double total = 0.00;
- double capital = 0.00;
- int step = 0;
- double currentBalance = 0.00;
- String keyboardError="Illegal keyboard entry in main class";
- // User instructions:
- System.out.format("The current fixed interest rate is at %f\n",fixedInterestRate);
- System.out.println("This example will ask for an investment capital and add");
- System.out.println("the interest after each year up to 500 years. You will be");
- System.out.println("asked to enter your initial capital amount, how many years");
- System.out.println("of interest and the step in each year(ie, 5 will show you");
- System.out.println("how much interest is added after each five years up to the");
- System.out.println("maximum that you enter).");
- while (run)
- {
- System.out.print("How much capital do you wish to invest? £");
- try
- {
- capital = keyboardInput.nextDouble();
- }
- catch(Exception e)
- {
- System.err.println(keyboardError);
- run = false;
- System.exit(0);
- }
- System.out.print("How many years do you wish to calculate the interest? ");
- try
- {
- numberOfYears = keyboardInput.nextInt();
- }
- catch(Exception e)
- {
- System.err.println(keyboardError);
- run = false;
- System.exit(0);
- }
- System.out.print("How often do you wish to see the balance (ie, every 5 years)? ");
- try
- {
- step = keyboardInput.nextInt();
- }
- catch(Exception e)
- {
- System.err.println(keyboardError);
- run = false;
- System.exit(0);
- }
- if (capital == 0 || numberOfYears == 0 || step == 0)
- {
- System.out.println("Goodbye!");
- run = false;
- System.exit(0);
- }
- for( i=1; i<=numberOfYears; ++i )
- {
- // The exercise seems to be asking for a simple interest calculation,
- // rather than a compound interest calculation, and this is the formula
- // provided on the sheet, so it seems to be okay. I'd check this - search
- // online for a simple interest calculator in Java or C/C++ and you
- // should have a more perfect solution I think.
- currentBalance = (double)capital*(1.00f + 0.01f * fixedInterestRate);
- if(i%step==0 && i!=0)
- {
- // Prints out the current balance to two decimal places:
- System.out.printf("After %d years, the balance is %.2f", i, currentBalance);
- System.out.println("");
- }
- capital = (double)currentBalance;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement