Java Programming Homework: Create Sandwich & TestSandwich Classes

  • Context: Comp Sci 
  • Thread starter Thread starter momof4
  • Start date Start date
  • Tags Tags
    Java Programming
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 7K views
momof4
Messages
16
Reaction score
11

Homework Statement



I decided to take a Java class so I can introduce my students to programming/java next year as part of an after school program. I am stuck! My code works except for the last part. It is in bold. Honestly, I am lost. I'm sure it is a simple solution but I can't see it. Any pointers in the right direction would help. Not looking for the solution. Thanks.

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”),

and a double for price (such as 4.99).Include methods to get and set values for each of these fields. Save the class as Sandwich.java.
b. Create an application named TestSandwich that instantiates one Sandwich object

and demonstrates the use of the set and get methods. Save this application as

TestSandwich.java.
  • In the Sandwich class, add a constructor that initially sets the main ingredient to "Turkey", the bread type to "Rye", and the price to 5.99.
  • In the TestSandwich class, add a secondary sandwich object that demonstrates that the constructor works by displaying the Sandwich's initial values

Homework Equations

The Attempt at a Solution


Mod note: Added code tags to preserve the original indentation
Sandwich[/B]
Java:
public class Sandwich
{
public Sandwich()
   {
      mainIng = "Turkey";
      bread = "Rye"; 
      price = 5.99;
   }
  

   public  String mainIng;
   public  String bread;
   public  double price;
  public void setMainIng(String ing)
     { 
    
      mainIng = ing;
    }
  public String getMainIng()
   {
      return mainIng;
    }

  public void setBread(String breadType)
     { 
    
         this.bread = breadType;
    }
  public String getBread()
   {
      return bread;
   }
     public void setPrice(double sandPrice)
     { 
    
      this.price = sandPrice;
    }
  public double getPrice()
   {
      return price;
   }
  public void Order(String mainIng, String bread, double cost)
    {
         System.out.print("Your " + mainIng + " sandwich on\n");
         System.out.print(bread +  " costs $ " + price + "\r");
  }
}
TestSandwich
Java:
public class TestSandwich   
  {   
     public static void main(String[] args)
   {
      Sandwich sandwich2 = new Sandwich();
      sandwich2.setMainIng("ham and cheese");
      sandwich2.setBread("sourdough");
      sandwich2.setPrice(7.99);
      sandwich2.getMainIng();
      sandwich2.getBread();
      sandwich2.getPrice();
      System.out.println("Your " + sandwich2.getMainIng() + " on " + sandwich2.getBread());
      System.out.println("is " + "$" + sandwich2.getPrice());
          
    }
    
   }
 
Last edited by a moderator:
on Phys.org
momof4 said:

Homework Statement



I decided to take a Java class so I can introduce my students to programming/java next year as part of an after school program. I am stuck! My code works except for the last part. It is in bold. Honestly, I am lost. I'm sure it is a simple solution but I can't see it. Any pointers in the right direction would help. Not looking for the solution. Thanks.

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”),

and a double for price (such as 4.99).Include methods to get and set values for each of these fields. Save the class as Sandwich.java.
b. Create an application named TestSandwich that instantiates one Sandwich object

and demonstrates the use of the set and get methods. Save this application as

TestSandwich.java.
  • In the Sandwich class, add a constructor that initially sets the main ingredient to "Turkey", the bread type to "Rye", and the price to 5.99.
  • In the TestSandwich class, add a secondary sandwich object that demonstrates that the constructor works by displaying the Sandwich's initial values

Homework Equations

The Attempt at a Solution


Mod note: Added code tags to preserve the original indentation
Sandwich[/B]
Java:
public class Sandwich
{
public Sandwich()
   {
      mainIng = "Turkey";
      bread = "Rye";
      price = 5.99;
   }
 

   public  String mainIng;
   public  String bread;
   public  double price;
  public void setMainIng(String ing)
     {
   
      mainIng = ing;
    }
  public String getMainIng()
   {
      return mainIng;
    }

  public void setBread(String breadType)
     {
   
         this.bread = breadType;
    }
  public String getBread()
   {
      return bread;
   }
     public void setPrice(double sandPrice)
     {
   
      this.price = sandPrice;
    }
  public double getPrice()
   {
      return price;
   }
  public void Order(String mainIng, String bread, double cost)
    {
         System.out.print("Your " + mainIng + " sandwich on\n");
         System.out.print(bread +  " costs $ " + price + "\r");
  }
}
TestSandwich
Java:
public class TestSandwich  
  {  
     public static void main(String[] args)
   {
      Sandwich sandwich2 = new Sandwich();
      sandwich2.setMainIng("ham and cheese");
      sandwich2.setBread("sourdough");
      sandwich2.setPrice(7.99);
      sandwich2.getMainIng();
      sandwich2.getBread();
      sandwich2.getPrice();
      System.out.println("Your " + sandwich2.getMainIng() + " on " + sandwich2.getBread());
      System.out.println("is " + "$" + sandwich2.getPrice());
         
    }
   
   }
The first line in the main() function creates a Sandwich object, so you should get rid of the three lines that start with "sandwich2.setXxx". The object of the exercise is to show that your class constructor creates an object that already has its properties set to default values. By using the set methods on your class you are defeating the purpose of the exercise.

Also, when you post code, use code tags, with
Java:
 at the top (with =java for java code, c for c or c++ code, and so on) and
at the bottom.