Write a class named TestPizza (a driver class) with the main method. This class
ID: 3581889 • Letter: W
Question
Write a class named TestPizza (a driver class) with the main method. This class instantiates a myPizza object from the Pizza class how do i do this ith the code below?
Class Pizza{
private String topping;
private int diameter;
private double price;
public String getTopping(){
return topping;
}
public int getDiameter(){
return diameter;
}
public double getPrice(){
return price;
}
public void setTopping(String newTopping){
topping=newTopping;
}
public void setDiameter(int newDiameter){
diameter=newDiameter;
}
public void setPrice(double newPrice){
price=newPrice;
}
public Pizza(String newTopping, int newDiameter, double Price){
this.topping=newTopping;
this.diameter=newDiameter;
this.price=newPrice;
}
}
class Testpizza{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
String topping=s.nextLine();
int diameter=s.nextInt();
double price=s.nextDouble();
Pizza myPizza=new Pizza(topping, diameter, price);
System.out.println("My Pizza topping is"+myPizza.getTopping());
System.out.println("My Pizza diameter is"+ myPizza.getDiameter());
System.out.println("My Pizza Price is"+myPizza.getPrice());
}
}
Explanation / Answer
Pizza.java
public class Pizza {
//Declaring instance variables
private String topping;
private int diameter;
private double price;
//Parameterized constructor
public Pizza(String newTopping, int newDiameter, double newPrice){
this.topping=newTopping;
this.diameter=newDiameter;
this.price=newPrice;
}
//Setters and getters
public String getTopping(){
return topping;
}
public int getDiameter(){
return diameter;
}
public double getPrice(){
return price;
}
public void setTopping(String newTopping){
topping=newTopping;
}
public void setDiameter(int newDiameter){
diameter=newDiameter;
}
public void setPrice(double newPrice){
price=newPrice;
}
}
____________________________
Testpizza.java
import java.util.Scanner;
public class Testpizza {
public static void main(String[] args) {
//Scanner class object is used to read the input entered by the user
Scanner s=new Scanner(System.in);
//Getting the pizza topping entered by the user
System.out.print("Enter Pizza Topping :");
String topping=s.nextLine();
//Getting the pizza diameter entered by the user
System.out.print("Enter Pizza Diameter :");
int diameter=s.nextInt();
//getting the pizza price diameter entered by the user
System.out.print("Enter Price :$");
double price=s.nextDouble();
//Creating the Pizza class object by passing the inputs as arguments
Pizza myPizza=new Pizza(topping, diameter, price);
//Displaying the pizza info
System.out.println("My Pizza topping is "+myPizza.getTopping());
System.out.println("My Pizza diameter is "+ myPizza.getDiameter());
System.out.println("My Pizza Price is $"+myPizza.getPrice());
}
}
_______________________________
Output:
Enter Pizza Topping :Jalapeno
Enter Pizza Diameter :5
Enter Price :$5.5
My Pizza topping is Jalapeno
My Pizza diameter is 5
My Pizza Price is $5.5
_______Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.