Question

In: Computer Science

Practice object-oriented principles by making two Peanut Butter and Jelly Sandwiches. The program must create two...

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

Solutions

Expert Solution

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)


Related Solutions

Americans love to eat peanut butter & jelly sandwiches. If the price of jelly increases (while...
Americans love to eat peanut butter & jelly sandwiches. If the price of jelly increases (while other things remain constant), what effect will this have on the market for peanut butter? Price rises and quantity remains the same Price rises, quantity falls (INCORRECT) Both price and quantity rise Bothe price and quantity fall               Price falls, quantity rises
3. Peanut Butter and Jelly - Consider the case of a consumer that buys peanut butter...
3. Peanut Butter and Jelly - Consider the case of a consumer that buys peanut butter in six-packs. Use the information that follows to calculate the appropriate elasticities of demand. a. When the price of a six-pack increased from $18 to $22, the number of six-packs bought decreased from 30 to 20. What is the consumer's price elasticity of demand in this case? How did the consumer's total spending on peanut butter change as a result of this? (Spending by...
This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
Jane’s utility function over jelly, J, and peanut butter, N, isU = JN. The price...
Jane’s utility function over jelly, J, and peanut butter, N, isU = JN. The price of a jar of jelly is $5. The price of a jar of peanut butter is $10. Jane has a budget of $100 to allocate to these two items. If Jane maximizes her utility, how much of each good does she consume?J* = 5 jars, N* = 5 jars.J* = 10 jars, N* = 10 jars.J* = 10 jars, N* = 0 jars.J* = 5...
Problem Write a movie management system using object-oriented design principles. The program will read from the...
Problem Write a movie management system using object-oriented design principles. The program will read from the supplied data file into a single array list. The data file (movies.txt) contains information about the movies. Each movie record has the following attributes: - Duration (in minutes) - Title - Year of release Each record in the movies.txt file is formatted as follows: - Duration,Title,Year - e.g.: 91,Gravity,2013 Specifically, you have to create an interactive menu driven application that gives the user the...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer information in the list and to search for a customer by specifying the customer’s ID. Sample Run Customer Information Management System --------------------------------------------------------------- CUSTOMER DATA ENTRY: Full Name (First, Last): Jenny Ha Company: Convergent Laser Technologies Street: 1000 International Ave City: Oakland State: CA Zip Code: 94506 ID: 100 Continue Your Data Entry? (y/n): y Full Name (First, Last): Bill Martinez Company: Cisco Systems Street:...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
Keep-or-Drop: Traditional Versus Activity-Based Analysis Nutterco, Inc., produces two types of nut butter: peanut butter and...
Keep-or-Drop: Traditional Versus Activity-Based Analysis Nutterco, Inc., produces two types of nut butter: peanut butter and cashew butter. Of the two, peanut butter is the more popular. Cashew butter is a specialty line using smaller jars and fewer jars per case. Data concerning the two products follow: Peanut Butter Cashew Butter Unused Capacity (a) units of Purchase(b) Expected sales (in cases) 50,000 10,000 - - Selling price per case $100 $80 - - Direct labor hours 40,000 10,000 - As...
Keep-or-Drop: Traditional Versus Activity-Based Analysis Nutterco, Inc., produces two types of nut butter: peanut butter and...
Keep-or-Drop: Traditional Versus Activity-Based Analysis Nutterco, Inc., produces two types of nut butter: peanut butter and cashew butter. Of the two, peanut butter is the more popular. Cashew butter is a specialty line using smaller jars and fewer jars per case. Data concerning the two products follow: Peanut Butter Cashew Butter Unused Capacitya units of Purchaseb Expected sales (in cases) 50,000 10,000 - - Selling price per case $100 $80 - - Direct labor hours 40,000 10,000 - As needed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT