CSU Banner

CS 150, Fall 2017

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

Monday - October 2, 2017


Objectives of this Lab

  1. Practice using a Scanner object to read input,
  2. practice writing a while loop,
  3. learn how to combine your Scanner object with 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 their ATM, a specific action will occur - deposit, withdrawal, display balance, transfer funds, etc. Today's lab asks you to implement two of the five options from P5.

Overview of a While Loop

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

For review from lecture, the first part of a while loop is the reserved word while and its associated boolean expression. If the expression evaluates to true, the statements inside the while loop will be executed. 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 additional program statements. Remember it is always good practice to comment the closing braces in your program 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

  1. Create a new Java Project in Eclipse called P5. Make a class also called P5 and check the box to have Eclipse add the main method for you.
  2. Declare and initialize a Scanner object to read from the keyboard.
  3. Declare two variables of type double, one to represent a user's account balance and the other to represent a user's deposit amount. Initialize both to zero.
  4. Declare a variable of type int to represent a user's choice from the ATM menu options and initialize it to 99.
  5. Next, print the welcome message to the console:
    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, meaning this while loop will surround all of the code in the following instructions except the code for closing your scanner.
  7. Inside the while loop, first print the following:
    0: Exit Simple ATM
    1: Make a Deposit
    What action would you like to perform?

    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. Write an if conditional that prints the following if the user enters zero.
    Thank you for using the CS150 Simple ATM. Goodbye.
  10. Next, write another if conditional for the user's choice of one that starts by printing a blank line to the console.
  11. After the empty line, print the following statement to the console inside this if conditional.
    Enter the amount to deposit:
  12. Read the amount entered by the user into your associated variable.
  13. 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.
  14. Your while loop should execute if the deposit amount is below 0.0.
  15. Inside this loop, print the following two statements to the console:
    Deposit value may not be negative.
    Enter the amount to deposit:
  16. Lastly, 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.
  17. 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.
  18. 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) following the prompt:
    New account balance is:
  19. 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

Welcome to the CS150 Simple ATM!
0: Exit ATM
1: Make a Deposit
What action would you like to perform? 1 

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

0: Exit ATM
1: Make a Deposit
What action would you like to perform? 1 

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

0: Exit ATM
1: Make a Deposit
What action would you like to perform? 0 
Thank you for using the CS150 Simple ATM. Goodbye.



CS Banner
CS Building