Question

In: Computer Science

For this homework we are going to walk you through creating a class. Lets look at...

For this homework we are going to walk you through creating a class. Lets look at a pizza restaurant and create a class for this. We will call our class pizza. The goal for every class is to include everything that has to happen for that class in 1 place. So whenever we create a class we need to think of what the nouns or variables will be for that class and then what the actions or methods will be for that class.For the pizza class lets say we will need to know pizza size (1 for small, 2 for medium, and 3 for large), how many toppings, how many drinks, what size drinks (again lets assume 1 for small, 2 for medium, and 3 for large – lets assume if people order more than 1 drink it’s going to be the same size for simplicity). We will also need to know the total cost (make up numbers for this one) and whether or no this will be for dine in, pick up, or delivery (assume 1, 2, or 3). So these will all become our member variables. In general we want these all to be private.Our actions will need to be ordering the pizza and calculating the cost so these will become our methods. Methods are usually public.So go ahead and write or type out your class.Finally, we will need a main to run it. Main acts like the conductor of an orchestra. We don’t want main to do the actual work, rather it will direct others to do the work. So we will need to have an instance of our pizza class and then call the ordering method and the compute cost method. Write main.

Solutions

Expert Solution

Solution for given question in two possible ways:

1. Pizza Class ( that includes Main)

============================================================

import java.util.Scanner;

public class pizza

{

   public static void main(String[] args) {

   Scanner keyboard = new Scanner( System.in );

      int pizzaLength = 0;

      int pizzaWidth = 0;

      int pizzaShape;

      double pizzaDiameter = 0;

      double pizzaToppingPrice = 0.025;

      int numberOfPizzaToppings;

      double pizzaSauceAndCheese = 0.036;

      double pizzaDoughPrice = 0.019;

      char doughType;

      String dough = null;

      int numberOfPizzas;

      double pizzasBeforeTax;

      int cheesyCrustInput;

      int deliveryWantedInput;

      int deliveryCharge = 0;

      double doughVolume = 0;

      double area = 0;

      double crustCost;

      int basePrice;

      int costOfPizzaWithTax;

      //Shape of pizza

      System.out.println ("What is the shape of pizza ?");

      System.out.println("Enter (1) for Rectange or (2) Round?");

      pizzaShape = keyboard.nextLine().charAt(0);

      if(pizzaShape == 1){

               System.out.println("What Length?");

               pizzaLength = keyboard.nextInt();

               System.out.println("What Width?");

               pizzaWidth = keyboard.nextInt();

               area = pizzaLength * pizzaWidth;

               }

      else if(pizzaShape == 2){

               System.out.println("What is the Diameter?");

               pizzaDiameter = keyboard.nextInt();

               area = Math.PI * (pizzaDiameter / 2);

               }

      //Volume

      System.out.print("type of dough you want? (1)Classic Hand-Tossed, (2)Thin and Crispy, (3)Texas Toast, or (4)Pan. (enter 1,2,3, or 4.");

      doughType = keyboard.nextLine().charAt(0);

      String Crust = "";

      String deliveryOutput="";

      if (doughType == 1) {

          dough = "Classic Hand-Tossed";

          doughVolume = area * .25;

          }

      else if (doughType == 2) {

          dough = "Thin and Crispy";

          doughVolume = area * .1;

          }

      else if (doughType == 3) {

          dough = "Texas Toast";

          doughVolume = area * .9;

          }

      else if (doughType == 4) {

          dough = "Pan";

          doughVolume = area * .5;

          }

     //Cheesey crust

      if(doughType == 2){

      }

      else{

          System.out.println("Would you like cheesy crust? (1) for yes (2) for no");

          cheesyCrustInput = keyboard.nextInt();

               if(cheesyCrustInput == 1){

               crustCost = area * .02;

                Crust = "";

               }

               else{

                Crust = "NO";

               }

      }

      //Toppings

      System.out.print("Enter the number of toppings:");

      numberOfPizzaToppings = keyboard.nextInt();

      int toppings;

      if(numberOfPizzaToppings == 0){

          toppings = 0;

         }

      else{

           toppings = numberOfPizzaToppings;

         }

     //Base price

      basePrice = area (pizzaSauceAndCheese + (pizzaToppingPrice * numberOfPizzaToppings) + (pizzaDoughPrice * doughVolume));

      //how many pizzas

       System.out.print("Enter the number of pizzas being ordered:");

       numberOfPizzas = keyboard.nextInt();

       pizzasBeforeTax = basePrice * numberOfPizzas;

      //tax

      costOfPizzaWithTax = pizzasBeforeTax ( 1 + 0.07);

      //delivery fee

      System.out.print("Would you like delivery? (1) for yes, (2) for no.");

      deliveryWantedInput = keyboard.nextInt();

      if(deliveryWantedInput == 1){

          deliveryOutput = numberOfPizzas + "deliverd";

         if(costOfPizzaWithTax < 10){

         deliveryCharge = 3;

         }

         if( 10 < costOfPizzaWithTax && costOfPizzaWithTax < 20 ){

         deliveryCharge = 2;

         }

         if(20 < costOfPizzaWithTax && costOfPizzaWithTax < 30){

         deliveryCharge = 1;

         }

         if(costOfPizzaWithTax > 30){

         deliveryCharge = 0;

         }

      }

      else{

       deliveryOutput = "no delivery";

      }

      //display total cost

      int total = costOfPizzaWithTax + deliveryCharge;

    //System.out.println("Total Due: $" + df.format(total));

      if(pizzaShape == 1){

      System.out.print("User requested: Rectangular, " + pizzaWidth + "X"

      + pizzaLength + ", " + dough + toppings + "topping(s)"

                      + Crust + "cheesy crust," + deliveryOutput + " - Program Output:");

      System.out.println("Details for - Rectangular Pizza (" + pizzaWidth + "\" X " + pizzaLength + "\"):");

      System.out.println( "Area: " + area );

      System.out.println( "Volume:" + doughVolume);

      System.out.println( "Base price:   $" + basePrice);

      System.out.println( "With Cheesy: $");

      System.out.println( "Multi Total: $" + pizzasBeforeTax);

      System.out.println( "With Tax:     $" + costOfPizzaWithTax);

      System.out.println( "And Delivery: $" + total);

      }

      else{

      System.out.print("User requested: Circular, " + pizzaDiameter + "\", " + dough + toppings + "topping(s)"

                      + Crust + "cheesy crust," + deliveryOutput + " - Program Output:");

      System.out.println( "Area: " + area );

      System.out.println( "Volume:" + doughVolume);

      System.out.println( "Base price:   $" + basePrice);

      System.out.println( "With Cheesy: $");

      System.out.println( "Multi Total: $" + pizzasBeforeTax);

      System.out.println( "With Tax:     $" + costOfPizzaWithTax);

      System.out.println( "And Delivery: $" + total);

      }                  

   }

2. Pizza Class and separate Main class ( pizza.java and MainClass.java )

================================================================

public class Pizza

{

