Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7. Complete the following tasks: a. Design a class named Pizza . Data fields inc

ID: 666794 • Letter: 7

Question

7. Complete the following tasks:

a. Design a class named Pizza . Data fields include a string field for a topping(such as pepperoni) and numeric fields for diameter in inches (such as 12)and price (such as 13.99). Include methods to get and set values for each of these fields. Create the class diagram and write the pseudocode that definesthe class.

b. Design an application that declares two Pizza objects and sets and displaystheir values.

c. Design an application that declares an array of 10 Pizza objects. Prompt theuser for data for each Pizza , then display all the values.

d. Design an application that declares an array of 10 Pizza objects. Prompt theuser for a topping and diameter for each Pizza , and pass each object to amethod that computes the price and returns the complete Pizza object to themain program. Then display all the Pizza values. A 12 inch pizza is $13.99, a14 inch pizza is $16.99, and a 15 inch pizza is $19.99. Any other entered size isinvalid and should cause the price to be set to 0.

Explanation / Answer


//Part-a
/*Pizza class that contains the constructor and methods to set toppings,diameter and price.
*The class constains methods to return the toppings,diamter and price of pizza object.
*ToString method returns the object of pizza in stirng representaion.
**/
//Pizza.java
public class Pizza
{
   //private members of class
   private String toppings;
   private int diameter;
   private double price;
  
  
   //constructor of class
   public Pizza()
   {
       toppings="";
       diameter=0;
       price=0;
   }
  
   //Mutator methods for toppings,diamter and price
   public void setToppings(String toppings)
   {
       this.toppings=toppings;
   }
   public void setDiameter(int diameter)
   {
       this.diameter=diameter;
   }  
   public void setPrice(double price)
   {
       this.price=price;
   }
   //Accessor methods to get toppings,diamter and price
   public String getToppings()
   {
       return toppings;
   }
   public int getDiameter()
   {
       return diameter;
   }  
   public double getPrice()
   {
       return price;
   }

   //Overide toString method that returns the description of pizza object
   public String toString()
   {      
       return "A "+diameter+"-inch diameter pizza is $ "+price ;
   }
}

--------------------------------------------------------------------------------------------------------------------

Part-b

/**The class cretes two instance of Pizza objects and set diameter then print the object of pizza* */
//Part_b.java
public class Part_b
{
   public static void main(String[] args)
   {
       //Create two pizza class objects
       Pizza pizza1=new Pizza();
       pizza1.setToppings("Pizza-One");
       pizza1.setDiameter(12);
      
       //print pizza1 object
       System.out.println(pizza1.toString());
      
       Pizza pizza2=new Pizza();      
       pizza2.setToppings("Pizza-One");
       pizza2.setDiameter(14);
      
      
       //print pizza1 object
           System.out.println(pizza2.toString());
      
   }
}


sample output;

A 12-inch diameter pizza is $ 13.99
A 14-inch diameter pizza is $ 16.99

--------------------------------------------------------------------------------------------------------------------

Part-c

/*The program prompt user to enter the details(name and diameter of pizza)
* of ten pizza objects and then print the details of the ten pizza details
* */
import java.util.Scanner;
public class Part_c
{
   public static void main(String[] args)
   {
       //set costant size of array 10
       final int size=10;
       Scanner reader=new Scanner(System.in);
       //create an array of Pizza type of size =10
       Pizza[] pizza=new Pizza[size];
          
       //prompt user to enter the details of pizza
       for (int index = 0; index < pizza.length; index++)
       {
           //create an instance of pizza
           pizza[index]=new Pizza();
          
           System.out.println("Enter name of pizza : ");
           String toppings=reader.nextLine();
           //calling setToppings to set toppings of pizza
           pizza[index].setToppings(toppings);
          
           System.out.println("Enter diameter of pizza :(in inches) ");
           //calling setDiameter to set diameter of pizza
           int diameter=Integer.parseInt(reader.nextLine());
           pizza[index].setDiameter(diameter);
          
       }
      
       //print the pizza array objects      
       for (int index = 0; index < pizza.length; index++)
       {
           System.out.println("Pizza "+(index+1)+" details");
           System.out.println(pizza[index]);
       }
      
      
   }
}

Sample output:

Pizza 1 details
Enter name of pizza :
PizzaOne
Enter diameter of pizza :(in inches)
12
Pizza 2 details
Enter name of pizza :
PizzaTwo
Enter diameter of pizza :(in inches)
14
Pizza 3 details
Enter name of pizza :
PizzaThree
Enter diameter of pizza :(in inches)
12
Pizza 4 details
Enter name of pizza :
PizzaFour
Enter diameter of pizza :(in inches)
12
Pizza 5 details
Enter name of pizza :
PizzaFive
Enter diameter of pizza :(in inches)
15
Pizza 6 details
Enter name of pizza :
PizzaSiz
Enter diameter of pizza :(in inches)
12
Pizza 7 details
Enter name of pizza :
PizzaSeven
Enter diameter of pizza :(in inches)
15
Pizza 8 details
Enter name of pizza :
PizzaNine
Enter diameter of pizza :(in inches)
12
Pizza 9 details
Enter name of pizza :
PizzaNine
Enter diameter of pizza :(in inches)
12
Pizza 10 details
Enter name of pizza :
PizzaTen
Enter diameter of pizza :(in inches)
14
Pizza 1 details
A 12-inch diameter pizza is $ 13.99
Pizza 2 details
A 14-inch diameter pizza is $ 16.99
Pizza 3 details
A 12-inch diameter pizza is $ 13.99
Pizza 4 details
A 12-inch diameter pizza is $ 13.99
Pizza 5 details
A 15-inch diameter pizza is $ 19.99
Pizza 6 details
A 12-inch diameter pizza is $ 13.99
Pizza 7 details
A 15-inch diameter pizza is $ 19.99
Pizza 8 details
A 12-inch diameter pizza is $ 13.99
Pizza 9 details
A 12-inch diameter pizza is $ 13.99
Pizza 10 details
A 14-inch diameter pizza is $ 16.99

