Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task #1 The if Statement, Comparing Strings, and Flags 1. Copy the file PizzaOrder.java (see Code Listing 3.1) from the Student CD or as directed

Task #1 The if Statement, Comparing Strings, and Flags

1. Copy the file PizzaOrder.java (see Code Listing 3.1) from the Student CD or as directed by your instructor.

2. Compile and run PizzaOrder.java. You will be able to make selections, but at this point, you will always get a Hand-tossed pizza at a base cost of $12.99 no matter what you select, but you will be able to choose toppings, and they should add into the price correctly. You will also notice that the output does not look like money. So we need to edit PizzaOrder.java to complete the program so that it works correctly.

3. Construct a simple if statement. The condition will compare the String input by the user as his or her first name with the first names of the owners, Mike and Diane. Be sure that the comparison is not case sensitive.

4. If the user has either first name, set the discount flag to true. This will not affect the price at this point yet.

Task #2 The if-else-if Statement

1. Write an if-else-if statement that lets the computer choose which statements to execute by the user input size (10, 12, 14, or 16). For each option, the cost needs to be set to the appropriate amount.

2. The default else of the above if-else-if statement should print a statement that the user input was not one of the choices, so a 12 inch pizza will be made. It should also set the pizza size to 12 and the cost to 12.99.

3. Compile, debug, and run. You should now be able to get correct output for the pizza size and price (it will still have Hand-tossed crust, the output wont look like money, and no discount will be applied yet). Run your program multiple times ordering a 10, 12, 14, 16, and 17 inch pizza.

Task #3 The switch Statement

1. Write a switch statement that compares the users choice with the appropriate characters (make sure that both capital letters and small letters will work).

2. Each case will assign the appropriate string indicating crust type to the crust variable.

3. The default case will print a statement that the user input was not one of the choices, so a Hand-tossed crust will be made.

4. Compile, debug, and run. You should now be able to get crust types other than Hand-tossed. Run your program multiple times to make sure all cases of the switch statement operate correctly.

Task #4 Using a Flag as a Condition

1. Write an if statement that uses the flag as the condition. Remember that the flag is a boolean variable, therefore is true or false. It does not have to be compared to anything.

2. The body of the if statement should contain two statements: a. A statement that prints a message indicating that the user is eligible for a $2.00 discount. b. A statement that reduces the variable cost by

3.Compile, debug, and run. Test your program using the owners names (both capitalized and not) as well as a different name. The discount should be displayed correctly at this time.

Task #5 Formatting Output

1. Edit the appropriate lines in the main method so that any monetary output has 2 decimal places.

2. Compile, debug, and run. Your output should be completely correct at this time, and numeric output should look like money.

Code Listing 3.1 (PizzaOrder.java)

