Question

In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and...

In JAVA write the following program:

Objective:

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

  • Instance Variables
    • Name: The name brand of the bread.

o   Calories: The number of calories per slice assumed to be between 50 and 250 inclusively.

  • Type: The kind of bread. This can either be “Honey Wheat”, “White”, “Whole Grain”, or “Whole Wheat”.
  • Methods
    • Accessors and Mutators for the instance variables
      • Make sure in the mutators check for valid values named in the Instance Variable Section.
    • Equals: This method takes in another instance of Bread and only returns true if all of the instance variables match. For name and type case should be ignored.
    • ToString: This method returns a string with all of the instance variable values.

Write a class called PeanutButter with the following

  • Instance Variables
    • Name: The name brand of peanut butter.
    • Calories: The number of calories per serving assumed to be between 100 and 300 inclusively.
    • IsCrunchy: True if the peanut butter is crunchy and false otherwise.
  • Methods
    • Accessors and Mutators for the instance variables
      • Make sure in the mutators check for valid values named in the Instance Variable Section.
    • Equals: This method takes in another instance of Peanut Butter and only returns true if all of the instance variables match. For name case should be ignored.
    • ToString: This method returns a string with all of the instance variable values.

Write a class called Jelly with the following

  • Instance Variables
    • Name: The brand name of the Jelly
    • Calories: The number of calories per serving assumed to be between 50 and 200 inclusively
    • FruitType: The type of fruit the jelly is made from and can only be “Apple”, “Blueberry”, “Grape”, “Strawberry”, or “Tomato”.
  • Methods
    • Accessors and Mutators for the instance variables
      • Make sure in the mutators check for valid values named in the Instance Variable Section.
    • Equals: This method takes in another instance of Jelly and only returns true if all of the instance variables match. For name and fruit type case should be ignored.
    • ToString: This method returns a string with all of the instance variable values.

Write a class called PBJSandwich with the following

  • Instance Variables
    • TopSlice: An instance of type Bread representing the top slice of bread.
    • PeanutButter: An instance of type PeanutButter representing the peanut butter.
    • Jelly: An instance of type Jelly representing the jelly.
    • BottomSlice: An instance of type Bread representing the bottom slice of bread.
  • Methods
    • Accessors and Mutators for the instance variables
      • Make sure in the mutators check for valid values named in the Instance Variable Section.
    • Equals: This method takes in another instance of PBJSandwich and only returns true if all of the instance variables match by calling their Equals methods.
    • ToString: This method returns a string with all of the instance variable values.

Write a class called PBJFrontEnd with the following

  • Methods
    • Main Method
      • Create Two instances of PBJSandwich
      • User enters all the properties of the sandwiches
      • Prints out the values of the two sandwiches
      • Determines if they are the same sandwich

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

Homework Answers

Answer #1

import java.util.Scanner;

// ------------------------------- Bread.java ------------------------------- //

class Bread{

// @_ All required fields

private String Name;
private int Calories; // 50 - 250
private String Type;

// --- All mutators & Accessors

public String getName() {
return Name;
}

public void setName(String name) {

Name = name;
}

public int getCalories() {
return Calories;
}

public void setCalories(int calories) {

// @ - Check validation rule that number of calories must be between 50 and 250
// @ - If not within given range, assign 50 to calories

if(calories >= 50 && calories <= 250)
Calories = calories;
else
Calories = 50;
}

public String getType() {
return Type;
}

public void setType(String type) {
// Type must be Honey Wheat, white, Whole Grain, or Whole Wheat, if not, assign default "Honey Wheat"

if(type.equalsIgnoreCase("Honey Wheat") || type.equalsIgnoreCase("White") ||
type.equalsIgnoreCase("Whole Grain") || type.equalsIgnoreCase("Whole Wheat"))

Type = type;

else

Type = "Honey Wheat";
}

// @ - The below method check whether every instance of a Bread matches with every instance of passed Bread object
// @ - For name & type, ignoring case

public boolean Equals(Bread second){

return (this.Name.equalsIgnoreCase(second.Name) && this.Type.equalsIgnoreCase(second.Type) && this.Calories == second.Calories);
}

public String ToString() {
return "Bread"+
"\nName: "+Name+
"\nCalories: "+Calories+
"\nType: "+Type;
}
}

