- #1
momof4
- 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");
}
}
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: