COP 2800, Java Programming Write Java programs meeting the following requirement
ID: 3703621 • Letter: C
Question
COP 2800, Java Programming
Write Java programs meeting the following requirements
Create a Cake class and two subclasses SheetCake and NoveltyCake for a cake shop you can name.
The Cake class contains fields for the flavor of the cake, the cost, and a static variable that contains the slogan of the shop. You set the cost and come up with the slogan.
The SheetCake has additional fields for size and filling and decoration. The cost of a 1?4 SheetCake is the same as the cake, 1?2 is twice as much, and a whole SheetCake is 4 times as much.
The NoveltyCake has an additional field for type, and costs 5 times as much as Cake.
All classes contain appropriate get and set methods for the private fields
Create a Java program that declares objects of all three types, Cake, SheetCake and NoveltyCake. Allow the user to enter the data for each cake and display the fields of each object, plus the slogan.
At the beginning of the program add appropriate comments as in previous assignments.
Purpose: Create a java program from detailed directions. Ability to:
Follow established programming standards for writing java programs Use inheritance to create subclasses
Create objects from all classes
Use data field encapsulation
Using private access to the fields of the classes Use getter and setter methods
Use a static variable
Use the print statement for output
Put your <<name of class>>.java file, pictures of code, proof of compile and output in a zipped file
Explanation / Answer
import java.util.Scanner;
class Cake {
private String flavor; // Declaring the variables
private double cost;
private static String slogan="Cake we bake";
public void Cake() { //taking the the book data from user and initializing the variables
this.cost=450.50;
}
public String getFlavor() {
return this.flavor;
}
public double getCost() {
return this.cost;
}
public void setFlavor(String flavor) {
this.flavor=flavor;
}
public void setCost(double cost) {
this.cost=cost;
}
public String getSlogan() {
return slogan;
}
public void setSlogan(String slogan1) {
slogan=slogan1;
}
};
class SheetCake extends Cake {
private int size;
private String filling;
private String decoration;
public SheetCake() {
super.Cake();
this.setCost(4*this.getCost());
}
public int getSize() {
return this.size;
}
public String getFilling() {
return this.filling;
}
public String getDecoration() {
return this.decoration;
}
public void setSize(int size) {
this.size=size;
}
public void setFilling(String filling) {
this.filling=filling;
}
public void setDecoration(String decoration) {
this.decoration=decoration;
}
};
class NoveltyCake extends Cake{
private String type;
public NoveltyCake() {
super.Cake();
this.setCost(5*this.getCost());
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type=type;
}
};
public class CakeTest() {
public static void main(String[] args) {
Cake C1=new Cake();
SheetCake S1=new SheetCake();
NoveltyCake N1=new NoveltyCake();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Flavor for the Cake");
String flavor=sc.next();
C1.setFlavor(flavor);
System.out.println("Cost of Cake is "+C1.getCost());
System.out.println("Flavor of Cake is "+C1.getFlavor());
System.out.println("Slogan is "+C1.getSlogan());
Scanner sc3=new Scanner(System.in);
System.out.println("Enter the Flavor for the Sheet Cake");
String flavor1=sc3.next();
S1.setFlavor(flavor1);
Scanner sc1=new Scanner(System.in);
System.out.println("Enter the Size for the Sheet Cake");
int size=sc1.nextInt();
S1.setSize(size);
Scanner sc2=new Scanner(System.in);
System.out.println("Enter the Filling for the Sheet Cake");
String filling=sc2.next();
S1.setFilling(filling);
Scanner sc4=new Scanner(System.in);
System.out.println("Enter the Decoration for the Sheet Cake");
String decor=sc4.next();
S1.setDecoration(decor);
System.out.println("Cost of Sheet Cake is "+S1.getCost());
System.out.println("Flavor of Sheet Cake is "+S1.getFlavor());
System.out.println("Size is "+S1.getSize());
System.out.println("filling is "+S1.getFilling());
System.out.println("Decoration is "+S1.getDecoration());
System.out.println("Slogan is "+S1.getSlogan());
Scanner sc5=new Scanner(System.in);
System.out.println("Enter the Flavor for the Novelty Cake");
String flavor2=sc5.next();
N1.setFlavor(flavor2);
Scanner sc6=new Scanner(System.in);
System.out.println("Enter the Type for the Sheet Cake");
String type=sc3.next();
N1.setType(type);
System.out.println("Flavor for the Novelty Cake is "+S1.getSlogan());
System.out.println("Type of the Novelty Cake is "+S1.getSlogan());
System.out.println("Slogan is "+S1.getSlogan());
}
}