A. Create a class named pizza. Data fields include a String for toppings (such a
ID: 3647364 • Letter: A
Question
A. Create a class named pizza. Data fields include a String for toppings (such as pepperoni), an integer for diameter in inches (such as 12), and a double for price (such as 13.99). Include methods to get and set values for each of these fields. In the Pizza class, add a constructor that initially sets the toppings to "Black Olives," the diameter to 16, and the price to 19.99.B. Create an application named TestPizza that instantiates one Pizza object and demonstrates the use of the Pizza set and get methods. Add a secondary pizza object that demonstrates that the constructor works by displaying the Pizza's initial values.
Explanation / Answer
public Class Pizza { String topping = ""; int diameter = 0; double price = 0.00; public Pizza() { // default constructor. } // end constructor. public String getTopping() { return topping; } // end method. public int getDiameter() { return diameter; } // end method. public double getPrice() { return price; } // end method. public void setTopping(String topping) { this.topping = topping; } // end method. public void setDiameter(int diameter) { this.diameter = diameter; } // end method. public void setPrice(double price) { this.price = price; } // end method. } // end class. B) public Class TestPizza { public static void main(String[] args) { Pizza p = new Pizza(); p.setTopping("Cheese & Tomato"); p.setDiameter(10); p.setPrice(5.00); System.out.println("Topping: " + p.getTopping()); System.out.println("Diameter: " + p.getDiameter()); System.out.println("Price: " + p.getPrice()); } // end main. } // end class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.