Java Programming

  • Comp Sci
  • Thread starter momof4
  • Start date
  • #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:

Answers and Replies

  • #2
34,692
6,396

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
16
11
Thank you.
 

Related Threads on Java Programming

  • Last Post
Replies
6
Views
718
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
9
Views
2K
Replies
7
Views
2K
  • Last Post
Replies
12
Views
8K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
9
Views
2K
  • Last Post
Replies
3
Views
2K
Top