0

I am new to Java and am in the 4th week of a programming course. We are learning about constructors and I just can't seem to grasp it.

Here is my test class that was already written for the lab assignment.

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());
  }
}

My assignment is to create second class called Sandwich that the one above can call to. What I have come up with is below.

public class Sandwich 
{
    private String mainIngredient;
    private String bread;
    private double price;

    public String getMainIngredient(){

        return mainIngredient;
    }
    public String getBread(){

        return bread;
    }
    public double getPrice(){

        return price;
    }
    public void setMainIngredient(String ingredient){
        mainIngredient = ingredient;
    }
    public void setBread(String bread){
        bread = bread;
    }
    public void setPrice(double cost){
        price = price;
    }
}

When I run what I have it tells me there is no main method in class Sandwich. There isn't supposed to be though so I don't understand how to eliminate that error?

sandbag89
  • 21
  • 3
  • 8
    It sounds like you're trying to run `Sandwich.java` instead of `TestSandwich.java`. – Jacob G. Sep 20 '19 at 14:04
  • 1
    You mention constructors but I don't see a constructor in either of your classes. – jsheeran Sep 20 '19 at 14:05
  • 1
    @jsheeran He is not using correct terminology, but I assume he means he is learning how to create a custom class that he calls `new` on, currently he is depending on the default constructor. To OP, your code looks fine other than running the wrong program. – Nexevis Sep 20 '19 at 14:06
  • After you have compiled the *.java to *.class, you have to run TestSandwich's main with all .class fies on as classpath: `java -cp . TestSandwich`. Here the class path is the current directory `-cp .` – Joop Eggen Sep 20 '19 at 14:09
  • Sorry for having the wrong terminology. This is all completely new to me. I was running the wrong program. With how the lab is set up, I probably never would've thought to switch tabs and hit run. – sandbag89 Sep 20 '19 at 14:30

1 Answers1

0

Run TestSandwich class instead of Sandwich.java. The entry point of any standalone java program is

public static void main (String args[])
Nishant Lakhara
  • 1,943
  • 3
  • 21
  • 42