// ------------------------------- PeanutButter.java ------------------------------- //

class PeanutButter{

private String Name;
private int Calories; // 100 - 300
private boolean IsCrunchy;

// All Accessors & Mutators

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public int getCalories() {
return Calories;
}

public void setCalories(int calories) {

// @ - Check validation rule that number of calories must be between 100 - 300
// @ - If not within given range, assign 100 to calories

if(calories >= 100 && calories <= 300)
Calories = calories;
else
Calories = 100;
}

public boolean isCrunchy() {
return IsCrunchy;
}

public void setCrunchy(boolean crunchy) {
IsCrunchy = crunchy;
}

public boolean Equals(PeanutButter second){

return (this.Name.equalsIgnoreCase(second.Name) && this.Calories == second.Calories && this.IsCrunchy == second.IsCrunchy);
}

// -- Other methods as specified in statement

public String ToString() {
return "Peanut Butter"+
"\nName: "+Name+
"\nCalories: "+Calories+
"\nIs Crunchy: "+IsCrunchy;
}
}

// ------------------------------- Jelly.java ------------------------------- //

class Jelly{

private String Name;
private int Calories; // 50 - 200
private String FruitType;

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public int getCalories() {
return Calories;
}

public void setCalories(int calories) {
// @ - Check validation rule that number of calories must be between 50 - 200
// @ - If not within given range, assign 50 to calories

if(calories >= 50 && calories <= 200)
Calories = calories;
else
Calories = 50;
}

public String getFruitType() {
return FruitType;
}

public void setFruitType(String fruitType) {

// -- Fruit type must be apple, blueberry, strawberry, Grape or tomato, if not any one of these types assign apple

if(fruitType.equalsIgnoreCase("Apple") || fruitType.equalsIgnoreCase("Blueberyy") ||
fruitType.equalsIgnoreCase("StrawBerry") || fruitType.equalsIgnoreCase("Grape") ||
fruitType.equalsIgnoreCase("Tomato"))

FruitType = fruitType;

else

FruitType = "Apple";
}

public boolean Equals(Jelly second){

return (this.Name.equalsIgnoreCase(second.Name) && this.Calories == second.Calories && this.FruitType.equalsIgnoreCase(second.FruitType));
}

// -- Other methods as specified in statement

public String ToString() {
return "Jelly"+
"\nName: "+Name+
"\nCalories: "+Calories+
"\nFruit Type: "+FruitType;
}
}

// ------------------------------- PBJSandwich.java ------------------------------- //

class PBJSandwich{

// --- All required instance variables as mentioned in statement

private Bread topSlice;
private PeanutButter peanutButter;
private Jelly jelly;
private Bread bottomSlice;

public PBJSandwich(){ // constructor initializing fields aebove

this.topSlice = new Bread();
this.peanutButter = new PeanutButter();
this.jelly = new Jelly();
this.bottomSlice = new Bread();
}

// --- All mutators & Accessors

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;
}

// The Equals method will check that whether the passed PBJSandwich object matches with this one or not

public boolean Equals(PBJSandwich second){

return (this.topSlice.Equals(second.topSlice) && this.peanutButter.Equals(second.peanutButter)
&& this.jelly.Equals(second.jelly) && bottomSlice.Equals(second.bottomSlice));
}

// TOString() will return all (Information) Strings of above instance variables

public String toString(){

// -- Just calling their ToString() methods

return topSlice.ToString() + peanutButter.ToString()+ jelly.ToString() + bottomSlice.ToString();
}
}

// ------------------------------- PBJFrontEnd.java ------------------------------- //

