111/p2/pizzaCalc.html and 111/p2/pizzaCalc.js The following is an exercise in Cl
ID: 3824839 • Letter: 1
Question
111/p2/pizzaCalc.html and 111/p2/pizzaCalc.js
The following is an exercise in Client-Side JavaScript: you will
need both an .html file and a .js file.
Create a web page named pizzaCalc.html based on the hello- world.html file from Project 1 (see example, below).
Create a second file pizzaCalc.js that will contain all of your JavaScript code, and connect pizzaCalc.html to pizzaCalc.js.
5
The .js file must include the use strict directive.
Write and test a function that accepts two arguments (the pizza price and pizza diameter), and returns a number ( the cost per square inch of the pizza). Store the function definition in the .js file.
Note that the function does not call prompt, alert or console.log-- it just accepts arguments and returns a value.
Following the function definition, write the JavaScript code to prompt and read the cost of a pizza, and the diameter of a pizza.
Call the function you wrote to calculate the cost per square inch of the pizza, and output the following information in a div element on the web page.
Explanation / Answer
import java.util.Scanner; import java.text.DecimalFormat; public class PizzaOrder { public static void main (String [] args) { DecimalFormat formatter = new DecimalFormat("#0.00"); Scanner keyboard = new Scanner (System.in); 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.out.println("Welcome to Mike and Diane's Pizza"); System.out.print("Enter your first name: "); firstName = keyboard.nextLine(); if (firstName == "Mike" || firstName == "mike") { discount = true; } if (firstName =="Diane" || firstName == "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(); if (inches == 10) { cost = 10.99; } if (inches == 12) { cost = 12.99; } if (inches == 14) { cost = 14.99; } if (inches == 16) { cost = 16.99; } else System.out.println("You have selected incorrect pizza size."); System.out.println("You have been assigned 12 inch pizza by default."); 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); switch (crustType) { case 'H': case 'h': System.out.println("You have selected Hand-tossed crust."); break; case 'T': case 't': System.out.println("You have selected Thin-crust."); break; case 'D': case 'd': System.out.println("You have selected Deep-dish crust."); break; default: System.out.println("You have selected invalid type of crust."); System.out.println("You have been assigned Hand-tossed crust by default."); break; } 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); if (discount) { System.out.println("You are eligible for $2.00 discount."); cost -= 2.00; } System.out.println("The cost of your order is: $" + formatter.format(cost)); tax = cost * TAX_RATE; System.out.println("The tax is: $" + formatter.format(tax)); System.out.println("The total due is: $" + formatter.format((tax+cost))); System.out.println("Your order will be ready for pickup in 30 minutes."); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.