1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pickupchik [31]
3 years ago
15

Up to this point, all the programs you have written had a sequence structure. This means that all statements are executed in seq

uence, one after another. Sometimes we need to let the computer make decisions, based on the data. A decision structure allows the computer to decide which statement to execute.
In order to have the computer make a decision, it needs to do a comparison. So we will work with writing boolean expressions. boolean expressions use relational operators and logical operators to create a condition that can be evaluated as true or false.

Once we have a condition, we can conditionally execute statements. This means that there are statements in the program that may or may not be executed, depending on the condition.

We can also chain conditional statements together to allow the computer to choose from several courses of action. We will explore this using nested if-else statements as well as a switch statement.

In this lab, we will be editing a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. The user will also receive a $2.00 discount if his or her name is Mike or Diane.

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

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

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.

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.

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

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.

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.

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 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 The switch Statement

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).

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

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.

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

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.

The body of the if statement should contain two statements:

A statement that prints a message indicating that the user is eligible for a $2.00 discount.

A statement that reduces the variable cost by 2.

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

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

Compile, debug, and run. Your output should be completely correct at this time, and numeric output should look like money.
Computers and Technology
1 answer:
schepotkina [342]3 years ago
8 0

Answer:

File: PizzaOrder.java

import java.util.Scanner;

public class PizzaOrder

{

public static void main(String[] args)

{

 Scanner keyboard = new Scanner(System.in);

 String firstName;

 boolean discount = false;

 int inches;

 char crustType;

 String crust; // MODIFIED

 double cost; // MODIFIED

 final double TAX_RATE = 0.08;

 double tax;

 char choice;

 String input;

 String toppings = "Cheese ";

 int numberOfToppings = 0;

 

 System.out.println("Welcome to Mike and Diane's Pizza");

 

 System.out.print("Enter your first name: ");

 firstName = keyboard.nextLine();

 

 // ADD LINES HERE FOR TASK #1

 if(firstName.equalsIgnoreCase("Mike") || firstName.equalsIgnoreCase("Diane"))

  discount = true;

 

 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();

 

 // ADD LINES HERE FOR TASK #2

 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("The user input was not one of the choices, so a 12 inch pizza will be made.");

  cost = 12.99;

 }  

 keyboard.nextLine();

 

 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);

 

 // ADD LINES FOR TASK #3

 switch(crustType)

 {

  case 'H':

  case 'h':

   crust = "Hand-tossed";

   break;

  case 'T':

  case 't':

   crust = "Thin-crust";

   break;

  case 'D':

  case 'd':

   crust = "Deep-dish";

   break;

  default:

   System.out.println("The user input was not one of the choices, so a Hand-tossed crust will be made.");

   crust = "Hand-tossed";

 }

 

 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");

 

 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 ";

 }

   

 cost = cost + (1.25 * numberOfToppings);

 

 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);

 

 // ADD LINES FOR TASK #4 HERE

 if(discount)

  cost = cost - 2.0;

 

 // EDIT PROGRAM FOR TASK #5

 // SO ALL MONEY OUTPUT APPEARS WITH 2 DECIMAL PLACES

 System.out.printf("The cost of your order " + "is: $%.2f\n", cost);// MODIFIED

 tax = cost * TAX_RATE;

 System.out.printf("The tax is: $%.2f\n", tax);// MODIFIED

 System.out.printf("The total due is: $%.2f\n", (tax + cost));// MODIFIED

 System.out.println("Your order will be ready for pickup in 30 minutes.");

}

}

Explanation:

You might be interested in
How to convert meters to centimeters in c programming​
mote1985 [20]

Answer:

Input

Enter length in centimeter = 1000

Output

Length in meter = 10 m

Length in kilometer = 0.01 km

Explanation:

hope this helps

5 0
3 years ago
Using the notation exemplified in following question , list a set of tables and attributes (and identify keys) to represent the
Paha777 [63]

Answer:

Entities are - Students, CourseList, Advisor and CourseSelection.

Explanation:

The database is structure is designed using Crow Foot Database Notation as attached

6 0
3 years ago
What is the best method to avoid getting spyware on a machine
monitta

Answer: The best method to avoid getting spyware on a user machine is to download software only from trusted websites. And depending on the machine it might say that "This download you are about to install might have spyware or malware and it might infect your laptop" "Do you wish to continue?"

3 0
2 years ago
What are the advantages of using the internet as theinfrastructure for electronic commerce and electronicbusiness?
Mrac [35]

Answer and Explanation:

E-commerce and e-business is a major business of the present time using the internet. It is basically defined as the online selling of goods or making any business online. Internet is the basic requirement for the e-commerce or e-business as

  • it helps in providing the internet connectivity so that the e-business can be displayed online and users can buy goods or interact with seller regarding the business.
  • Due to internet service users get to know about the online business and thus the business attains economic growth and benefit.

6 0
4 years ago
"The _____ of the Open Systems Interconnection (OSI) model generates the receiver’s address and ensures the integrity of message
aleksklad [387]

Answer:

The transport layer

Explanation: Layer 4, is the transport layer of the Open System Interconnection (OSI), that handles message transfer of data between end systems or hosts and ensures complete packets transfer.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Why are computer programs so much longer now than they were in the late 1980?
    8·1 answer
  • he specific gravity of gold is 19.3. Write a MATLAB program that will ask the user to input the mass of a cube of solid gold in
    15·1 answer
  • Which of the following data recording procedures is best used for behaviors that have a clear ending and beginning, do not occur
    12·1 answer
  • We already know that we can create a lunar lander application of the pipe-and-filter architecture style from three independent J
    6·1 answer
  • Is it possible to have a deadlock involving only oneprocess? Explain your answer.
    11·1 answer
  • A workstation with a static IP (Internet Protocol) address can print and authenticate to a server, but cannot browse to www.comp
    14·1 answer
  • Consider the following class definition.
    7·1 answer
  • HELP ME PLEASE ASAP
    6·1 answer
  • To excel at these professions, you need to be able to combine an eye for elegant design with a mind that delights in efficient o
    8·1 answer
  • Edhesive 7.6 lesson practice python
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!