1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Programming

  1. May 21, 2016 #1
    1. The problem statement, all variables and given/known data

    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

    2. Relevant equations


    3. The attempt at a solution
    Mod note: Added code tags to preserve the original indentation
    Sandwich

    Code (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
    Code (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: May 22, 2016
  2. jcsd
  3. May 22, 2016 #2

    Mark44

    Staff: Mentor

    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.
     
  4. May 23, 2016 #3
    Thank you.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Java Programming
  1. Java programming (Replies: 4)

  2. Java program (Replies: 3)

  3. Java programming error (Replies: 6)

Loading...