Recitation R3
- Java: Control Loops and Restaurant Receipts
Summer 2016
CS160: Foundations in Programming
Preview
In this recitation we will:
- In class quiz covering Conditional logic and concepts from the previous week
- Learn about loops as another way to control the execution flow of a program.
- Introduction to methods.
- Interpret code to gain a better understanding of loops.
- Use Hackerrank to practice coding different types of loops.
- Complete a pair programming exercise and turn into Checkin.
Interpretation Section
- What does the following code print?
for(int i = 1; i < 12; i+=2)
System.out.println(i);
What does the following code print?
int j = 0;
while (j <= 8) {
System.out.println(++j);
}
What does the following code print?
for (int k = 7; k >= 0; k--);
System.out.print(k + " ");
// are you sure?
What does the following code print?
for (int l = 0; l > 0; l++)
System.out.println(l);
What does the following code print?
int m = 0;
do {
System.out.println(m);
m--;
}
while (m > 0);
Practice Problems
- Log into Hackerrank
- Complete the section named R3
- These practice problems will be open after this recitation as an additional resource.
- Remember, you can use the examples from this recitation and the lecture notes.
Getting Started: Restaurant Reciepts
Specifications: Finishing the Program
- Instantiate and initialize a Scanner object.
- Prompt the user for the name of the restaurant.
- Read in the user's input to the variable restaurantName.
- The restaurant can be be more than one word, like "Park Sushi".
- Prompt the user for the name of the server.
- Store the value in the variable serverName.
- The server's name can be either:
- A first name
- A first and a last name, separated by one space character.
- Prompt the user for the cost of the bill.
- Store the value in the variable subtotal.
- Write a method to calculate the tax on the bill based on the subtotal and taxRate.
public static double computeTax(double amount, double rate)
- Call the method and store the result in the variable tax.
- Calculate the total bill by adding the subtotal and tax together. Store the total bill in the variable total.
- Write a method to calculate the suggested tip, based on the total and the tip rate.
- Here is the signature of the method:
public static double computeTip(double amount, double rate)
- Using the calculated values you stored in variables, print a receipt to the console.
- Print the receipt one line at a time. You can use println() and printf() to do this.
- Only the server's first name should be printed! If the server only has a first name, this isn't a problem! But what about if the server provides and first and last name? This would be a good time to check out some String methods!
- You should make calls to the computeTip method for each of the three tip rates.
Sample Output
Make sure you copy exactly the format of the receipt as seen here.
- NOTE: this does not include the prompts and input in steps 1-6 because you are allowed to customize your prompts.
=====================================
Park Sushi
Server: JULIE
Subtotal: $56.23
Tax: $2.81
=====================================
Total: $59.04
Suggested tips:
10%: $5.90
15%: $8.86
20%: $11.81
Thank you!
=====================================
Additional Tips and Tricks
Note the following details:
- The restaurant is printed exactly how the user typed it.
- The server name is only the first name converted to all uppercase.
- There are spaces after every colon, except the line with "Suggested tips:".
- There are dollar signs before every money amount.
- All money amounts are formatted to exactly two decimal places.
Some tips:
Once you have completed the interpretation exercise, Hackerrank activities, and submitted R3.java to Checkin you are finished.
© 2016 CS160 Colorado State University. All Rights Reserved.