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

Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
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)...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
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; }
Re-write all with proper code syntax and conventions Add a while loop that continues to ask...
Re-write all with proper code syntax and conventions Add a while loop that continues to ask the age until a blank value is entered, or a user presses cancel. let userage=prompt("enterage"); let userageindays=userage*365; let userageinmonths=userage*12; let useragein hours=userageindays*24 get useragein-minutes=userageinhours*60 let userageinseconds=userageinminutes*60; if userage>=40 { console.log("You are older than 40"; } else { console.log("You are younger than 40"); } switch (true) { case (userage<5): console.log("You are between 0 and 5"); break; case (userage<10): console.log("You are between 5 and 10"); break;...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu...
JAVA PROGRAMMING Hi! I need to create a calculator using do/while loop that calls a menu switch where the user choice the calculation type. In the switch each case calls a method to each type of calculation (addition/subtraction/division/multiply/ change set of numbers in the array) those methods sends back the result to be displayed in another method. This application needs to hold the user input's numbers as an array. Thanks for your time. It is a great help!
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...
Hello, I have created the following code in C++. The goal of this code is to...
Hello, I have created the following code in C++. The goal of this code is to read 3 types of employee information(String/*Name*/, String/*Phone number*/,Int/*Office number*/, ) from a .txt file and store that information into an array which can be accessed by various functions. The code below is within a header file, and im trying to define some functions (within a .cpp file) of my class to carry out the following tasks. I have already managed to define a couple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT