Question

Create a class named Pizza that scores information about a single pizza. It should contain the...

Create a class named Pizza that scores information about a single pizza. It
should contain the following:
 Private instance variable to store the size of the pizza (either small,
medium, or larger), the number of cheese toppings, the number of
pepperoni toppings, and the number of ham toppings.
 Constructor(s) that set all of the instance variables.
 Public methods to get and set the instance variables.
 A public method named calsCost( ) that returns a double that is the cost
of the pizza.
Pizza cost is determined by:
Small: $10 + $2 per topping
Medium: $12 + $2 per topping
Large: $14 + $2 per topping.
 A public method named getDescription( ) that returns a String
containing the pizza cost as calculated by calsCost ( ).

Write test code to create several pizzas and output their descriptions. For
example, a large pizza with one cheese, one pepperoni and two ham toppings
should cost a total of $22.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Pizza {
    private String size;
    private int numCheeseToppings;
    private int numPepperoniToppings;
    private int numHamToppings;

    public Pizza(String size, int numCheeseToppings, int numPepperoniToppings, int numHamToppings) {
        this.size = size;
        this.numCheeseToppings = numCheeseToppings;
        this.numPepperoniToppings = numPepperoniToppings;
        this.numHamToppings = numHamToppings;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public int getNumCheeseToppings() {
        return numCheeseToppings;
    }

    public void setNumCheeseToppings(int numCheeseToppings) {
        this.numCheeseToppings = numCheeseToppings;
    }

    public int getNumPepperoniToppings() {
        return numPepperoniToppings;
    }

    public void setNumPepperoniToppings(int numPepperoniToppings) {
        this.numPepperoniToppings = numPepperoniToppings;
    }

    public int getNumHamToppings() {
        return numHamToppings;
    }

    public void setNumHamToppings(int numHamToppings) {
        this.numHamToppings = numHamToppings;
    }

    public double calcCost() {
        double cost = 14;
        if (size.equalsIgnoreCase("small")) {
            cost = 10;
        } else if (size.equalsIgnoreCase("medium")) {
            cost = 12;
        }
        cost += 2 * numCheeseToppings;
        cost += 2 * numHamToppings;
        cost += 2 * numPepperoniToppings;
        return cost;
    }

    public String getDescription() {
        String str = "Size: " + size;
        str += "\nNumber of cheese toppings: " + numCheeseToppings;
        str += "\nNumber of pepperoni toppings: " + numPepperoniToppings;
        str += "\nNumber of ham toppings: " + numHamToppings;
        str += String.format("\nTotal cost: $%.2f\n", calcCost());
        return str;
    }
}

class PizzaTest {

    public static void main(String[] args) {
        Pizza p1 = new Pizza("large", 1, 1, 2);
        Pizza p2 = new Pizza("small", 2, 0, 2);
        Pizza p3 = new Pizza("medium", 3, 1, 1);
        Pizza p4 = new Pizza("large", 1, 0, 0);
        Pizza p5 = new Pizza("medium", 1, 3, 1);

        System.out.println(p1.getDescription() + "\n");
        System.out.println(p2.getDescription() + "\n");
        System.out.println(p3.getDescription() + "\n");
        System.out.println(p4.getDescription() + "\n");
        System.out.println(p5.getDescription() + "\n");
    }
}

Add a comment
Know the answer?
Add Answer to:
Create a class named Pizza that scores information about a single pizza. It should contain the...
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
  • C# Programming Exercise and Bonus Exercise (Next Page) 1) Define a class called OrderPizza that has...

    C# Programming Exercise and Bonus Exercise (Next Page) 1) Define a class called OrderPizza that has member variables to track the type of pizza being order (either deep dish, hand tossed, or pan) along with the size (small, medium or large), type of toppings (pepperoni, cheese etc.) and the number of toppings. Create accessors for each property (ex.: type, size, type of toppings, and number of toppings). Implementation class should have a CalculateOrderPayment method to compute the total cost of...

  • 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...

  • 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...

  • USE!! PYTHON!!! Programming:   Pizza 1. Write a Pizza class to that this client code works. Please...

    USE!! PYTHON!!! Programming:   Pizza 1. Write a Pizza class to that this client code works. Please note that it is ok if the toppings are listed in a different order. >>> pie = Pizza() >>> pie Pizza('M',set()) >>> pie.setSize('L') >>> pie.getSize() 'L' >>>pie.addTopping('pepperoni') >>>pie.addTopping('anchovies') >>>pie.addTopping('mushrooms') >>> pie Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'}) >>>pie.addTopping('pepperoni') >>> pie Pizza('L',{'anchovies', 'mushrooms', 'pepperoni'}) >>>pie.removeTopping('anchovies') >>> pie Pizza('L',{'mushrooms', 'pepperoni'}) >>> pie.price() 16.65 >>> pie2 = Pizza('L',{'mushrooms','pepperoni'}) >>> pie2 Pizza('L',{'mushrooms', 'pepperoni'}) >>> pie==pie2 True The Pizza class should have...

  • Create an order entry screen program in C# to give a total for an individual pizza...

    Create an order entry screen program in C# to give a total for an individual pizza order. The Pizza order should have radio buttons for small $7, medium $9, and large $12 choices for the pizza. There should be a checkbox for drink (where a drink is $2 added if it is checked and nothing added if it is not checked. Include a textbox for the customer’s name. Have a button to calculate the subtotal (pizza choice and whether there...

  • Remember the pizza example from the class. You are given a randomly picked pizza from the...

    Remember the pizza example from the class. You are given a randomly picked pizza from the previously produced ones. Assume that you can have in one the two sizes (small or large) and you can have it in one the four different meat flavors. The number of pizzas produced with various size and meat topping combinations are given in the table below. Let Es F[pepperoni], and G (beef) Meat Topping SizePepperon Beef Chicken Ham Small Large 60 20 40 20...

  • Take the Pizza class from assignment # 1 and modify it in the following way: -...

    Take the Pizza class from assignment # 1 and modify it in the following way: - Rename it to DeluxePizza. - Add a stuffedWithCheese (boolean) attribute to the class. - Add a veggie Topping (integer) attribute to the class. This attribute keeps track of the number of vegetables toppings excluding mushrooms. Add a static attribute numberOfPizzas (integer) which will keep track of the number of DeluxePizza objects created Update the existing non-default constructor such that it increments the numberOfPizzas by...

  • Create a user friendly interface to order a pizza. Use appropriate controls (radio buttons, list boxes,...

    Create a user friendly interface to order a pizza. Use appropriate controls (radio buttons, list boxes, check boxes) to obtain the type of pizza (e.g. small, medium, large) and the toppings. Calculate the cost of the pizza based upon the size, number of toppings and delivery charge. Display a summary of the order in a text area along with the total cost. Provide buttons which places the order and clears the order. Allow for multiple pizzas to orders Submit as...

  • The function should have a class name CPizza that will have three constructors (default, type, and...

    The function should have a class name CPizza that will have three constructors (default, type, and copyl, public functions to use and implement are noted for you in the starter kit, and private member variables (all which are allocated dynamically). The private member variables include the three different sizes of pizza (Large, Medium, and Small), cost, a bool delivery, name, and delivery fee. The prices of the pizzas' are $20. $15, and $10 for large, medium, and small respectively. The...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

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