Java Programming Homework: Create Sandwich & TestSandwich Classes

In summary, the conversation is about a person seeking help with a Java class and creating a Sandwich class with data fields for main ingredient, bread type, and price. They are also instructed to create methods to get and set these values and create a constructor that sets default values. The conversation also includes a code example for the Sandwich class and the TestSandwich application, with the purpose of demonstrating the constructor's functionality.
  • #1
16
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:
Physics news on Phys.org
  • #2
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 [code=java] at the top (with =java for java code, c for c or c++ code, and so on) and [/code] at the bottom.
 
  • #3
Thank you.
 

What is Java programming?

Java programming is a high-level, object-oriented programming language that is widely used for developing various applications, including web and mobile applications. It is known for its platform independence, meaning that it can run on any operating system, and its robustness, which makes it a popular choice for building large-scale applications.

What is a sandwich class in Java programming?

In Java programming, a sandwich class is a class that represents a sandwich object, which contains data and methods related to sandwiches. It can have attributes such as type of bread, fillings, and condiments, as well as methods to manipulate and access these attributes.

What is the purpose of creating a sandwich class?

The purpose of creating a sandwich class is to encapsulate the properties and behaviors of a sandwich into a single entity. This allows for easier management and manipulation of sandwich data, as well as promoting code reusability by using the sandwich class in multiple parts of a program.

What is the difference between a sandwich class and a testsandwich class?

A sandwich class is the blueprint or template for creating sandwich objects, while a testsandwich class is used to test the functionality of the sandwich class. The testsandwich class typically contains code that creates and manipulates sandwich objects and verifies that the expected results are achieved.

How can I test my sandwich class in Java programming?

To test your sandwich class in Java programming, you can create a testsandwich class and write code to create sandwich objects, set their attributes, and call methods to manipulate them. You can also use debugging tools to step through the code and check the values of variables at different points. Additionally, you can use testing frameworks such as JUnit to automate the testing process and ensure that your sandwich class is functioning correctly.

Suggested for: Java Programming Homework: Create Sandwich & TestSandwich Classes

Replies
7
Views
2K
Replies
1
Views
850
Replies
8
Views
914
Replies
11
Views
811
Replies
4
Views
986
Replies
2
Views
826
Replies
17
Views
1K
Replies
2
Views
4K
Replies
7
Views
1K
Replies
12
Views
1K
Back
Top