Write a Java application that meets the following specifications. Make sure to g
ID: 3828404 • Letter: W
Question
Write a Java application that meets the following specifications. Make sure to give the classes and methods the exact names shown in this prompt, since I will be testing them using my own test class.
The abstract class Pizza has two subclasses Pizza, namely TypeAPizza and TypeBPizza.
TypeAPizzas are round and weigh 25 grams per square centimeter, while TypeBPizzas are square and weigh 50 grams per square centimeter.
Every Pizza initially has ten slices with equal areas. Each Pizza contains zero or more toppings, stored in a list. Each topping name is a String. Think about what other variables you need.
Every Pizza has the following methods. At least one of these methods should be abstract in Pizza, but I have not indicated which one(s):
public void addTopping(String t) adds a topping to the pizza
public boolean checkTopping(String t) returns true if the pizza has a particular topping, otherwise false. Think carefully about the String comparisons.
public Double getCurrentAreaInSqCM() returns the current surface area in square CM
public double getCurrentWeightInGrams() returns the current weight of the pizza in grams
public double eatSlice() returns the weight of one slice if one is available; otherwise returns zero. Also does whatever is necessary to ensure that future calls to getCurrentAreaInSqCM() and getCurrentWeightInGrams() will reflect the reduced amount of pizza remaining.
Each type of Pizza needs a constructor. Think carefully about the parameters. Hint: Remember that variables that are protected, rather than private, in Pizza can be accessed from the subclasses.
You do not need to take user input for this problem. Use a driver class to thoroughly demonstrate your work. The driver class should create Pizzas of various sizes and demonstrate that all the methods work correctly.
Explanation / Answer
Output
-------------------------For Type A Pizza----------------------
Initial Area =31400.0
Initial Weight =785000.0
Eating One Slice
New Area =28260.0
New Weight =706500.0
Adding Cheese Topping
Checking For Cheese Topping :true
-------------------------For Type A Pizza----------------------
Initial Area =100.0
Initial Weight =2500.0
Eating One Slice
New Area =90.0
New Weight =2250.0
Adding Cheese Topping
Checking For Cheese Topping :true
Pizza.java
import java.util.ArrayList;
public abstract class Pizza {
protected double area;
protected double remaining;
protected int slices=10;
protected ArrayList<String> list=new ArrayList<>();
abstract protected void addTopping(String t);
protected boolean checkTopping(String t)
{
if(list.contains(t))
return true;
else
return false;
}
}
TypeAPizza.java
public class TypeAPizza extends Pizza {
double perSliceSize;
public TypeAPizza(int radius){
this.area=3.14*radius*radius;
this.remaining=this.area;
perSliceSize=area/10;
}
@Override
public void addTopping(String t) {
if(!list.contains(t))
this.list.add(t);
}
public double eatSlice()
{
if(slices>0)
{
slices--;
return perSliceSize*25;
}
else
return 0;
}
public double getCurrentAreaInSqCM()
{
return (perSliceSize*slices);
}
public double getCurrentWeightInGrams()
{
return getCurrentAreaInSqCM()*25;
}
}
TypeBPizza.java
public class TypeBPizza extends Pizza {
double perSliceSize;
public TypeBPizza(int length, int breadth){
this.area=length * breadth;
this.remaining=this.area;
perSliceSize=area/10;
}
@Override
public void addTopping(String t) {
if(!list.contains(t))
this.list.add(t);
}
public double eatSlice()
{
if(slices>0)
{
slices--;
return perSliceSize*50;
}
else
return 0;
}
public Double getCurrentAreaInSqCM()
{
return perSliceSize*slices;
}
public double getCurrentWeightInGrams()
{
return getCurrentAreaInSqCM()*25;
}
}
PizzaTest.java
public class PizzaTest {
public static void main(String[] args) {
TypeAPizza pizzaA=new TypeAPizza(100);
System.out.println("-------------------------For Type A Pizza----------------------");
System.out.println("Initial Area ="+ pizzaA.getCurrentAreaInSqCM());
System.out.println("Initial Weight ="+ pizzaA.getCurrentWeightInGrams());
System.out.println("Eating One Slice");
pizzaA.eatSlice();
System.out.println("New Area ="+ pizzaA.getCurrentAreaInSqCM());
System.out.println("New Weight ="+ pizzaA.getCurrentWeightInGrams());
System.out.println("Adding Cheese Topping");
pizzaA.addTopping("Cheese");
System.out.println("Checking For Cheese Topping :"+ pizzaA.checkTopping("Cheese"));
TypeBPizza pizzaB=new TypeBPizza(10,10);
System.out.println(" -------------------------For Type A Pizza----------------------");
System.out.println("Initial Area ="+ pizzaB.getCurrentAreaInSqCM());
System.out.println("Initial Weight ="+ pizzaB.getCurrentWeightInGrams());
System.out.println("Eating One Slice");
pizzaB.eatSlice();
System.out.println("New Area ="+ pizzaB.getCurrentAreaInSqCM());
System.out.println("New Weight ="+ pizzaB.getCurrentWeightInGrams());
System.out.println("Adding Cheese Topping");
pizzaB.addTopping("Cheese");
System.out.println("Checking For Cheese Topping :"+ pizzaB.checkTopping("Cheese"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.