Question

In: Computer Science

Hello, I Have create this code and Tried to add do while loop but it gives...

Hello,

I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod

So cloud you please help me to do ( do while ) in this code.

// Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then give total amount due with tax included.


import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); //initialise a Scanner object, and get all the information about items quantity and toppings ordered and the amount of money tendered as mentioned in questions.

string answar;

do {

System.out.print("\nEnter quantity of Small pizzas: ");// allows you to enter the quantuty of the samll pizza

int numSmallPizzas = sc.nextInt();//initialise a Scanner object for small pizza

System.out.print("Enter quantity of Small pizza toppings: "); // allows you to enter the topoings for small pizza.

int numSmallPizzaToppings = sc.nextInt();// allows you to enter the small toppings.

System.out.print("Enter quantity of Large pizzas: ");// allows you to enter the quantity of large pizza.

int numLargePizzas = sc.nextInt();//initialise a Scanner object for lage pizza.

System.out.print("Enter quantity of Large pizza toppings: ");// allows you to enter the Toppings for large pizza.

int numLargePizzaToppings = sc.nextInt();//Allows you to enter the toppings for large pizza.

System.out.print("Enter quantity of Soft Drinks: "); // allows you to enter the quantity of soft drinks.

int numSoftDrinks = sc.nextInt(); //initialise a Scanner object for sum of Soft drinks

System.out.print("Enter Amount tendered: ");

double amountTendered = sc.nextDouble();//initialise a Scanner object for totalAmount.

//stored the rates of all items

double smallPizzasRate = 8.99; // rate of small pizza

double smallPizzaToppingsRate = 1.5; // rate of the small pizza toppings

double largePizzasRate = 12.99; //rate of large pizza

double largePizzaToppingsRate = 2.5; // rate of the large pizza toppings

double softDrinksRate = 1.5; // rate of soft drinks

//find the amount for all items

double amountSmallPizzas = numSmallPizzas*smallPizzasRate; // calculates the amount of the small pizza.

double amountSmallPizzaToppings = numSmallPizzaToppings*smallPizzaToppingsRate; // calculates the amount of Toppings for small pizza.

double amountLargePizzas = numLargePizzas*largePizzasRate;// calculates the amount of large.

double amountLargePizzaToppings = numLargePizzaToppings*largePizzaToppingsRate; //calculates the amount of toppinf for large pizza

double amountSoftDrinks = numSoftDrinks*softDrinksRate;// calcualtes the amount for soft drinks intered.

//find totalAmount,salesTax, amountDueWithTax and change

double totalAmount = amountSoftDrinks + amountLargePizzaToppings + amountLargePizzas + amountSmallPizzas + amountSmallPizzaToppings;

double salesTax = totalAmount*0.06;

double amountDueWithTax = totalAmount + salesTax;

double change = amountTendered - amountDueWithTax;

//prints the receipt

System.out.println("\nWelcome to Yassir Pizza!"); //prints the title of the busniess

System.out.println("Address 111 Yassir Road Largo MD 20783 "); // print the address of the busniess

System.out.println("busniess phone number: (240)241-3462"); // prrints Busniess phone number

System.out.println("Your Order Was");

System.out.printf("%d Small pizzas @ %.4f each: $%.4f\n",numSmallPizzas,smallPizzasRate,amountSmallPizzas);

System.out.printf("%d Small pizza toppings @ %.4f each: $%.4f\n",numSmallPizzaToppings,smallPizzaToppingsRate,amountSmallPizzaToppings);

// prints out the total amount of small pizza and its toppings.

System.out.printf("%d Large pizzas @ %.4f each: $%.4f\n",numLargePizzas,largePizzasRate,amountLargePizzas);

System.out.printf("%d Large Pizza Toppings @ %.4f each: $%.4f\n",numLargePizzaToppings,smallPizzaToppingsRate,amountLargePizzaToppings);

// prints out the total amount of large pizza and its toppings.

System.out.printf("%d Soft Drinks @ %.4f each: $%.4f\n",numSoftDrinks,softDrinksRate,amountSoftDrinks);

// prints out the total amount of Softdrinks.

System.out.printf("\nTotal cost: $%.4f\n", totalAmount); // prints totalAmount due without tax

System.out.printf("Sales tax amount is: $%.4f\n", salesTax); // prints the salesTax

System.out.printf("Total amount due: $%.4f\n", amountDueWithTax);// prints the total amount due including tax.

System.out.printf("Amount tendered: $%.3f\n", amountTendered);// prints the amount given to casherir.

System.out.printf("Change: $%.4f\n", change);// prints the change for the receipt

System.out.println("\nThank you! \nYour order is complete!");

//print the complete statment.

if (totalAmount < amountTendered) {

System.out.println("\nThank you! \nYour order is complete!");

}

else

System.out.println("\nYour money is not enough");

System.out.println("do you want to add anything else? Yes or No");

answar = sc.next();

}