            private String pizzaSize;

            private int cheeseCount;

            private int pepperoniCount;

            private int hamCount;

            public Pizza()

            {

                        this.pizzaSize = "";

                        this.cheeseCount = 0;

                        this.pepperoniCount = 0;

                        this.hamCount = 0;

            }

           

            public Pizza(String pizzaSize, int cheeseCount,

                                                            int pepperoniCount, int hamCount)

            {

                        this.pizzaSize = pizzaSize;

                        this.cheeseCount = cheeseCount;

                        this.pepperoniCount = pepperoniCount;

                        this.hamCount = hamCount;

            }

           

            public String getPizzaSize()

            {

                        return pizzaSize;

            }

            public void setPizzaSize(String pizzaSize)

            {

                        this.pizzaSize = pizzaSize;

            }

            public int getNumCheeseToppings()

            {

                        return cheeseCount;

            }

            public void setNumCheeseToppings(int cheeseCount)

            {

                        this.cheeseCount = cheeseCount;

            }

            public int getNumPepperoniToppings()

            {

                        return pepperoniCount;

            }

            public void setNumPepperoniToppings(int pepperoniCount)

            {

                        this.pepperoniCount = pepperoniCount;

            }

            public int getNumHmaToppings()

            {

                        return hamCount;

            }

            public void setNumHmaToppings(int hamCount)

            {

                        this.hamCount = hamCount;

            }

            public double calcCost()

            {                     

                        if(pizzaSize.equalsIgnoreCase("small"))

                        {

                                    return 10 + (cheeseCount + pepperoniCount + hamCount) * 2;

                        }

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

                        {

                                    return 12 + (cheeseCount + pepperoniCount + hamCount) * 2;

                        }

                        else if(pizzaSize.equalsIgnoreCase("large"))

                        {

                                    return 14 + (cheeseCount + pepperoniCount + hamCount) * 2;

                        }

                        else

                        {

                                    return 0.0;

                        }

            }

            public String getDescription()

