1 Create a class called Sandwich It should have the followin
1. Create a class called Sandwich. It should have the following instance variables: Name of Instance variable Description of instance variable meat The kind of meat in the sandwich, for example “ham” or “none” numOfSlicesOfTomato Number of tomato slices in sandwich lettuce Do you want lettuce in your sandwich? This is a new type of instance variable. It can only be true or false. How to declare it: private boolean lettuce; 2. Create a no-arg constructor for Sandwich. Inside the constructor, initialize the instance variables: meat should be “turkey” numOfSlicesOfTomato should be 3 lettuce should be true (lettuce = true) 3. Create a three parameter constructor for Sandwich. The Constructor declaration will look like: public Sandwich(String inMeat, int inNumOfSlicesOfTomato, boolean inLettuce) { //what code goes here? } 4. Create getters and setters for all instance variables. 5. Create the printData method that will System.out.print (SOP) each of the instance variables. 6. Create a method that will calculate the price of the sandwich. A regular sandwich with no tomatoes is $3. There is a 25 cent extra charge for each tomato purchased. a. Name the method: calculatePrice b. Number of parameters: none c. Return: double d. Method body: calculate and return the price of the sandwich using the description above. Part II – SandwichTester.java 1. Create a class called SandwichTester. 2. It should have a main method in it 3. Inside the main method a. Create a Sandwich object using your zero parameter constructor and call it sandwich1. Do all of the following for sandwich1: Use your setter methods for all three instance variables to change the values for sandwich1. Call the printData method on sandwich1. Call the calculatePrice method on sandwich1 and SOP the results. b. Create a Sandwich object using your three parameter constructor and call it sandwich2. Do all of the following for sandwich2 Use valid and correct values to create sandwich2 using three parameter constructor. Call the printData method on sandwich2. Call the calculatePrice method on sandwich2 and SOP the results. b. Use the Scanner class to i. Ask the user for type of meat. Store this in a local variable called meat. ii. Ask the user for number of tomatoes. Store this in a local variable called numTomatoes. iii. Create a Sandwich object using your no-arg constructor and call it sandwich3. iv. Use the setMeat method to set the type of meat to whatever the user typed in v. Use the setNumTomatoes method to set the number of tomatoes based upon what the user typed in vi. Call the printData method on sandwich3. 4. Compile, Save, and Run the SandwichTester class. If it works correctly, you should see a printout of 3 sandwiches, each containing a meat, a number of slices of tomato, and whether or not lettuce is included when you run the program. Lettuce will be true or false.
Solution
Follow these step:
1. First Create a package (i.e.folder) and name as Chegg.
2. Create a java file and name as Sandwich.java inside the Chegg package(i.e.folder)
3. copy the following code and paste it into Sandwich.java file
package Chegg;
public class Sandwich {
private String meat;
private int numOfSlicesOfTomato;
private boolean lettuce;
public Sandwich() {
this.meat = \"turkey\";
this.numOfSlicesOfTomato = 3;
this.lettuce = true;
}
public Sandwich(String inMeat, int inNumOfSlicesOfTomato, boolean inLettuce) {
this.meat = inMeat;
this.numOfSlicesOfTomato = inNumOfSlicesOfTomato;
this.lettuce = inLettuce;
}
public String getMeat() {
return meat;
}
public void setMeat(String meat) {
this.meat = meat;
}
public int getNumOfSlicesOfTomato() {
return numOfSlicesOfTomato;
}
public void setNumOfSlicesOfTomato(int numOfSlicesOfTomato) {
this.numOfSlicesOfTomato = numOfSlicesOfTomato;
}
public boolean isLettuce() {
return lettuce;
}
public void setLettuce(boolean lettuce) {
this.lettuce = lettuce;
}
public void printData() {
System.out.println(\"Meat: \" + meat);
System.out.println(\"Num Of Slices Of Tomato: \" + numOfSlicesOfTomato);
System.out.println(\"Lettuce: \" + lettuce);
}
public double calculatePrice() {
double price = 0.0;
price = 3 + (((double)25 / 100 * 3) * numOfSlicesOfTomato);
return price;
}
}
4.Create a java file and name as SandwichTester.java inside the Chegg package(i.e.folder)
5. Copy the following code paste it into SandwichTester.java file
package Chegg;
import java.util.Scanner;
public class SandwichTester {
public static void main(String[] args) {
String meat = null;
int numTomatoes = 0;
/* Start: Question Part II , part 3a */
Sandwich sandwich1 = new Sandwich();
sandwich1.setMeat(\"ham\");
sandwich1.setNumOfSlicesOfTomato(2);
sandwich1.setLettuce(true);
sandwich1.printData();
System.out.println(\"The price of Sandwich1 is : $\" + sandwich1.calculatePrice());
/* End: Question 2, part 3a */
/* Start: Question 2, part 3b */
Sandwich sandwich2 = new Sandwich(\"none\", 1, false);
sandwich2.printData();
System.out.println(\"The price of Sandwich2 is : $\" + sandwich2.calculatePrice());
/* End: Question 2, part 3b */
/* Start: Last Question solution as the question numbering was incorrect*/
Scanner myScanner = new Scanner(System.in);
System.out.println(\"Enter the type of meat\");
meat = myScanner.next();
System.out.println(\"Enter the number of tomatoes\");
numTomatoes = myScanner.nextInt();
Sandwich sandwich3 = new Sandwich();
sandwich3.setMeat(meat);
sandwich3.setNumOfSlicesOfTomato(numTomatoes);
sandwich3.printData();
System.out.println(\"The price of Sandwich3 is : $\" + sandwich3.calculatePrice());
/* End: Last Question solution as the question numbering was incorrect*/
}
}
6. Now Compile the code and run


