CSU Banner

CS 150, Spring 2018

Lab 9 - Introduction to While Loops and the Start of P5

Wednesday - March 21, 2018


Lab 7 Regrade: If you need a regrade for the week 7 lab, please open Lab 7 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time. Additionally, if you forgot to take the quiz two weeks ago for the single attendance point, your maximum possible grade for the previous lab is now 3 out of 4.

Objectives of this Lab

  1. More practice using a Scanner object to read input,
  2. practice writing a while loop,
  3. learn how to combine your Scanner object within a while loop,
  4. learn how while loops help control the execution flow of a program, and
  5. start the code necessary to complete P5.

Getting Started

Create a new Java Project named P5, and make a class named P5. The inspiration for P5 are the options one might see when using an ATM. When a user chooses an option from an ATM's user interface, a specific action will occur - deposit, withdrawal, display balance, transfer funds, etc. Today's lab asks you to implement two menu options.


Overview of a While Loop

Consider the following while loop:
int j = 0;
while (j <= 6) {
    System.out.println(++j);
} // end while loop

As review from lecture, the first part of a while loop is the Java reserved word while and its associated boolean expression. If the expression evaluates to true, the statement/s inside the while loop will execute. Once all statements have been executed, a program will reevaluate the boolean expression again to determine if the condition is still true. If the expression evaluates to false, a program will flow to the closing brace of a while loop and begin to execute any additional program statements. Remember it is always good practice to comment the closing braces in your program as a way to help find bugs when they occur.

Note of caution: When working with loops it is essential that you make sure to terminate your program before rerunning if you encounter a problem in the execution of your code. If your code has an infinite loop and you do not terminate before running the program again, you run the risk of slowing the performance of your computer down significantly.

Today's Assignment

    Part One
  1. Create a new Java Project in Eclipse called P5. Make a class called P5 and check the box to have Eclipse add the main method for you.
  2. Initialize a Scanner object to read from the keyboard.
  3. Initialize two variables of type double to zero - one to represent a user's account balance and the other to represent a user's deposit amount.
  4. Initialize a variable of type int to 99 to represent a user's choice from the ATM menu options.
  5. Next, print the welcome message to the console with a new line:
    Welcome to the CS150 Simple ATM!
  6. Create a while loop that continues to execute as long as the user's selected menu option is not zero.
    NOTE: The rest of the code for this lab, as well as most of the remaining code for P5, needs to be written inside this while loop. Make sure you understand why this is.
  7. Inside the while loop, first print the following:
    0: Exit Simple ATM
    1: Make a Deposit
    Please select a menu option:

    Make sure there is a space at the end of the last prompt.
  8. After the three prompts, read the user's menu choice into your associated variable.

  9. Part Two
  10. Next, write an if conditional that executes if the user enters zero.
  11. First, start by printing a blank line to the console.
  12. Then write a statement that prints the following message.
    Thank you for using the CS150 Simple ATM. Goodbye.
  13. Next, write another separate if conditional that executes if the user enters one.
  14. It will also start by printing a blank line to the console.
  15. After the empty line, print the following statement to the console inside this if conditional.
    Enter the amount to deposit:
  16. Read the amount entered by the user into your associated variable.
  17. Following this, you will need to write an additional while loop inside the current if conditional related to menu option 1. This while loop will handle the potential condition of a user entering a negative deposit amount.
  18. Your inner while loop should execute if the deposit amount is below 0.0.
  19. Inside this loop, print the following two statements to the console:
    Deposit value cannot be negative.
    Enter the amount to deposit:
  20. While still inside this inner while loop, again read the amount to be deposited into your associated variable.
    Note: The above while loop will not terminate until a user enters either 0.0 or a positive value. This is one way to handle user errors.
  21. Next, outside the inner while loop but still inside option 1's if conditional, you will update the user's account balance by adding the deposit amount to the existing balance.
  22. Lastly, print the user's new account balance to the console. This value should be printed in standard dollars format (i.e. with only two numbers after the decimal point).
  23. Print one more blank line to the console after printing the account balance. This will help will readability since the while will continue to execute until the user enters zero.

Questions to Consider


Sample Console Output (User input shown in blue)

Welcome to the CS150 Simple ATM!
0: Exit ATM
1: Make a Deposit
Please select a menu option: 1 

Enter the amount to deposit: 56.789 
New account balance is: $56.79

0: Exit ATM
1: Make a Deposit
Please select a menu option: 1 

Enter the amount to deposit: -98.00 
Deposit value cannot be negative.
Enter the amount to deposit: 98.00 
New account balance is: $154.79

0: Exit ATM
1: Make a Deposit
Please select a menu option: 0 

Thank you for using the CS150 Simple ATM. Goodbye.



© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building