public class PBJFrontEnd {

public static void main(String[] args) {

System.out.println("--------------------------------------\n" +
"Welcome to the PBJ Sandwich Maker\n"+
"--------------------------------------");

// -- Creating two instances of PBJSandwich

PBJSandwich sandwich1 = new PBJSandwich();
PBJSandwich sandwich2 = new PBJSandwich();

// -- Entering information of Sandwiches from user

System.out.println("----- Sandwich 1 -----");
enterInformation(sandwich1);

System.out.println("----- Sandwich 2 -----");
enterInformation(sandwich2);

// Then showing information

System.out.println("----- Sandwich 1 -----");
showInformation(sandwich1);

System.out.println("----- Sandwich 2 -----");
showInformation(sandwich2);

// -- checking for equality

System.out.print("\nAre they the same sandwich? ");

if(sandwich1.Equals(sandwich2))
System.out.println("true");
else
System.out.println("false");
}

// --- A function to prompt user to enter information for sandwich & according to that set these things i.e Jelly

public static void enterInformation(PBJSandwich sandwich){

Scanner input = new Scanner(System.in);

// --------------------- Top Slice of Bread Information -------------------- //

System.out.println("Top Slice of Bread Information");

System.out.println("Enter name of the Bread ");
String b1Name = input.nextLine();

System.out.println("Enter the number of calories ");
int cal1 = input.nextInt();

System.out.println("Enter the type of bread. Must be \"Honey Wheat\", \"White\", \"Whole Grain\", or \"Whole Wheat\"");
input.nextLine();
String b1Type = input.nextLine();

sandwich.getTopSlice().setName(b1Name);
sandwich.getTopSlice().setCalories(cal1);
sandwich.getTopSlice().setType(b1Type);

// --------------------- Peanut Butter Information -------------------- //

System.out.println("Peanut Butter Information");

System.out.println("Enter name of the Peanut Butter ");
String pName = input.nextLine();

System.out.println("Enter the number of calories ");
int pCal = input.nextInt();

System.out.println("Is it crunchy? Enter \"true\", or \"false\" ");
input.nextLine();
String isCrunchy = input.nextLine();

sandwich.getPeanutButter().setName(pName);
sandwich.getPeanutButter().setCalories(pCal);

if(isCrunchy.equalsIgnoreCase("true"))

sandwich.getPeanutButter().setCrunchy(true);

else
sandwich.getPeanutButter().setCrunchy(false);

// --------------------- Jelly Information -------------------- //

System.out.println("Jelly Information");

System.out.println("Enter name of the Jelly ");
String jName = input.nextLine();

System.out.println("Enter the number of calories ");
int jCal = input.nextInt();

System.out.println("Enter the type of Fruit. Must be \"Apple\", \"Blueberry\", \"Grape\", \"Strawberry\", or \"Tomato\"");
input.nextLine();
String jFruit = input.nextLine();

sandwich.getJelly().setName(jName);
sandwich.getJelly().setCalories(jCal);
sandwich.getJelly().setFruitType(jFruit);

// --------------------- Bottom Slice of Bread Information -------------------- //

System.out.println("Bottom Slice of Bread Information");

System.out.println("Enter name of the Bread ");
String b2Name = input.nextLine();

System.out.println("Enter the number of calories ");
int cal2 = input.nextInt();

System.out.println("Enter the type of bread. Must be \"Honey Wheat\", \"White\", \"Whole Grain\", or \"Whole Wheat\"");
input.nextLine();
String b2Type = input.nextLine();

sandwich.getBottomSlice().setName(b2Name);
sandwich.getBottomSlice().setCalories(cal2);
sandwich.getBottomSlice().setType(b2Type);
}

// -- A method for showing information of sandwich

public static void showInformation(PBJSandwich sandwich){

System.out.println("PBJ Sandwich");

System.out.println("Top Slice:");
System.out.println(sandwich.getTopSlice().ToString());

System.out.println("Peanut Butter:");
System.out.println(sandwich.getPeanutButter().ToString());

System.out.println("Jelly:");
System.out.println(sandwich.getJelly().ToString());

System.out.println("Bottom Slice:");
System.out.println(sandwich.getBottomSlice().ToString());
}
}

Sample output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions