Question

Create a class named Pizzawith the following data fields: description - of type String price -...

Create a class named Pizzawith the following data fields:

  • description - of type String
  • price - of type double

The description stores the type of pizza (such as sausage and onion). Include a constructor that requires arguments for both fields and a method named display to display the data. For example, if the description is 'sausage and onion' and the price is '14.99', the display method should output:

sausage and onion pizza Price: $14.99

Create a subclass named DeliveryPizza that inherits from Pizza but adds the following data fields:

  • deliveryFee - of type double
  • address - of type String

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Pizza {

    protected String description;
    protected double price;

    public Pizza(String description, double price) {
        this.description = description;
        this.price = price;
    }

    public void display() {
        System.out.printf("%s pizza Price: $%.2f\n", description, price);
    }
}

class DeliveryPizza extends Pizza {

    private double deliveryFee;
    private String address;

    public DeliveryPizza(String description, double price, String address) {
        super(description, price);
        this.address = address;
        deliveryFee = 3;
        if (price > 15) deliveryFee = 5;
    }

    public void display() {
        System.out.printf("%s pizza Price: $%.2f with additional delivery fee of $%.2f will be delivered to %s\n", description, price, deliveryFee, address);
    }

}

class DemoPizzas {

    public static void main(String[] args) {
        DeliveryPizza pizza = new DeliveryPizza("sausage and onion", 14.99, "address 1");
        pizza.display();
    }
}
Add a comment
Know the answer?
Add Answer to:
Create a class named Pizzawith the following data fields: description - of type String price -...
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
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