Colorado State University

Recitation R6 - Loops & Editor Tips
Fall 2013

CS160: Foundations in Programming


The goal of this lab is to:
  1. Understand for loops.
  2. Write a while loop for a simple ATM.
  3. Read input and error check.
  4. Manage flow of control using boolean values.
  5. Prepare for the next programming assigment.

Phase 1: How for, while and do while loops work

WITHOUT RUNNING THIS CODE, answer the following questions as comments at the bottom of your code:

Question 1) What does the following loop output?
for(int i = 0; i < 5; i++)
    System.out.println(i);
Question 2) What does the following loop output?
int j = 0;
while (j <= 9) {
    System.out.println(++j);
}
Question 3) What does the following loop output?
for(int j = 10; j >= 0; j--);
    System.out.print(j + " ");
    // are you sure?
Question 4) What does the following loop output?
for(int n = 0; n > 0; n++)
    System.out.println(n);

Question 5) What does the following loop output?
int j = 0;
do {
    System.out.println(j);
    j--;
}
while (j > 0);


Write the following program from scratch in the following phases:

Phase 2

  1. Create a new class in Eclipse called R6.
    - Check the box to have Eclipse add the main method for you
  2. Print the welcome message and beginning of the menu:

    Welcome to the CS160 Simple ATM!
    0) Exit Simple ATM


  3. Add a scanner to read input from the user (the console).
  4. Add a variable to keep track of the user's account balance.
  5. while the user wants to complete an action:
  6. If the user enters a 0, the program should end, otherwise, prompt again.
  7. When the loop is done, print the goodbye message:
    Thanks for using the CS160 Simple ATM, goodbye!
Sample:
Welcome to the CS160 Simple ATM!
--------------------
0) Exit ATM
--------------------
What action would you like to complete? 1

What action would you like to complete? 0

Thanks for using the CS160 Simple ATM, goodbye!

Phase 3

  1. Add another menu option:

    1) Make a deposit

  2. If the user types a 1,
  3. ALL dollar values from this point forward should be formatted to 2 decimal places
  4. If they type any other value than 0 or 1, tell them it's not a valid option
Sample:
Welcome to the CS160 Simple ATM!
--------------------
0) Exit ATM
1) Make a deposit
--------------------
What action would you like to complete? 2
That is not a valid option!

What action would you like to complete? 1
How much are you depositing? 100
Your new balance is: $100.00

What action would you like to complete? 0

Thanks for using the CS160 Simple ATM, goodbye!

Phase 4

  1. Add another menu option:

    2) Withdraw money

  2. If the user types a 2,
  3. Test your program!
Sample:
Welcome to the CS160 Simple ATM!
--------------------
0) Exit ATM
1) Make a deposit
2) Withdraw money
--------------------
What action would you like to complete? 3
That is not a valid option!

What action would you like to complete? 1
How much are you depositing? 100
Your new balance is: $100.00

What action would you like to complete? 2
How much are you withdrawing? 40
Your new balance is: $60.00

What action would you like to complete? 0

Thanks for using the CS160 Simple ATM, goodbye!

Phase 5

  1. Add the final menu option:

    3) Check balance

  2. If the user types a 3, tell them their current running balance
  3. There should be a blank line after each option is fully executed
Here is some sample output:
Welcome to the CS160 Simple ATM!
--------------------
0) Exit ATM
1) Make a deposit
2) Withdraw money
3) Check Balance
--------------------
What action would you like to complete? 3
Your current balance is: $0.00

What action would you like to complete? 1
How much are you depositing? 100
Your new balance is: $100.00

What action would you like to complete? 2
How much are you withdrawing? 20
Your new balance is: $80.00

What action would you like to complete? 3
Your current balance is: $80.00

What action would you like to complete? 4
That's not a valid option!

What action would you like to complete? 0

Thanks for using the CS160 Simple ATM, goodbye!

Show your R6.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2013 CS160 Colorado State University. All Rights Reserved.