Recitation R6
- Loops & Editor Tips
Fall 2013
CS160: Foundations in Programming
The goal of this lab is to:
- Understand
for
loops.
- Write a
while
loop for a simple ATM.
- Read input and error check.
- Manage flow of control using boolean values.
- 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
- Create a new class in Eclipse called R6.
- Check the box to have Eclipse add the main method for you
- Print the welcome message and beginning of the menu:
Welcome to the CS160 Simple ATM!
0) Exit Simple ATM
- Add a scanner to read input from the user (the console).
- Add a variable to keep track of the user's account balance.
- while the user wants to complete an action:
- Ask them what they want to do
- Save the response into a variable
- Use a boolean variable to know when to stop
- If the user enters a 0, the program should end, otherwise, prompt again.
- 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
- Add another menu option:
1) Make a deposit
- If the user types a 1,
- Ask how much they want to deposit
- Read in the value into a variable
- If the value is greater than 0
- Add that value into the running balance
- Tell them their new balance
- Otherwise tell them that it's not a valid amount
- ALL dollar values from this point forward should be formatted to 2 decimal places
- 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
- Add another menu option:
2) Withdraw money
- If the user types a 2,
- Ask how much they want to withdraw
- Read the value into a variable
- If the value is less than the current balance
- Subtract the value from the running balance
- Tell them their new balance
- Otherwise, tell them they don't have enough money to do that
- 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
- Add the final menu option:
3) Check balance
- If the user types a 3, tell them their current running balance
- 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.