1. Design a class named pizza. Data fields include a string field for toppings (
ID: 3633439 • Letter: 1
Question
1. Design a class named pizza. Data fields include a string field for toppings (such as pepperoni) and numeric fields for diameter in inches (such as 12) and price (such as 13.99). Include methods to get and set values for each of these fields. Create the class diagram and write the pseudocode that defines the class.
2. Design an application program that declares two pizza objects and sets and displays their values.
3. Design an application program that declares an array of 10 pizzas. Prompt the user for data for each of the pizzas, and then display the values.
Explanation / Answer
public class Pizza {
private String type;
private int size;
private double price;
public Pizza(){}
public Pizza(String t,int s,double p){
type=t;
size=s;
price=p;
}
public void setType(String t){
type=t;
}
public void setSize(int s){
size=s;
}
public void setPrice(double p){
price=p;
}
public String getType(){
return type;
}
public int getSize(){
return size;
}
public double getPrice(){
return price;
}
public void display(){
System.out.println("pizza type: "+type+" and size = "+size+" cost = "+price);
}
}
class Pizza{
//declare any thing for the pizza like the size or type or both
String size;
String type;
//declare constructors
public Pizza(){}
public Pizza(String s,String t){
size=s;
type=t;
}
// declare methods for sets and display
public void setSize(String s){
size=s;
}
public void setType(String t){
type=t;
}
public void setBoth(String s;String t){
size=s;
type=t;
}
public void display(){
System.out.println("Pizza size = "+size+" of type "+type");
}
}
now go to the main class
import Pizza;
public static void main(int args[]){
Pizza p1=new Pizza();
Pizza p2=new Pizza("Large","b type");
p1.setBoth("Small","c type");
p1.display();
p2.display();
}
now the next q so easy keep the same pizza class the change gonna be only in the main class
import Pizza;
import java.util.Scanner;
public static void main(int args[]){
Scanner data=new Scanner(System.in);
Pizza pa[]=new Pizza[9];
for(int i=0;i<pa.length;i++){
System.out.println("Enter the size of Pizza["+i+"]");
String s=data.nextLine();
System.out.println("Enter the type of Pizza["+i+"]");
String t=data.nextLine();
pa[i]=new Pizza(s,t);
}
for(int i=0;i<pa.length;i++)
pa[i].display();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.