--------------------------------------------------------------------------------------------------------------------

Part-d

/**The java program that prompts user to enter details of ten pizza objects
* and then calls the method computerTotalCost that create the order of all pizza
* and returns the stirng represetntaion of all pizzas and total cost of pizza */
import java.util.Scanner;
public class Part_d
{
   public static void main(String[] args)
   {
      
       //constant cost of pizza based on diameters
       final double COST_OF_12_INCH_PIZZA=13.99;
       final double COST_OF_14_INCH_PIZZA=16.99;
       final double COST_OF_15_INCH_PIZZA=19.99;
      
       //set costant size of array 10
       final int size=10;
       Scanner reader=new Scanner(System.in);
       //create an array of Pizza type of size =10
       Pizza[] pizza=new Pizza[size];
      
      
       //prompt user to enter the details of pizza
       for (int index = 0; index < pizza.length; index++)
       {
           //create an instance of pizza
           pizza[index]=new Pizza();
           System.out.println("Pizza "+(index+1)+" details");
           System.out.println("Enter name of pizza : ");
           String toppings=reader.nextLine();
           pizza[index].setToppings(toppings);
          
           System.out.println("Enter diameter of pizza :(in inches) ");
           int diameter=Integer.parseInt(reader.nextLine());
           pizza[index].setDiameter(diameter);
          
           //check the diameter of pizza and set the price
           //if the diamter is not of size 12,14 or 14 then
           //set the price to zero
           if(diameter==12)
               pizza[index].setPrice(COST_OF_12_INCH_PIZZA);
           else if(diameter==14)
               pizza[index].setPrice(COST_OF_14_INCH_PIZZA);
           else if(diameter==15)
               pizza[index].setPrice(COST_OF_15_INCH_PIZZA);
           else
               pizza[index].setPrice(0);  
       }
      
       //calling method computeTotalCost of pizza
       String completeOrder=computeTotalCost(pizza);      
      
       System.out.println("Complete pizza order details");
       System.out.println(completeOrder);
      
      
      
   }
   /*The method computerTotalCost takes an array of pizza and totalCost as
   * arguments and returns the total pizza object details and totalcost of pizza
   * */
   private static String computeTotalCost(Pizza[] pizza)
   {
       double totalCost=0;//to calculate total cost
       String completePizza=""; //string to store complete pizza objects
      
       for (int i = 0; i < pizza.length; i++)
       {
           //Add price to totalCost
           totalCost+=pizza[i].getPrice();
           //add string representation of pizza to completePizza string
           completePizza+=pizza[i].toString()+" ";
       }
               //format for 2 decimal points
       return completePizza+" Total cost : "+String.format("%.2f",totalCost);
   }
}

Sample output

Pizza 1 details

Enter name of pizza :
PizzaOne
Enter diameter of pizza :(in inches)
12
Pizza 2 details
Enter name of pizza :
PizzaTwo
Enter diameter of pizza :(in inches)
14
Pizza 3 details
Enter name of pizza :
PizzaThree
Enter diameter of pizza :(in inches)
15
Pizza 4 details
Enter name of pizza :
PizzaFour
Enter diameter of pizza :(in inches)
12
Pizza 5 details
Enter name of pizza :
PizzaFive
Enter diameter of pizza :(in inches)
14
Pizza 6 details
Enter name of pizza :
PizzaSix
Enter diameter of pizza :(in inches)
15
Pizza 7 details
Enter name of pizza :
PizzaSeven
Enter diameter of pizza :(in inches)
15
Pizza 8 details
Enter name of pizza :
PizzaEight
Enter diameter of pizza :(in inches)
12
Pizza 9 details
Enter name of pizza :
PizzaNine
Enter diameter of pizza :(in inches)
12
Pizza 10 details
Enter name of pizza :
PizzaTen
Enter diameter of pizza :(in inches)
15
Complete pizza order details
A 12-inch diameter pizza is $ 13.99
A 14-inch diameter pizza is $ 16.99
A 15-inch diameter pizza is $ 19.99
A 12-inch diameter pizza is $ 13.99
A 14-inch diameter pizza is $ 16.99
A 15-inch diameter pizza is $ 19.99
A 15-inch diameter pizza is $ 19.99
A 12-inch diameter pizza is $ 13.99
A 12-inch diameter pizza is $ 13.99
A 15-inch diameter pizza is $ 19.99

Total cost : 169.90

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote