Question

Using Java, I created 3 files, Pizza,PizzaOrder, and PizzaOrder_Demo that I attached down below. But I...

Using Java, I created 3 files, Pizza,PizzaOrder, and PizzaOrder_Demo that I attached down below. But I cannot compile it. Could you please point out the error.The question is that

This programming project extends Q1. Create a PizzaOrder class that allows up to three pizzas to be saved in an order. Each pizza saved should be a Pizza object as described in Q1. In addition to appropriate instance variables and constructors, add the following methods:

public void setNumPizzas(int numPizzas)—sets the number of pizzas in the order. numPizzas must be between 1 and 3.

public void setPizza1(Pizza pizza1)—sets the first pizza in the order.

public void setPizza2(Pizza pizza2)—sets the second pizza in the order.

public void setPizza3(Pizza pizza3)—sets the third pizza in the order.

public double calcTotal()—returns the total cost of the order.

Write a main method to test the class. The setPizza2 and setPizza3 methods will be used only if there are two or three pizzas in the order, respectively. Sample code illustrating the methods is shown below. Note that first three lines are incomplete. You must complete them as part of the Programming Project.

Pizza pizza1 = // Code to create a large pizza, 1 cheese, 1 ham
Pizza pizza2 = // Code to create a medium pizza, 2 cheese, 2 pepperoni
PizzaOrder order = // Code to create an order
order.setNumPizzas(2); // 2 pizzas in the order
order.setPizza1(pizza1); // Set first pizza
order.setPizza2(pizza2); // Set second pizza
double total = order.calcTotal(); // Should be 18+20 = 38





//My program
class Pizza
{
private String sofpizza;
private int nofcheese,nofpepperoni,nofham;
//construtor
public Pizza(String sofpizza, int nofcheese, int nofpepperoni, int nofham)
{
this.sofpizza=sofpizza;
this.nofcheese=nofcheese;
this.nofpepperoni=nofpepperoni;
this.nofham=nofham;
}
//method

public void set_sofpizza(String l)
{
this.sofpizza=l;
}
public String get_sofpizza()
{
return sofpizza;
}
public void set_nofcheese(int b)
{
this.nofcheese=nofcheese;
}
public int get_nofcheese()
{
return nofcheese;
}
public void set_nofpepperoni(int p)
{
this.nofpepperoni=nofpepperoni;
}
public int get_nofpepperoni()
{
return nofpepperoni;
}
public void set_nofham(int h)
{
this.nofham=nofham;
}
public int get_nofham()
{
return nofham;
}
public double CalCost()
{
if(sofpizza.equalsIgnoreCase("small"))

return (10+(2*nofcheese)+(2*nofpepperoni)+(2*nofham));
else if(sofpizza.equalsIgnoreCase("medium"))
return (12+(2*nofcheese)+(2*nofpepperoni)+(2*nofham));
else
return (14+(2*nofcheese)+(2*nofpepperoni)+(2*nofham));
}

public String getDiscription
() {
return(" Size of pizza= "+sofpizza+"\n number of cheese= "+nofcheese+"\n number of pepperoni= "+nofpepperoni+"\n number of ham="+nofham);
}
}

//import java.util.Pizza;
class PizzaOrder
{
private int numPizzas;
private Pizza pizza1;
private Pizza pizza2;
private Pizza pizza3;

//constructor
public PizzaOrder(int numPizzas,Pizza pizza1,Pizza pizza2,Pizza pizza3)
{
this.numPizzas=numPizzas;
this.pizza1=pizza1;
this.pizza2=pizza2;
this.pizza3=pizza3;
}

  
public void SetNumPizzas(int numPizzas)
{
if(numPizzas>3)
this.numPizzas=3;
else if(numPizzas<1)
this.numPizzas=0;
else
numPizzas=numPizzas;

}
public int getNumpizzas()
{
return numPizzas;
}
public void setPizza1(Pizza pizza1)
{
this.pizza1=pizza1;
}
public Pizza getPizza1()
{
return pizza1;
}
public void setPizza2(Pizza pizza2)
{
this.pizza2=pizza2;
}
public Pizza getPizza2()
{
return pizza2;
}
public void setPizza3(Pizza pizza3)
{
this.pizza3=pizza3;
}
public Pizza getPizza3()
{
return pizza3;
}
public double calcTotal()
{
double total=pizza1.CalCost();
if(numPizzas>=2)
total+=pizza2.CalCost();
if(numPizzas==3)
total+=pizza3.CalCost();
return total;
}
}

  

import java.util.Scanner;
public class PizzaOrder_Demo
{
public static void main(String [] args)
{
Scanner input=new Scanner(System.in);
String size1;
int cheese1,pepp1,ham1,cheese2,pepp2,ham2,num;
System.out.println("Enter the size of pizza for pizza1 for pizza1 ");
size1=input.nextLine();
System.out.println("Enter the number of cheese toppings for pizza1");
cheese1=input.nextInt();
System.out.println("the number of pepperoni toppings for pizza1");
pepp1=input.nextInt();
System.out.println("the number of ham toppings for pizza1");
ham1=input.nextInt();
System.out.println("Enter the size of pizza for pizza1 for pizza2");
input.nextLine();
String size2;
size2=input.nextLine();
System.out.println("Enter the number of cheese toppings for pizza2");
cheese2=input.nextInt();
System.out.println("the number of pepperoni toppings for pizza2");
pepp2=input.nextInt();
System.out.println("the number of ham toppings for pizza2");
ham2=input.nextInt();
Pizza pizza1=new Pizza(size1,cheese1,pepp1,ham1);
Pizza pizza2=new Pizza(size2,cheese2,pepp2,ham2);
Pizza pizza3=new Pizza("small",0,0,0);
PizzaOrder order=new PizzaOrder(1,pizza1,pizza2,pizza3);
num=input.nextInt();
order.SetNumPizzas(num);
order.setPizza1(pizza1);
order.setPizza2(pizza2);
double total=order.calcTotal();


}
}


  

  
  
  
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class PizzaOrder_Demo {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String size1;

int cheese1, pepp1, ham1, cheese2, pepp2, ham2, num;

System.out.println("Enter the size of pizza for pizza1 for pizza1 ");

size1 = input.nextLine();

System.out.println("Enter the number of cheese toppings for pizza1");

cheese1 = input.nextInt();

System.out.println("the number of pepperoni toppings for pizza1");

pepp1 = input.nextInt();

System.out.println("the number of ham toppings for pizza1");

ham1 = input.nextInt();

System.out.println("Enter the size of pizza for pizza1 for pizza2");

input.nextLine();

String size2;

size2 = input.nextLine();

System.out.println("Enter the number of cheese toppings for pizza2");

cheese2 = input.nextInt();

System.out.println("the number of pepperoni toppings for pizza2");

pepp2 = input.nextInt();

System.out.println("the number of ham toppings for pizza2");

ham2 = input.nextInt();

Pizza pizza1 = new Pizza(size1, cheese1, pepp1, ham1);

Pizza pizza2 = new Pizza(size2, cheese2, pepp2, ham2);

Pizza pizza3 = new Pizza("small", 0, 0, 0);

PizzaOrder order = new PizzaOrder(1, pizza1, pizza2, pizza3);

num = input.nextInt();

order.SetNumPizzas(num);

order.setPizza1(pizza1);

order.setPizza2(pizza2);

double total = order.calcTotal();

System.out.println("The total cost is "+ total);

input.close();

}

}

class PizzaOrder {

private int numPizzas;

private Pizza pizza1;

private Pizza pizza2;

private Pizza pizza3;

// constructor

public PizzaOrder(int numPizzas, Pizza pizza1, Pizza pizza2, Pizza pizza3) {

this.numPizzas = numPizzas;

this.pizza1 = pizza1;

this.pizza2 = pizza2;

this.pizza3 = pizza3;

}

public void SetNumPizzas(int numPizzas) {

if (numPizzas > 3)

this.numPizzas = 3;

else if (numPizzas < 1)

this.numPizzas = 0;

}

public int getNumpizzas() {

return numPizzas;

}

public void setPizza1(Pizza pizza1) {

this.pizza1 = pizza1;

}

public Pizza getPizza1() {

return pizza1;

}

public void setPizza2(Pizza pizza2) {

this.pizza2 = pizza2;

}

public Pizza getPizza2() {

return pizza2;

}

public void setPizza3(Pizza pizza3) {

this.pizza3 = pizza3;

}

public Pizza getPizza3() {

return pizza3;

}

public double calcTotal() {

double total = pizza1.CalCost();

if (numPizzas >= 2)

total += pizza2.CalCost();

if (numPizzas == 3)

total += pizza3.CalCost();

return total;

}

}

