I'm working on a college exercise. It requires me to use "getter and setter" methods in the program "Sandwich.java" so that the references in "TestSandwich.java" may use them. Apologies for any confusion.
class Sandwich {
public static void main(String[] args){
}
public class PrimaryIngredient{
private String mainIngredient;
public String getMainIngredient(){
return mainIngredient;
}
public void setMainIngredient(String newMain){
this.mainIngredient = newMain;
}
}
public class Loaf{
private String bread;
public String getBread(){
return bread;
}
public void setBread(String newBread){
this.bread = newBread;
}
}
public class Cost{
private double price;
public double getPrice(){
return price;
}
public void setPrice(double newPrice){
this.price = newPrice;
}
}
}
class TestSandwich
{
public static void main (String args[])
{
Sandwich sandwich = new Sandwich();
sandwich.setMainIngredient("tuna");
sandwich.setBread("wheat");
sandwich.setPrice(4.99);
System.out.println("You have ordered a " +
sandwich.getMainIngredient() + " sandwich on " +
sandwich.getBread() + " bread, and the price is " + sandwich.getPrice());
}
}
Originally, I had the variables in the main() method, not private (but this didn't really make sense to me).
This should output: "You have ordered a tuna sandwich on wheat bread, and the price is 4.99"
At least in my head, the methods make sense. However upon execution:
TestSandwich.java:6: error: cannot find symbol
sandwich.setMainIngredient("tuna");
^
symbol: method setMainIngredient(String)
location: variable sandwich of type Sandwich