CSU Banner

CS 150, Fall 2017

Programming Assignment - P5

While and For Loops Plus Error Handling

Due - October 10th, 2017 at 6:00pm

Late - October 11th, 2017 at 8:00am


Objectives of this Assignment

  1. To write a program that utilizes while and for loops combined with if statements, and
  2. to practice writing code that handles potential user error.

Instructions

Create a Java program called P5 with a class named P5, which after created should contain a file named P5.java in the src folder. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the main method. For this assignment, you must follow the directions below exactly:

    Part One
  1. Declare four additional variables of type double to serve the following purposes: withdrawal amount, principal, interest rate, and future investment amount. Initialize the interest rate variable to 0.025 and the rest to 0.0.
  2. Declare two additional variables of type int to serve the following purposes: number of times per year the investment is compounded and number of years the investment grows. Initialize the variable related to compounding in each year to 2. The other should be initialized to 0.
  3. Add three additional menu prompts after option 1. After the addition, your menu prompt should match the following:
    0: Exit ATM
    1: Make a Deposit
    2: Make a Withdrawal
    3: Display Balance
    4: Calculate Compound Interest
    What action would you like to perform?
  4. Next before the if conditional for menu option 1, insert a while loop that handles the situation of a user entering a value below 0 or above 4 as their selected menu option.
  5. Begin this while loop by printing a blank line (again for better readability).
  6. Then print the following two statements to the console:
    That is not a valid menu option.
    What action would you like to perform?
  7. Before the end of the while loop, write another statement that assigns an integer value via console input (i.e. use your Scanner object) to your variable that holds the user's menu option. This will complete this inner while loop.

  8. Part Two
  9. After the if conditionals for menu options 0 and 1, start by printing a blank line inside the if conditional for menu option 2.
  10. Then print the following prompt to the console:
    Enter the amount to withdrawal:
  11. Assign the value entered by the user to your associated variable.
  12. Next write a while loop inside the if conditional for menu option 2.
  13. This while loop will check to make sure the user is not trying to withdrawal more than the account balance. You may do this verification with the following in the while parentheses:
    accountBalance - withdrawalAmount < 0.0
  14. Inside the while loop print the following two prompts and afterward write a statement that assigns the new value to your withdrawal amount variable.
    Withdrawal amount can not be more than account balance.
    Enter the amount to withdrawal:
  15. After this inner while loop, update the value in your account balance variable.
  16. Lastly, print the new account balance to the console to two decimal places after the prompt below, and follow with a blank line.
    New account balance is: $
  17. For menu option 3, simply print the account balance to the console to two decimal places after the prompt below.
    Current account balance: $

  18. Part Three
  19. The if conditional for menu option 4 will again begin by printing a blank line to the console.
  20. After this, print the following prompt:
    Enter principal investment amount:
  21. Assign the value entered to your associated variable.
  22. Next, write a while loop to handle the situation of a user entering a negative value. The following two prompts should be printed in this situation:
    Principal amount can not be negative.
    Enter principal investment amount:
  23. After the two prompts, write another statement to assign a value entered to the principal variable.
  24. Outside of the previous while loop, print the following:
    Enter number of years to grow investment:
  25. Assign the value entered to your associated variable.
  26. Write another inner while loop to handle the situation of a user entering a negative value. The following two prompts should be printed in this situation:
    Growth period can not be negative:
    Enter number of years to grow investment:
  27. After the two prompts, write another statement to assign a value entered to the variable associated with years of investment growth.
  28. Outside of the second inner while loop, assign the value in the principal variable to your variable for future investment amount.

  29. Part Four
  30. The last part of this assignment involves writing a for loop to calculate the future investment amount.
  31. Initialize your loop variable to 0 and have the terminating loop value be the number of years of growth multiplied by the number of times the interest is compounded during each year (this variable should have been initialized to 2).
  32. Inside the for loop, you will need to execute the following formula (note your variables will likely have different names):
    futureInvestmentAmount *= 1.0 + interestRate/timesCompoundedPerYear
  33. Lastly, inside the if conditional for menu option 4, print a concatenated statement that matches the following:
    In 10 years your principal will grow to $1123.65.
    The values above in blue are example values, but your future value for the principal should be printed with only two values after the decimal point.
  34. And lastly, as good practice, close your Scanner object at the end of your code in main.

Sample Output (One Run With Multiple Options Selected)

Your program should print eight lines. This includes the first, where the user enters the value for testString. User input is shown in blue.

Welcome to the CS150 Simple ATM!
0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
What action would you like to perform? -9 

That is not a valid menu option.
What action would you like to perform? 1 

Enter the amount to deposit: 635.589 
New account balance is: $635.59

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
What action would you like to perform? 2 

Enter the amount to withdrawal: 1542.05 
Withdrawal amount can not be more than account balance.
Enter the amount to withdrawal: 89 
New account balance is: $546.59

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
What action would you like to perform? 4 

Enter principal investment amount: 1500.36 
Enter number of years to grow investment: 10 
In 10 years your principal will grow to $1923.52.

0: Exit ATM
1: Make a Deposit
2: Make a Withdrawal
3: Display Balance
4: Calculate Compound Interest
What action would you like to perform? 0 
Thank you for using the CS150 Simple ATM. Goodbye.

Specifications

Grading Criteria


Submit P5.java to Checkin.


CS Banner
CS Building