while (answar.equals("Yes"));

}

}

Solutions

Expert Solution

Solution :

Java is a case sensitive language. So there is a difference between "Hello" and "hello".

You are getting the error because you have written"string" instead of "String" in the statement : "string answar;"

Following is the corrected code :

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); //initialise a Scanner object, and get all the information about items quantity and toppings ordered and the amount of money tendered as mentioned in questions.

String answar;

do {

System.out.print("\nEnter quantity of Small pizzas: ");// allows you to enter the quantuty of the samll pizza

int numSmallPizzas = sc.nextInt();//initialise a Scanner object for small pizza

System.out.print("Enter quantity of Small pizza toppings: "); // allows you to enter the topoings for small pizza.

int numSmallPizzaToppings = sc.nextInt();// allows you to enter the small toppings.

System.out.print("Enter quantity of Large pizzas: ");// allows you to enter the quantity of large pizza.

int numLargePizzas = sc.nextInt();//initialise a Scanner object for lage pizza.

System.out.print("Enter quantity of Large pizza toppings: ");// allows you to enter the Toppings for large pizza.

int numLargePizzaToppings = sc.nextInt();//Allows you to enter the toppings for large pizza.

System.out.print("Enter quantity of Soft Drinks: "); // allows you to enter the quantity of soft drinks.

int numSoftDrinks = sc.nextInt(); //initialise a Scanner object for sum of Soft drinks

System.out.print("Enter Amount tendered: ");

double amountTendered = sc.nextDouble();//initialise a Scanner object for totalAmount.

//stored the rates of all items

double smallPizzasRate = 8.99; // rate of small pizza

double smallPizzaToppingsRate = 1.5; // rate of the small pizza toppings

double largePizzasRate = 12.99; //rate of large pizza

double largePizzaToppingsRate = 2.5; // rate of the large pizza toppings

double softDrinksRate = 1.5; // rate of soft drinks

//find the amount for all items

double amountSmallPizzas = numSmallPizzas*smallPizzasRate; // calculates the amount of the small pizza.

double amountSmallPizzaToppings = numSmallPizzaToppings*smallPizzaToppingsRate; // calculates the amount of Toppings for small pizza.

double amountLargePizzas = numLargePizzas*largePizzasRate;// calculates the amount of large.

double amountLargePizzaToppings = numLargePizzaToppings*largePizzaToppingsRate; //calculates the amount of toppinf for large pizza

double amountSoftDrinks = numSoftDrinks*softDrinksRate;// calcualtes the amount for soft drinks intered.

//find totalAmount,salesTax, amountDueWithTax and change

double totalAmount = amountSoftDrinks + amountLargePizzaToppings + amountLargePizzas + amountSmallPizzas + amountSmallPizzaToppings;

double salesTax = totalAmount*0.06;

double amountDueWithTax = totalAmount + salesTax;

double change = amountTendered - amountDueWithTax;

//prints the receipt

System.out.println("\nWelcome to Yassir Pizza!"); //prints the title of the busniess

System.out.println("Address 111 Yassir Road Largo MD 20783 "); // print the address of the busniess

System.out.println("busniess phone number: (240)241-3462"); // prrints Busniess phone number

System.out.println("Your Order Was");

System.out.printf("%d Small pizzas @ %.4f each: $%.4f\n",numSmallPizzas,smallPizzasRate,amountSmallPizzas);

System.out.printf("%d Small pizza toppings @ %.4f each: $%.4f\n",numSmallPizzaToppings,smallPizzaToppingsRate,amountSmallPizzaToppings);

// prints out the total amount of small pizza and its toppings.

System.out.printf("%d Large pizzas @ %.4f each: $%.4f\n",numLargePizzas,largePizzasRate,amountLargePizzas);

System.out.printf("%d Large Pizza Toppings @ %.4f each: $%.4f\n",numLargePizzaToppings,smallPizzaToppingsRate,amountLargePizzaToppings);

// prints out the total amount of large pizza and its toppings.

System.out.printf("%d Soft Drinks @ %.4f each: $%.4f\n",numSoftDrinks,softDrinksRate,amountSoftDrinks);

// prints out the total amount of Softdrinks.

System.out.printf("\nTotal cost: $%.4f\n", totalAmount); // prints totalAmount due without tax

System.out.printf("Sales tax amount is: $%.4f\n", salesTax); // prints the salesTax

System.out.printf("Total amount due: $%.4f\n", amountDueWithTax);// prints the total amount due including tax.

System.out.printf("Amount tendered: $%.3f\n", amountTendered);// prints the amount given to casherir.

System.out.printf("Change: $%.4f\n", change);// prints the change for the receipt

System.out.println("\nThank you! \nYour order is complete!");

//print the complete statment.

if (totalAmount < amountTendered) {

System.out.println("\nThank you! \nYour order is complete!");

}

else

System.out.println("\nYour money is not enough");

System.out.println("do you want to add anything else? Yes or No");

answar = sc.next();

}

