Colorado State University

Recitation R6 - Control Loops
Spring 2014

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 user input and error check.
  4. Manage flow of control with loops, switches, and conditionals.

Understanding for, while and do while loops

WITHOUT RUNNING THIS CODE, answer the following questions on paper or in a file:

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 by decomposing the problem into in the following phases, testing after each step:

Phase 1: Create the command menu and key variables:

  1. Create a new project and class called R6, and put all code in the main method.
  2. Declare and initialize a scanner to read input from the user.
  3. Declare a variable to keep track of the user's account balance, initialized to 1000.0.
  4. Print the welcome message and command menu shown below, you can copy and paste!
  5. Now test your program as shown in the sample output:
	Welcome to the CS160 Simple ATM!
	--------------------
	0) Exit program
	1) Check balance
	2) Make a deposit
	3) Withdraw money
	--------------------
	

Phase 2: Implement a simple command processing loop:

  1. Prompt the user to enter a command by printing "Command? " with System.out.print().
  2. Read the command number from the console and store it in an integer.
  3. Implement a switch on the command number, for each user input as follows:

    When input is 0, print "Thanks for using the CS160 Simple ATM, goodbye!" and call System.exit(0).
    When input is 1, print "Your current balance is " and the current balance.
    When input is 2, print "How much are you depositing? ".
    When input is 3, print "How much are you withdrawing? ".

  4. For any other command number, print "Invalid command!".
  5. Put the entire command processing loop in a while (true) loop.
  6. NOTE: Why is the loop from the previous step not infinite?
  7. There should be a blank line printed after each command is executed.
  8. Now test your program as shown in the sample output:
	Welcome to the CS160 Simple ATM!
	--------------------
	0) Exit program
	1) Check balance
	2) Make a deposit
	3) Withdraw money
	--------------------
	Command? 1 
	Your current balance is $1000.0.

	Command? 2 
	How much are you depositing?

	Command? 3 
	How much are you withdrawing? 

	Command? 4 
	Invalid command!

	Command? 0
	Thanks for using the CS160 Simple ATM, goodbye!
	

Phase 3: Implement the deposit command:

  1. If command number is 2, use the scanner to read the deposit amount.
  2. If the deposit is less than zero, print "Invalid deposit amount!".
  3. Otherwise add the deposit amount to the current balance and print the balance.
  4. Now test your program as shown in the sample output.
	Welcome to the CS160 Simple ATM!
	--------------------
	0) Exit program
	1) Check balance
	2) Make a deposit
	3) Withdraw money
	--------------------
	Command? 2
	How much are you depositing? 100
	Your new balance is: $1100.00

	Command? 2
	How much are you depositing? -100
	Invalid deposit amount!

	Command? 0
	Thanks for using the CS160 Simple ATM, goodbye!
	

Phase 4: Implement the withdrawal command:

  1. If the command number is 3, use the scanner to read the withdrawal amount.
  2. If the withdrawal is less than zero, print "Invalid withdrawal amount!".
  3. If the withdrawal exceeds the balance, print "You cannot withdraw more than your bank balance, sorry!".
  4. Otherwise subtract the withdrawal amount from the current balance and print the balance.
  5. All dollar values should be formatted to 2 decimal places.
  6. Now test your program as shown in the sample output.
	Welcome to the CS160 Simple ATM!
	--------------------
	0) Exit program
	1) Check balance
	2) Make a deposit
	3) Withdraw money
	--------------------
	Command? 2
	How much are you withdrawing? 500.01
	Your new balance is: $499.99

	Command? 2
	How much are you withdrawing? -100
	Invalid withdrawal amount!

	Command? 2
	How much are you withdrawing? 9999.99
	You cannot withdraw more than your bank balance, sorry!

	Command? 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.

© 2014 CS160 Colorado State University. All Rights Reserved.