Recitation R6
- Control Loops
Spring 2014
CS160: Foundations in Programming
The goal of this lab is to:
- Understand
for
loops.
- Write a
while
loop for a simple ATM.
- Read user input and error check.
- 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:
- Create a new project and class called R6, and put all code in the main method.
- Declare and initialize a scanner to read input from the user.
- Declare a variable to keep track of the user's account balance, initialized to 1000.0.
- Print the welcome message and command menu shown below, you can copy and paste!
- 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:
- Prompt the user to enter a command by printing "Command? " with System.out.print().
- Read the command number from the console and store it in an integer.
- 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? ".
- For any other command number, print "Invalid command!".
- Put the entire command processing loop in a while (true) loop.
- NOTE: Why is the loop from the previous step not infinite?
- There should be a blank line printed after each command is executed.
- 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:
- If command number is 2, use the scanner to read the deposit amount.
- If the deposit is less than zero, print "Invalid deposit amount!".
- Otherwise add the deposit amount to the current balance and print the balance.
- 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:
- If the command number is 3, use the scanner to read the withdrawal amount.
- If the withdrawal is less than zero, print "Invalid withdrawal amount!".
- If the withdrawal exceeds the balance, print "You cannot withdraw more than your bank balance, sorry!".
- Otherwise subtract the withdrawal amount from the current balance and print the balance.
- All dollar values should be formatted to 2 decimal places.
- 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.