class Pizza {

private String sofpizza;

private int nofcheese, nofpepperoni, nofham;

// construtor

public Pizza(String sofpizza, int nofcheese, int nofpepperoni, int nofham) {

this.sofpizza = sofpizza;

this.nofcheese = nofcheese;

this.nofpepperoni = nofpepperoni;

this.nofham = nofham;

}

// method

public void set_sofpizza(String l) {

this.sofpizza = l;

}

public String get_sofpizza() {

return sofpizza;

}

public void set_nofcheese(int b) {

this.nofcheese = b;

}

public int get_nofcheese() {

return nofcheese;

}

public void set_nofpepperoni(int p) {

this.nofpepperoni = p;

}

public int get_nofpepperoni() {

return nofpepperoni;

}

public void set_nofham(int h) {

this.nofham = h;

}

public int get_nofham() {

return nofham;

}

public double CalCost() {

if (sofpizza.equalsIgnoreCase("small"))

return (10 + (2 * nofcheese) + (2 * nofpepperoni) + (2 * nofham));

else if (sofpizza.equalsIgnoreCase("medium"))

return (12 + (2 * nofcheese) + (2 * nofpepperoni) + (2 * nofham));

else

return (14 + (2 * nofcheese) + (2 * nofpepperoni) + (2 * nofham));

}

public String getDiscription() {

return (" Size of pizza= " + sofpizza + "\n number of cheese= " + nofcheese + "\n number of pepperoni= "

+ nofpepperoni + "\n number of ham=" + nofham);

}

}

//////////////////////////////////

I put all the three files in one file and then compiled them all......

According to your program this is the output that I got..........

/////////////////////

Let me tell you all the errors that i corrected.......

public void set_nofcheese(int b) {

this.nofcheese = b;

}

//You had written

public void set_nofcheese(int b) {

this.nofcheese = nofcheese;

//Same error here..

public void set_nofpepperoni(int p) {

this.nofpepperoni = p;

}

//Same error here too

public void set_nofham(int h) {

this.nofham = h;

}

//Another error

public void SetNumPizzas(int numPizzas) {

if (numPizzas > 3)

this.numPizzas = 3;

else if (numPizzas < 1)

this.numPizzas = 0;

//You had simply given an extra else condition which wasnt needed;

//After changing that the code runs......

//Hope this helps you

Happy Coding :) :)

Add a comment
Know the answer?
Add Answer to:
Using Java, I created 3 files, Pizza,PizzaOrder, and PizzaOrder_Demo that I attached down below. But I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Q1. rewrite the exercise on April 4 by adding the following operations: 1) In the php...

    Q1. rewrite the exercise on April 4 by adding the following operations: 1) In the php script, create an associative array named $order that stores the following values: - the number of cheese toppings; - the number of pepperoni toppings; - the number of ham toppings; - the size of the ordered pizza; - the number of the ordered pizza; - the total cost of the order; - the discount rate; - the total cost after the discount. 2) the...

  • Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a...

    Hello can someone help me in my code I left it as comment  task #0, task#1, task#2a and task#2b the things that I am missing I'm using java domain class public class Pizza { private String pizzaCustomerName; private int pizzaSize; // 10, 12, 14, or 16 inches in diameter private char handThinDeep; // 'H' or 'T' or 'D' for hand tossed, thin crust, or deep dish, respecitively private boolean cheeseTopping; private boolean pepperoniTopping; private boolean sausageTopping; private boolean onionTopping; private boolean...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

  • Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import...

    Must be written in java. Modularize the following code. //CIS 183 Lab 4 //Joseph Yousef import java.util.*; public class SalaryCalc { public static void main(String [] args) { Scanner input = new Scanner(System.in); int sentinelValue = 1; //initializes sentinelValue value to 1 to be used in while loop while (sentinelValue == 1) { System.out.println("Enter 1 to enter employee, or enter 2 to end process: ");    sentinelValue = input.nextInt(); input.nextLine(); System.out.println("enter employee name: "); String employeeName = input.nextLine(); System.out.println("enter day...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT