1

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
  • Does your `Sandwich` class compile? Hint `setMainIngredient` is not a setter on `Sandwich` – Scary Wombat Mar 02 '23 at 01:10
  • `Sandwich` has no fields at all. You've defined classes in classes which is not what you want. Take the content of your Cost, Loaf, and PrimaryIngredient classes and move it all into Sandwich itself, then delete those classes. – rzwitserloot Mar 02 '23 at 01:11

1 Answers1

0

(I cannot remove main, MindTap won't allow it)

Updated Sandwich.java class, many many thanks to Mr.rzwitserloot:

class Sandwich {
    public static void main(String[] args){
        
    }
    
    String mainIngredient;
    String bread;
    double price;
    
    public String getMainIngredient(){
        return mainIngredient;
    }

    public void setMainIngredient(String newMain){
        mainIngredient = newMain;
    }        

    public String getBread(){
        return bread;
    }

    public void setBread(String newBread){
        this.bread = newBread;
    }

    public double getPrice(){
        return price;
    }
        
    public void setPrice(double newPrice){
        this.price = newPrice;
    }

(No changes made to TestSandwich.java)

  • Your fields should be `private`, and you should remove the empty `main` method -- it serves no purpose. – tgdavies Mar 02 '23 at 03:25