In: Computer Science
Practice object-oriented principles by making two Peanut Butter and Jelly Sandwiches. The program must create two sandwiches based on user input. The sandwich information for both must then print out their details and determine if the two sandwiches are equal.
Requirements:
Write a class called Bread with the following
o Calories: The number of calories per slice assumed to be between 50 and 250 inclusively.
Write a class called PeanutButter with the following
Write a class called Jelly with the following
Write a class called PBJSandwich with the following
Write a class called PBJFrontEnd with the following
Example Output:
-----------------------------------
Welcome to the PBJ Sandwich Maker!
-----------------------------------
-----Sandwich 1-----
Top Slice of Bread Information
Enter name of the bread
Wonder Bread
Enter the number of calories
80
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
whole wheat
Peanut Butter Information
Enter name of the peanut butter
JIF
Enter the number of calories
190
Is it crunchy? Enter "true", or "false"
false
Jelly Information
Enter name of the jelly
Smuckers
Enter the number of calories
150
Enter the type of bread. Must be "Apple", "Blueberry", "Grape", "Strawberry", or "Tomato"
Apple
Bottom Slice of Bread Information
Enter name of the bread
Wonder Bread
Enter the number of calories
120
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
honey Wheat
-----Sandwich 2-----
Top Slice of Bread Information
Enter name of the bread
Pepperidge Farm
Enter the number of calories
100
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
Whole grain
Peanut Butter Information
Enter name of the peanut butter
Peter Pan
Enter the number of calories
180
Is it crunchy? Enter "true", or "false"
true
Jelly Information
Enter name of the jelly
Welch's
Enter the number of calories
150
Enter the type of bread. Must be "Apple", "Blueberry", "Grape", "Strawberry", or "Tomato"
Grape
Bottom Slice of Bread Information
Enter name of the bread
Lender's
Enter the number of calories
150
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
Whole Wheat
-----Sandwich 1-----
PBJ Sandwich
Top Slice:
Bread
Name: Wonder Bread
Calories: 80
Type: whole wheat
Peanut Butter:
Peanut Butter
Name: JIF
Calories: 190
Is Crunchy: true
Jelly:
Jelly
Name: Smuckers
Calories: 100
Fruit Type: Apple
Bottom Slice:
Bread
Name: Wonder Bread
Calories: 120
Type: honey Wheat
-----Sandwich 2-----
PBJ Sandwich
Top Slice:
Bread
Name: Pepperidge Farm
Calories: 100
Type: Whole grain
Peanut Butter:
Peanut Butter
Name: Peter Pan
Calories: 180
Is Crunchy: true
Jelly:
Jelly
Name: Welch's
Calories: 100
Fruit Type: Grape
Bottom Slice:
Bread
Name: Lender's
Calories: 150
Type: Whole Wheat
Are they the same sandwich? false
Example Output 2:
-----------------------------------
Welcome to the PBJ Sandwich Maker!
-----------------------------------
-----Sandwich 1-----
Top Slice of Bread Information
Enter name of the bread
Wonder
Enter the number of calories
100
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
White
Peanut Butter Information
Enter name of the peanut butter
JIF
Enter the number of calories
150
Is it crunchy? Enter "true", or "false"
false
Jelly Information
Enter name of the jelly
Welchs
Enter the number of calories
150
Enter the type of bread. Must be "Apple", "Blueberry", "Grape", "Strawberry", or "Tomato"
Grape
Bottom Slice of Bread Information
Enter name of the bread
Wonder
Enter the number of calories
100
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
White
-----Sandwich 2-----
Top Slice of Bread Information
Enter name of the bread
Wonder
Enter the number of calories
100
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
white
Peanut Butter Information
Enter name of the peanut butter
JIF
Enter the number of calories
150
Is it crunchy? Enter "true", or "false"
false
Jelly Information
Enter name of the jelly
Welchs
Enter the number of calories
150
Enter the type of bread. Must be "Apple", "Blueberry", "Grape", "Strawberry", or "Tomato"
grape
Bottom Slice of Bread Information
Enter name of the bread
wonder
Enter the number of calories
100
Enter the type of bread. Must be "Honey Wheat", "White", "Whole Grain", or "Whole Wheat"
white
-----Sandwich 1-----
PBJ Sandwich
Top Slice:
Bread
Name: Wonder
Calories: 100
Type: White
Peanut Butter:
Peanut Butter
Name: JIF
Calories: 150
Is Crunchy: true
Jelly:
Jelly
Name: Welchs
Calories: 100
Fruit Type: Grape
Bottom Slice:
Bread
Name: Wonder
Calories: 100
Type: White
-----Sandwich 2-----
PBJ Sandwich
Top Slice:
Bread
Name: Wonder
Calories: 100
Type: white
Peanut Butter:
Peanut Butter
Name: JIF
Calories: 150
Is Crunchy: true
Jelly:
Jelly
Name: Welchs
Calories: 100
Fruit Type: grape
Bottom Slice:
Bread
Name: wonder
Calories: 100
Type: white
Are they the same sandwich? true
public class Bread {
/* instance variables */
private String name;
private int calories;
private String type;
/* Accessors and Mutators with validation */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
if (calories >= 50 &&
calories <= 250)
this.calories =
calories;
}
public String getType() {
return type;
}
public void setType(String type) {
/* validate the type before setting
the value */
if (type.equalsIgnoreCase("honey
wheat") || type.equalsIgnoreCase("white")
|| type.equalsIgnoreCase("whole grain") ||
type.equalsIgnoreCase("whole wheat")) {
this.type =
type;
}
}
/* checks if two Bread are equal by comparing the
object and their values */
public boolean equals(Bread obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Bread other = obj;
if (calories !=
other.calories)
return
false;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equalsIgnoreCase(other.name))
return
false;
if (type == null) {
if (other.type
!= null)
return false;
} else if
(!type.equalsIgnoreCase(other.type))
return
false;
return true;
}
/* prints the object in a user defined manner
*/
public String toString() {
return "Bread\nName: " + name +
"\nCalories: " + calories + "\nType: " + type + "\n";
}
}
public class PeanutButter {
/* instance variables */
private String name;
private int calories;
private boolean isCrunchy;
/* Accessors and Mutators with validation */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
/* validate the calories before
setting the value */
if (calories >= 100 &&
calories <= 300) {
this.calories =
calories;
}
}
public boolean isCrunchy() {
return isCrunchy;
}
public void setCrunchy(boolean isCrunchy) {
this.isCrunchy = isCrunchy;
}
/* checks if two PeanutButter are equal by
comparing the object and their values */
public boolean equals(PeanutButter obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
PeanutButter other = obj;
if (calories !=
other.calories)
return
false;
if (isCrunchy !=
other.isCrunchy)
return
false;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equalsIgnoreCase(other.name))
return
false;
return true;
}
/* prints the object in a user defined manner
*/
public String toString() {
return "PeanutButter\nName: " +
name + "\nCalories: " + calories + "\nIs Crunchy:" + isCrunchy +
"\n";
}
}
public class Jelly {
/* instance variables */
private String name;
private int calories;
private String fruitType;
/* Accessors and Mutators with validation */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
/* validate the calories before
setting the value */
if (calories >= 50 &&
calories <= 200) {
this.calories =
calories;
}
}
public String getFruitType() {
return fruitType;
}
public void setFruitType(String fruitType) {
/* validate the fruitType before
setting the value */
if
(fruitType.equalsIgnoreCase("apple") ||
fruitType.equalsIgnoreCase("blueberry")
|| fruitType.equalsIgnoreCase("grape") ||
fruitType.equalsIgnoreCase("strawberry")
|| fruitType.equalsIgnoreCase("tomato")) {
this.fruitType =
fruitType;
}
}
/* checks if two Jelly are equal by comparing the
object and their values */
public boolean equals(Jelly obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Jelly other = obj;
if (calories !=
other.calories)
return
false;
if (fruitType == null) {
if
(other.fruitType != null)
return false;
} else if
(!fruitType.equalsIgnoreCase(other.fruitType))
return
false;
if (name == null) {
if (other.name
!= null)
return false;
} else if
(!name.equalsIgnoreCase(other.name))
return
false;
return true;
}
/* prints the object in a user defined manner
*/
public String toString() {
return "Jelly\nName: " + name +
"\nCalories: " + calories + "\nFruit Type: " + fruitType +
"\n";
}
}
public class PBJSandwich {
/* instance variables */
private Bread topSlice;
private PeanutButter peanutButter;
private Jelly jelly;
private Bread bottomSlice;
/* Accessors and Mutators with validation */
public Bread getTopSlice() {
return topSlice;
}
public void setTopSlice(Bread topSlice) {
this.topSlice = topSlice;
}
public PeanutButter getPeanutButter() {
return peanutButter;
}
public void setPeanutButter(PeanutButter
peanutButter) {
this.peanutButter =
peanutButter;
}
public Jelly getJelly() {
return jelly;
}
public void setJelly(Jelly jelly) {
this.jelly = jelly;
}
public Bread getBottomSlice() {
return bottomSlice;
}
public void setBottomSlice(Bread bottomSlice)
{
this.bottomSlice =
bottomSlice;
}
/* checks if two sandwiches are equal */
public boolean equals(PBJSandwich obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
PBJSandwich other = obj;
if (bottomSlice == null) {
if
(other.bottomSlice != null)
return false;
} else if
(!bottomSlice.equals(other.bottomSlice)) // equals two Bread
return
false;
if (jelly == null) {
if (other.jelly
!= null)
return false;
} else if
(!jelly.equals(other.jelly)) // equals two Jelly
return
false;
if (peanutButter == null) {
if
(other.peanutButter != null)
return false;
} else if
(!peanutButter.equals(other.peanutButter)) // equals two
PeanutButter
return
false;
if (topSlice == null) {
if
(other.topSlice != null)
return false;
} else if
(!topSlice.equals(other.topSlice)) // equals two Bread
return
false;
return true;
}
/* prints the object in a user defined manner
*/
public String toString() {
return "PBJSandwich\nTop Slice:\n"
+ topSlice + "Peanut Butter:\n" + peanutButter + "Jelly:\n" +
jelly
+ "Bottom Slice:\n" + bottomSlice + "\n";
}
}
Main class to test:
import java.util.Scanner;
public class PBJFrontEnd {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);// to take user input
/* Create two PBJSandwich objects
*/
PBJSandwich pbj1 = new
PBJSandwich();
PBJSandwich pbj2 = new
PBJSandwich();
System.out.println("--------------------------------\n"
+ "Welcome to PBJ Sandwich
Maker\n--------------------------------\n");
/*
* running loop twice to get the
values entered by user for 2 PBJSandwiches,
* here we are taking user inputs
then creating a Bread, PeanutButter and Jelly
* object setting their values using
Mutators and then finally setting values
* for PBJSandwich
*/
for (int i = 1; i <= 2; i++)
{
PBJSandwich pbj
= new PBJSandwich();
System.out.println("-----Sandwich " + i + "-----");
System.out.println("Top Slice of Bread Information");
System.out.println("Enter name of the bread");
String name =
sc.nextLine();
System.out.println("Enter the number of calories");
int calories =
sc.nextInt();
/*
* this is done
since nextInt() does not consumes the line after the number
* entered
*/
sc.nextLine();
System.out.println(
"Enter the type of bread.
Must be \"Honey Wheat\", \"White\", \"Whole Grain\", or \"Whole
Wheat\"");
String type =
sc.nextLine();
Bread topSlice =
new Bread();
topSlice.setName(name);
topSlice.setCalories(calories);
topSlice.setType(type);
System.out.println("Peanut Butter Information");
System.out.println("Enter the name of peanut butter");
String pName =
sc.nextLine();
System.out.println("Enter the number of calories");
int pCalories =
sc.nextInt();
sc.nextLine();
System.out.println("Is it crunchy? Enter \"true\", or
\"false\"");
boolean pCrucnhy
= sc.nextBoolean();
/*
* this is done
since nextBoolean() does not consumes the line after the
boolean
* entered
*/
sc.nextLine();
PeanutButter pb
= new PeanutButter();
pb.setName(pName);
pb.setCrunchy(pCrucnhy);
pb.setCalories(pCalories);
System.out.println("Jelly Information");
System.out.println("Enter name of the jelly");
String jellyName
= sc.nextLine();
System.out.println("Enter the number of calories");
int jCalories =
sc.nextInt();
sc.nextLine();
System.out.println(
"Enter the fruit type of
Jelly. Must be \"Apple\", \"Blueberry\", \"Grape\", \"Strawberry\",
or \"Tomato\"");
String fruitType
= sc.nextLine();
Jelly j = new
Jelly();
j.setCalories(jCalories);
j.setFruitType(fruitType);
j.setName(jellyName);
System.out.println("Bottom Slice Bread Information");
System.out.println("Enter name of the bread");
String
bottomBreadName = sc.nextLine();
System.out.println("Enter the number of calories");
int
bottomCalories = sc.nextInt();
sc.nextLine();
System.out.println(
"Enter the type of bread.
Must be \"Honey Wheat\", \"White\", \"Whole Grain\", or \"Whole
Wheat\"");
String
bottomType = sc.nextLine();
Bread
bottomSlice = new Bread();
bottomSlice.setName(bottomBreadName);
bottomSlice.setCalories(bottomCalories);
bottomSlice.setType(bottomType);
pbj.setTopSlice(topSlice);
pbj.setBottomSlice(bottomSlice);
pbj.setJelly(j);
pbj.setPeanutButter(pb);
/* for first
iteration it should be pbj1 else pbj2 */
if (i ==
1)
pbj1 = pbj;
else
pbj2 = pbj;
}
System.out.println("-----Sandwich
1-----\n" + pbj1);// internally uses toString() method to
print
System.out.println("-----Sandwich
2-----\n" + pbj2);// internally uses toString() method to
print
System.out.println("Are they the
same sandwich? " + pbj1.equals(pbj2));
sc.close();// close the scanner
object
}
}
output when you run above class:(I've used your first example, please test by yourself for the second)