Q&A: Cant figure out the methods and contructors I need?
Question by endo555: Cant figure out the methods and contructors I need?
Ok, I am totally lost at going about this problem. I cant go to tutoring at my school because i have class at the exact same time its offered so I thought ide come here. Here is the problem i was assigned:
Sandwich
- bread: String
- vegetables: String
- meat: String
- price: double
+ Sandwich()
+ Sandwich()(String, String, String, double)
+ getBread(): String
+ getVegetable(): String
+ getMeat(): String
+ getPrice(): String
+ setBread(): String
+ setVegetable(): String
+ setMeat(): String
+ setPrice(): String
+toString(): String
The above class diagram (in URL notation), write Sandwich.java that simulates the sandwich concept in real world. As the diagram shows, it has three instance variables, two constructors, and nine methods. In particular,
- The first constructor sets the four instance variables (bread, vegetables, meat, and balance) to the following arbitrary values: “”, “”, “”, and 0.0, respectively.
- The second constructor sets bread, vegetables, meat, and balance according to the input parameters’ values.
- The toString() method should return a string for the content of bread, vegetables, meat, and price (and the price should be in a typical U.S. currency format). To help you define the toString method, a sample content of the returned value for the toString method is given as follows (dummy values are used below).
Bread: Organic Bread
Vegetable: lettuce, tomatoes, green peppers
Meat: Roast Beef
Price: .55
And this is what i have so far with the help of a couple people getting me started. What am i missing to make this work?
import java.text.NumberFormat;
public class Sandwich
{
private String bread = “”;
private String vegetables = “”;
private String meat = “”;
private double price = 0.0;
public Sandwich(String bread, String vegetable,
String meat, double price)
{
setBread(bread);
setVegetables(vegetables);
setMeat(meat);
setPrice(price);
}
public void setBread(String bread)
{
this.bread = bread;
}
public void setVegetables(String vegetables)
{
this.vegetables = vegetables;
}
public void setMeat(String meat)
{
this.meat = meat;
}
public void setPrice(double price)
{
this.price = price;
}
public String getBread()
{
return this.bread;
}
public String getVegetables()
{
return this.vegetables;
}
public String getMeat()
{
return this.meat;
}
public double getPrice()
{
return price;
}
public String toString()
{
return “Bread: ” + getBread() + “\nVegetable: ” +
getVegetables() + “Meat: ” + getMeat() + “Price: ” +
price;
}
}
Best answer:
Answer by Taz
Sandwich
- bread: String
- vegetables: String
- meat: String
- price: double
private String bread, vegatables, meat;
private double price;
+ Sandwich()
public Sandwich() { }
+ Sandwich()(String, String, String, double) <-- overload the constructor
public Sandwich(String b, String v, String m, double p){ price = p; b = bread; ect... }
+ getBread(): String
+ getVegetable(): String
+ getMeat(): String
+ getPrice(): String
get methods (+ = public)
public String getBread()
{
return bread;
}
+ setBread(): String
+ setVegetable(): String
+ setMeat(): String
+ setPrice(): String
set methods are voids
public void setBread(String b)
{
bread = b;
}
+toString(): String
toString gets called when you want to print it.
ie print stuff.
public String toString()
{
String output = "Bread = ___\nVegatables = ___"; ect...
return output;
}
the - means make a private variable of that name
UML* format.
Hope I helped.
Add your own answer in the comments!
Tagged with: Can't • contructors • figure • Methods • need
Filed under: Organic Gardening
Like this post? Subscribe to my RSS feed and get loads more!


Leave a Reply