            {

                        return "Pizza size: " + pizzaSize + "\n Cheese toppings: "

                                                + cheeseCount + "\n Pepperoni toppings: "

                                                + pepperoniCount + "\n Ham toppings: " + hamCount

                                                + "\n Pizza cost: $" + calcCost() + "\n";

            }

}

===============================================

// MainClass.java

================================================

public class MainClass

{

            public static void main(String[] args)

            {

                        Pizza p1 = new Pizza("large", 1, 1, 2);

                        Pizza p2 = new Pizza("small", 2, 1, 1);

                        Pizza p3 = new Pizza("medium", 1, 2, 1);

                       

                        System.out.println(p1.getDescription());

                        System.out.println(p2.getDescription());

                        System.out.println(p3.getDescription());

            }

}

Output:

Pizza size: large

Cheese toppings: 1

Pepperoni toppings: 1

Ham toppings: 2

Pizza cost: $22.0

Pizza size: small

Cheese toppings: 2

Pepperoni toppings: 1

Ham toppings: 1

Pizza cost: $18.0

Pizza size: medium

Cheese toppings: 1

Pepperoni toppings: 2

Ham toppings: 1

Pizza cost: $20.0


Related Solutions

We started creating a Java class for Car. In this lab we are going to complete...
We started creating a Java class for Car. In this lab we are going to complete it in the following steps: 1- First create the Car class with some required instance variables, such make, model, year, price, speed, maxSpeed, isOn, isMoving, and any other properties you like to add. 2- Provide couple of constructors for initializing different set of important properties, such as make, model, year, and price. Make sure that you do not repeat initialization of instance variables in...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Excel Homework: We are going to start working on sample statistics—that is, on measures we can...
Excel Homework: We are going to start working on sample statistics—that is, on measures we can sue to describe a sample. The two that we will learn about today are histograms, and 5 number summaries (which can be represented using box-plots). 1. You are designing a game, and considering having players roll a six-sided die and a 20-sided die, and add the values of each die. To get a picture of what happens when someone rolls dice like that, you...
For Homework 4, we are going to present the user with a series of menus, accept...
For Homework 4, we are going to present the user with a series of menus, accept as input the action they wish to take, and act appropriately. You must practice basic input validation to ensure that the menu option they chose is valid. Create “CPS132 Homework 4” project in Eclipse Create “Homework4.py” file Download "Homework4_incomplete.py" Paste the text into "Homeworkpy" Make sure to fill in the appropriate information in the header including your name, username, due date, Homework #4, and...
Lets make a Deal c# simulation? The steps that you will need to complete this homework...
Lets make a Deal c# simulation? The steps that you will need to complete this homework is: //display and output to the screen telling the player that they can win a car with exactly $1000 //simulate the rolling of the die for $100, $200 or $300 //show the player the amount they have just rolled //show the player their total amount //check if total amount has not exceeded $1000. If exceeded, game over (tell player they lost) //if total is...
For this discussion, we are going to look at data that should follow a linear relationship....
For this discussion, we are going to look at data that should follow a linear relationship. 1. Begin by gathering data that interests you. - You can look online, in the newspaper, conduct an experiment yourself… Be creative! 2. What variables did you choose to explore? - Which is the explanatory variable and which is the response variable? Fully explain why. 3. Describe how you obtained your data. (Since I'm only allowed about 3 questions per post, this would be...
This week we are going to look at aspects of mental illness, stigma and the manner...
This week we are going to look at aspects of mental illness, stigma and the manner in which society treats those who suffer from mental illness.Look at one or two of the resources posted in the assignment folder. Discuss your opinion as to how your chosen case was handled either by the court system, by mental health professionasl, or by the mental health system or society or schools.
The current issue we are going to look at is boeing's current issue with the safety...
The current issue we are going to look at is boeing's current issue with the safety of their 737 MAM jet. Last week one of their 737 crashed and killed over 150 people on Etheopian Airlines. How might this current international issue affect boeings international marketing decisions?
This week we are going to look at how benchmarking is used in the current day...
This week we are going to look at how benchmarking is used in the current day health care industry! Please find a current events (within the past year) news article from a reputable news source (MSN, New York Times, Chicago Tribune, etc) that discusses the use of benchmarking by a health care organization. For your original post, please provide a 150+ word summary of your article. Be sure that you demonstrate your understanding of benchmarking by discussing how the benchmarking...
You are the first to walk into class and you see an iPod sitting on the...
You are the first to walk into class and you see an iPod sitting on the floor under one of the student desks. You pick it up and turn it on. It works just fine, and it even has some of your favorite music listed. Looking around, you realize you are still the only person in the room. What should you do? Use the ethical decision-making process taught in this course (seven steps) to explain what an ethical decision would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT