227k views
2 votes
Create a class named Pizza with data fields for description (such as sausage and onion) and price. Include a constructor that requires arguments for both fields and a method to display the data. Create a subclass named DeliveryPizzathat inherits from Pizza but adds a delivery fee and a delivery address. The description, price, and delivery address are required as arguments to the constructor. The delivery fee is $3 if the pizza ordered costs more than $15; otherwise it is $5. Write an application that instantiates at least two objects of each type, and display the values. Save the files as Pizza.java, DeliveryPizza.java, and DemoPizzas.java.

1 Answer

2 votes

Answer:

The three parts are as follows

Pizza.java

public class Pizza {

public String description;

public int price;

public Pizza() {}

public Pizza(String desc,int pri)

{

this. description=desc;

this.price=pri;

}

//Output

public String toString()

{

return this. description+" , "+this.price;

}

}

DeliveryPizza.java

public class DeliveryPizza extends Pizza{

public int fee;

public String address;

public DeliveryPizza(String desc,int pri,String add)

{

super(desc, pri);//invoke base class constructor

this.address=add;

if(super.price>15)

{

this.fee=3;//set delivery fee

}

else

{

this.fee=5;//set delivery fee

}

}

public String printDetails()

{

return super.toString()+", Fee : "+this.fee+" , "+"Address : "+this.address;

}

}

DemoPizzas.java

public class DemoPizzas {

public static void main(String[] args) {

Pizza pizza=new Pizza("Crudo",200);

System.out.println(pizza.toString());

Pizza pizza1=new Pizza("Marinara sauce",430);

System.out.println(pizza1.toString());

DeliveryPizza deliveryPizza=new DeliveryPizza("Pizza Margherita",15,"New Jercy");

System.out.println(deliveryPizza.printDetails());

DeliveryPizza deliveryPizza1=new DeliveryPizza("onion Pizza",20,"Maxico");

System.out.println(deliveryPizza1.printDetails());

}

}

User Azizbro
by
5.6k points