This is a Java related question: Write a complete Java program in a source file
ID: 3782694 • Letter: T
Question
This is a Java related question:
Write a complete Java program in a source file to be named Assignment2.java. The program prints out the following menu to a user, using ’ ’ (tabs) and ’ ’ (newline), and print or println methods of System.out: Welcome to the In-N-Out Burger menu: ------------------------------------------------------ Hamburger $2.75 Cheeseburger $3.25 French Fries $2.50 Shake & Beverage: $1.50 Then prompts the user: How many hamburger(s) would you like to buy? and read in the number. Then prompts the user: How many cheeseburger(s) would you like to buy? and read in the number. Then prompts the user: How many French fries would you like to buy? and read in the number. Then prompts the user: How many drinks would you like to buy? and read in the number. Then the program computes and prints the following: • The total cost for the hamburger(s) • The total cost for the cheeseburger(s) • The total cost for fries • The total cost for drink(s) • Which item had the highest total cost • The total cost for the order • The total number of hamburgers, cheeseburgers, French fries, and drinks ordered
Write a complete Java program in a source file to be named Assignments 2. java, The program prints out the following menu to a user, using 'lt' tabs) and 'In' (newline) and print or println methods of System. out: Welcome to the In N-Out Burger menu: Hamburger $2.75 Cheeseburger $3.25 French Fries $2.50 Shake & Beverage: $1.50 Then prompts the user: How many hamburger(s) would you like to buy and read in the number. Then prompts the user How many cheeseburger (s) would you like to buy and read in the number. Then prompts the user How many French fries would you like to buy? and read in the number. Then prompts the user How many drinks would you like to buy? and read in the number. Then the program computes and prints the following The total cost for the hamburger(s) The total cost for the cheeseburger The total cost for fries The total cost for drink (s) Which item had the highest total cost The total cost for the order The total number of hamburgers, cheeseburgers, French fries, and drinks orderedExplanation / Answer
Assignment2.java
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args) {
//Declaring constants
final double HAMBURGER=2.75,CHEESEBURGER=3.25,FRENCHFRIES=2.50,SHAKE=1.50;
//Declaring variables
double tot_cost_hamburgers,tot_cost_cheeseburgers,tot_cost_frenchfries,tot_cost_drinks;
//Scanner Object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Displaying the menu
System.out.println("Welcome to the In-N-Out Burger menu :");
System.out.println("----------------------------------");
System.out.println("Hamburger $2.75");
System.out.println("Cheeseburger $3.25");
System.out.println("French Fries $2.50");
System.out.println("Shake & Beverage $1.50");
//Getting the inputs entered by the user
System.out.print(" How many Hamburger(s) would you like to buy?");
int no_of_hamburgers=sc.nextInt();
System.out.print(" How many Cheeseburger(s) would you like to buy?");
int no_of_cheeseburgers=sc.nextInt();
System.out.print(" How many French Fries(s) would you like to buy?");
int no_of_frenchfries=sc.nextInt();
System.out.print(" How many Drink(s) would you like to buy?");
int no_of_drinks=sc.nextInt();
//Calculating the total cost of hamburgers
tot_cost_hamburgers=no_of_hamburgers*HAMBURGER;
//Calculating the total cost of cheeseburgers
tot_cost_cheeseburgers=no_of_cheeseburgers*CHEESEBURGER;
//Calculating the total cost of French Fries
tot_cost_frenchfries=no_of_frenchfries*FRENCHFRIES;
//Calculating the total cost of drinks
tot_cost_drinks=no_of_drinks*SHAKE;
//Displaying the total cost of each item
System.out.println(" Total cost for the Hamburger(s) :"+tot_cost_hamburgers);
System.out.println("Total cost for the Cheeseburger(s) :"+tot_cost_cheeseburgers);
System.out.println("Total cost for Fries(s) :"+tot_cost_frenchfries);
System.out.println("Total cost for drink(s) :"+tot_cost_drinks);
//Creating an array and populate the total cos of each item into an array
double cost[]=new double[4];
String items[]=new String[4];
cost[0]=tot_cost_hamburgers;
cost[1]=tot_cost_cheeseburgers;
cost[2]=tot_cost_frenchfries;
cost[3]=tot_cost_drinks;
items[0]="Hamburger(s)";
items[1]="CheeseBurger(s)";
items[2]="French Fries";
items[3]="Drink(s)";
double max_cost=cost[0];
String item_name = null;
//This for loop find which item has the highest cost
for(int i=0;i<cost.length;i++)
{
if(max_cost<cost[i])
{
max_cost=cost[i];
item_name=items[i];
}
}
//Displaying which item has the highest total cost
System.out.println(item_name+" has the highest total cost of "+max_cost);
double total_cost_for_order=0.0;
//Calculating the total cost of all orders
for(int i=0;i<cost.length;i++)
{
total_cost_for_order+=cost[i];
}
//Displaying total no of items
System.out.println("Total No of Hamburgers,CheeseBurgers,French Fries,Drinks ordered :"+(no_of_hamburgers+no_of_cheeseburgers+no_of_frenchfries+no_of_drinks));
}
}
________________________
Output:
Welcome to the In-N-Out Burger menu :
----------------------------------
Hamburger $2.75
Cheeseburger $3.25
French Fries $2.50
Shake & Beverage $1.50
How many Hamburger(s) would you like to buy?5
How many Cheeseburger(s) would you like to buy?6
How many French Fries(s) would you like to buy?8
How many Drink(s) would you like to buy?9
Total cost for the Hamburger(s) :13.75
Total cost for the Cheeseburger(s) :19.5
Total cost for Fries(s) :20.0
Total cost for drink(s) :13.5
French Fries has the highest total cost
Total No of Hamburgers,CheeseBurgers,French Fries,Drinks ordered :28
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.