11. A. Create a class named Sandwich. Data fields include a string for the main
ID: 3545595 • Letter: 1
Question
11. A. Create a class named Sandwich. Data fields include a string for the main ingredient (such as "tuna") , a string for bread type (such as "wheat"l , and a double for price (such as 4.99). Include methods to get and set values for each of these fields. Save as Sandwich.java.
B. Create an application named TestSandwich that instantiates one sandwich object and demonstrates the use of the set and get method. Save as TestSandwich.java.
*****Side Note***** This is for Chapter 3 which means the code needs to be wrote out of coding that we have learned in Chapter 1,2,3. So this code needs to be wrote using the things we have learned to this point. It cannot be wrote using stuff that has not yet been learned. Please keep that in mind as I will not award points if it is beyond what we have learned Thank you.
Explanation / Answer
// first class
class Sandwich
{
private String ingredient;
private String bread;
private double price;
public void setIngredient(String i)
{
ingredient = i;
}
public String getIngredient()
{
return ingredient;
}
public void setBread(String b)
{
bread = b;
}
public String getBread()
{
return bread;
}
public void setPrice(double p)
{
price = p;
}
public double getPrice()
{
return price;
}
}
//second class
class TestSandwich
{
public static void main (String args[])
{
Sandwich sandwich = new Sandwich();
sandwich.setIngredient("tuna");
sandwich.setBread("wheat");
sandwich.setPrice(4.95);
System.out.println("Ingredient: " + sandwich.getIngredient());
System.out.println("Bread: " + sandwich.getBread());
System.out.println("Price: " + sandwich.getPrice());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.