1 import java.util.Scanner; // Needed for the Scanner class 2 3 /** 4 This program allows the user to order a pizza. 5 */ 6 7 public class PizzaOrder 8 { 9 public static void main (String[] args) 10 { 11 // Create a Scanner object to read input. 12 Scanner keyboard = new Scanner (System.in); 13 14 String firstName; // User's first name 15 boolean discount = false; // Flag for discount 16 int inches; // Size of the pizza 17 char crustType; // For type of crust 18 String crust = "Hand-tossed"; // Name of crust 19 double cost = 12.99; // Cost of the pizza 20 final double TAX_RATE = .08; // Sales tax rate 21 double tax; // Amount of tax 22 char choice; // User's choice 23 String input; // User input 24 String toppings = "Cheese "; // List of toppings 25 int numberOfToppings = 0; // Number of toppings 26 27 // Prompt user and get first name. 28 System.out.println("Welcome to Mike and " + 29 "Diane's Pizza"); 30 System.out.print("Enter your first name: "); 31 firstName = keyboard.nextLine(); 32 33 // Determine if user is eligible for discount by 34 // having the same first name as one of the owners. 35 // ADD LINES HERE FOR TASK #1 36 37 // Prompt user and get pizza size choice. 38 System.out.println("Pizza Size (inches) Cost"); 39 System.out.println(" 10 $10.99"); 40 System.out.println(" 12 $12.99"); 41 System.out.println(" 14 $14.99"); 42 System.out.println(" 16 $16.99"); 43 System.out.println("What size pizza " + 44 "would you like?"); 45 System.out.print("10, 12, 14, or 16 " + 46 "(enter the number only): "); 47 inches = keyboard.nextInt(); 48 49 // Set price and size of pizza ordered. 50 // ADD LINES HERE FOR TASK #2 51 52 // Consume the remaining newline character. 53 keyboard.nextLine(); 54 55 // Prompt user and get crust choice. 56 System.out.println("What type of crust " + 57 "do you want? "); 58 System.out.print("(H)Hand-tossed, " + 59 "(T) Thin-crust, or " + 60 "(D) Deep-dish " + 61 "(enter H, T, or D): "); 62 input = keyboard.nextLine(); 63 crustType = input.charAt(0); 64 65 // Set user's crust choice on pizza ordered. 66 // ADD LINES FOR TASK #3 67 68 // Prompt user and get topping choices one at a time. 69 System.out.println("All pizzas come with cheese."); 70 System.out.println("Additional toppings are " + 71 "$1.25 each, choose from:"); 72 System.out.println("Pepperoni, Sausage, " + 73 "Onion, Mushroom"); 74 75 // If topping is desired, 76 // add to topping list and number of toppings 77 System.out.print("Do you want Pepperoni? (Y/N): "); 78 input = keyboard.nextLine(); 79 choice = input.charAt(0); 80 if (choice == 'Y' || choice == 'y') 81 { 82 numberOfToppings += 1; 83 toppings = toppings + "Pepperoni "; 84 } 85 System.out.print("Do you want Sausage? (Y/N): "); 86 input = keyboard.nextLine(); 87 choice = input.charAt(0); 88 if (choice == 'Y' || choice == 'y') 89 { 90 numberOfToppings += 1; 91 toppings = toppings + "Sausage "; 92 } 93 System.out.print("Do you want Onion? (Y/N): "); 94 input = keyboard.nextLine(); 95 choice = input.charAt(0); 96 if (choice == 'Y' || choice == 'y') 97 { 98 numberOfToppings += 1; 99 toppings = toppings + "Onion "; 100 } 101 System.out.print("Do you want Mushroom? (Y/N): "); 102 input = keyboard.nextLine(); 103 choice = input.charAt(0); 104 if (choice == 'Y' || choice == 'y') 105 { 106 numberOfToppings += 1; 107 toppings = toppings + "Mushroom "; 108 } 109 110 // Add additional toppings cost to cost of pizza. 111 cost = cost + (1.25 * numberOfToppings); 112 113 // Display order confirmation. 114 System.out.println(); 115 System.out.println("Your order is as follows: "); 116 System.out.println(inches + " inch pizza"); 117 System.out.println(crust + " crust"); 118 System.out.println(toppings); 119 120 // Apply discount if user is eligible. 121 // ADD LINES FOR TASK #4 HERE 122 123 // EDIT PROGRAM FOR TASK #5 124 // SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES 125 System.out.printf("The cost of your order " + 126 "is: $%f ", cost); 127 128 // Calculate and display tax and total cost. 129 tax = cost * TAX_RATE; 130 System.out.printf("The tax is: $%f ", tax); 131 System.out.printf("The total due is: $%f ", 132 (tax + cost)); 133 134 System.out.println("Your order will be ready " + 135 "for pickup in 30 minutes."); 136 } 137 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Big Data Interview 751 Comprehensive Questions And Expert Answers

Authors: Mr Bhanu Pratap Mahato

1st Edition

B0CLNT3NVD, 979-8865047216

More Books

Students also viewed these Databases questions

Question

An overview of financial market and institutions of Bangladesh.

Answered: 1 week ago

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago

Question

orderliness, patience and seeing a task through;

Answered: 1 week ago

Question

well defined status and roles (class distinctions);

Answered: 1 week ago