while (answar.equals("Yes"));

}

}

Code demo for reference :

Output :


Related Solutions

Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
Open Average Test Scores while loop, comment out the while loop and add a for loop...
Open Average Test Scores while loop, comment out the while loop and add a for loop that averages 4 test scores. Code C# While loop code using System; class Program { static void Main() { int count = 0, total = 0, number;    while (count < 3) { Console.Write("Enter a number: "); number = Convert.ToInt32(Console.ReadLine()); total += number; count++; }    double average = total / 3.0; Console.Write("Average = " + average.ToString("####0.00")); } }
Hello, i have this assignment i have tried solving it but been hitting road blocks how...
Hello, i have this assignment i have tried solving it but been hitting road blocks how do i solve this also, i have the texas BA II PLUS Calculator can you tell me how to solve for this using the fomular approach Thank you Questions Based on the following cash flows, which of the following mutually exclusive project would you choose? You require a 15% return on your investment. Year 0 (Initial Investment) 1 2 3 4 Project #1 ($300,000)...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
The following code is inside a do while loop. choice is supposed to be an integer...
The following code is inside a do while loop. choice is supposed to be an integer input by the user. However, when a character is put instead of an integer, the code loops infinitely. Putting break; or exit(); after the catch statements terminates the whole program, which is wrong. How can i fix this? #include <iostream> #include <vector> #include <iomanip> #include<stdlib.h> #include <cctype> #include "MyGrades.h" using namespace std; int main () {    int choice; // user input for menu...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
Hello I have these questions, I would like not so long answers but something that gives...
Hello I have these questions, I would like not so long answers but something that gives me a good idea about the topic. What is a perfectly competitive market? Why are many agricultural products in a perfectly competitive market? What is a price taker? What are barriers to entry in a market? What is a standardized product? Give an example What is a company’s market share? What is free entry and exit? How do you calculate a company’s total revenues?...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT