Task #1 The if Statement, Comparing Strings, and Flags 1. Copy the file PizzaOrd
ID: 3628075 • Letter: T
Question
Task #1 The if Statement, Comparing Strings, and Flags1. Copy the file PizzaOrder.java (see code listing 3.1) from www.aw.com/cssup- port 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/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 size to 12 and the cost to 12.99.
3. Compile, debug, and run. You should now be able to get correct output for size and price (it will still have Hand-tossed crust, the output won’t 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 Switch Statement
1. Write a switch statement that compares the user’s 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 com- pared 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 2.
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 cor- rectly at this time.
Task #5 Formatting Numbers
1. Add an import statement to use the DecimalFormat class as indicated above the class declaration.
2. Create a DecimalFormat object that always shows 2 decimal places.
3. Edit the appropriate lines in the main method so that any monetary output has 2 decimal places.
4. 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)
import java.util.Scanner;
import java.text.DecimalFormat;
/** This program allows the user to order a pizza
*/
public class PizzaOrder {
public static void main (String [] args) {
//TASK #5 Create a DecimalFormat object with 2 decimal //places
//Create a Scanner object to read input
Scanner keyboard = new Scanner
String firstName; boolean discount = false;
int inches; char crustType; String crust = "Hand-tossed"; double cost = 12.99; final double TAX_RATE = .08; double tax; char choice; String input; String toppings = "Cheese "; int numberOfToppings = 0;
(System.in);
//user’s first name //flag, true if user is //eligible for discount //size of the pizza //code for type of crust //name of crust //cost of the pizza //sales tax rate //amount of tax //user’s choice //user input //list of toppings //number of toppings
//prompt user and get first name System.out.println("Welcome to Mike and Diane’s Pizza"); System.out.print("Enter your first name: "); firstName = keyboard.nextLine();
//determine if user is eligible for discount by //having the same first name as one of the owners //ADD LINES HERE FOR TASK #1
//prompt user and get pizza size choice System.out.println("Pizza Size (inches) Cost");
System.out.println(" 10 System.out.println(" 12 System.out.println(" 14 System.out.println(" 16 System.out.println("What size pizza would you like?"); System.out.print(
"10, 12, 14, or 16 (enter the number only): "); inches = keyboard.nextInt();
//set price and size of pizza ordered //ADD LINES HERE FOR TASK #2
//consume the remaining newline character keyboard.nextLine();
//prompt user and get crust choice System.out.println("What type of crust do you want? "); System.out.print("(H)Hand-tossed, (T) Thin-crust, or " +
"(D) Deep-dish (enter H, T, or D): "); input = keyboard.nextLine(); crustType = input.charAt(0);
//set user’s crust choice on pizza ordered //ADD LINES FOR TASK #3
//prompt user and get topping choices one at a time
System.out.println("All pizzas come with cheese."); System.out.println("Additional toppings are $1.25 each,"
+ " choose from"); System.out.println("Pepperoni, Sausage, Onion, Mushroom");
$10.99”); $12.99"); $14.99"); $16.99");
//if topping is desired, //add to topping list and number of toppings System.out.print("Do you want Pepperoni? (Y/N): "); input = keyboard.nextLine(); choice = input.charAt(0); if (choice == ‘Y’ || choice == ‘y’) {
numberOfToppings += 1; toppings = toppings + "Pepperoni ";
} System.out.print("Do you want Sausage? (Y/N): "); input = keyboard.nextLine(); choice = input.charAt(0); if (choice == ‘Y’ || choice == ‘y’) {
numberOfToppings += 1; toppings = toppings + "Sausage ";
} System.out.print("Do you want Onion? (Y/N): "); input = keyboard.nextLine(); choice = input.charAt(0); if (choice == ‘Y’ || choice == ‘y’) {
numberOfToppings += 1; toppings = toppings + "Onion ";
} System.out.print("Do you want Mushroom? (Y/N): "); input = keyboard.nextLine(); choice = input.charAt(0); if (choice == ‘Y’ || choice == ‘y’) {
numberOfToppings += 1; toppings = toppings + "Mushroom ";
}
//add additional toppings cost to cost of pizza
}
cost = cost + (1.25*numberOfToppings);
//display order confirmation System.out.println(); System.out.println("Your order is as follows: "); System.out.println(inches + " inch pizza"); System.out.println(crust + " crust"); System.out.println(toppings);
//apply discount if user is eligible //ADD LINES FOR TASK #4 HERE
//EDIT PROGRAM FOR TASK #5 //SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
System.out.println("The cost of your order is: $" + cost);
//calculate and display tax and total cost tax = cost * TAX_RATE; System.out.println("The tax is: $" + tax); System.out.println("The total due is: $" + (tax+cost));
System.out.println( "Your order will be ready for pickup in 30 minutes.");
Explanation / Answer
please rate - thanks
import java.util.Scanner;
import java.text.DecimalFormat;
/**
This program allows the user to order a pizza
*/
public class PizzaOrder
{
public static void main (String [] args)
{
//TASK #5 Create a DecimalFormat object with 2 decimal
DecimalFormat decimal = new DecimalFormat("#0.00");
//places
//Create a Scanner object to read input
Scanner keyboard = new Scanner (System.in);
String firstName; //user’s first name
boolean discount = false; //flag, true if user is
//eligible for discount
int inches; //size of the pizza
char crustType; //code for type of crust
String crust = "Hand-tossed"; //name of crust
double cost = 12.99; //cost of the pizza
final double TAX_RATE = .08; //sales tax rate
double tax; //amount of tax
char choice; //user’s choice
String input; //user input
String toppings = "Cheese "; //list of toppings
int numberOfToppings = 0; //number of toppings
//prompt user and get first name
System.out.println("Welcome to Mike and Diane’s Pizza");
System.out.print("Enter your first name: ");
firstName = keyboard.nextLine();
//determine if user is eligible for discount by
//having the same first name as one of the owners
//ADD LINES HERE FOR TASK #1
if(firstName.compareToIgnoreCase("Mike")==0|| firstName.compareToIgnoreCase("Diane")==0)
discount=true;
//prompt user and get pizza size choice
System.out.println("Pizza Size (inches) Cost");
System.out.println(" 10 $10.99");
System.out.println(" 12 $12.99");
System.out.println(" 14 $14.99");
System.out.println(" 16 $16.99");
System.out.println("What size pizza would you like?");
System.out.print("10, 12, 14, or 16 (enter the number only): ");
inches = keyboard.nextInt();
//set price and size of pizza ordered
if(inches==10)
cost=10.99;
else if(inches==12)
cost=12.99;
else if(inches==14)
cost=14.99;
else if(inches==16)
cost=16.99;
else
{System.out.println(inches+" is not a valid size-you will recieve a 12" pizza");
inches=12;
cost=12.99;
}
//ADD LINES HERE FOR TASK #2
//consume the remaining newline character
keyboard.nextLine();
//prompt user and get crust choice
System.out.println("What type of crust do you want? ");
System.out.print("(H)Hand-tossed, (T) Thin-crust, or " +"(D) Deep-dish (enter H, T, or D): ");
input = keyboard.nextLine();
crustType = input.charAt(0);
//set user’s crust choice on pizza ordered
//ADD LINES FOR TASK #3
switch(crustType)
{default: System.out.println(crustType+" is not a choice-you will get Hand-tossed");
crustType='h';
case 'H':
case 'h': crust="Hand-tossed";
break;
case 'T':
case 't': crust="Thin-crust";
break;
case 'D':
case 'd': crust="Deep-dish ";
break;
}
//prompt user and get topping choices one at a time
System.out.println("All pizzas come with cheese.");
System.out.println("Additional toppings are $1.25 each," + " choose from");
System.out.println("Pepperoni, Sausage, Onion, Mushroom");
//if topping is desired,
//add to topping list and number of toppings
System.out.print("Do you want Pepperoni? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y')
{
numberOfToppings += 1;
toppings = toppings + "Pepperoni ";
}
System.out.print("Do you want Sausage? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y')
{
numberOfToppings += 1;
toppings = toppings + "Sausage ";
}
System.out.print("Do you want Onion? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y')
{
numberOfToppings += 1;
toppings = toppings + "Onion ";
}
System.out.print("Do you want Mushroom? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y')
{
numberOfToppings += 1;
toppings = toppings + "Mushroom ";
}
//add additional toppings cost to cost of pizza
cost = cost + (1.25*numberOfToppings);
//display order confirmation
System.out.println();
System.out.println("Your order is as follows: ");
System.out.println(inches + " inch pizza");
System.out.println(crust + " crust");
System.out.println(toppings);
//apply discount if user is eligible
//ADD LINES FOR TASK #4 HERE
if(discount)
{System.out.println("You are eligible for a $2 discount");
cost-=2;
}
//EDIT PROGRAM FOR TASK #5
//SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES
System.out.println("The cost of your order is: $" +decimal.format(cost));
//calculate and display tax and total cost
tax = cost * TAX_RATE;
System.out.println("The tax is: $" + decimal.format(tax));
System.out.println("The total due is: $" + decimal.format(tax+cost));
System.out.println("Your order will be ready for pickup in 30 minutes.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.