In: Computer Science
Create a class named Sandwich that contains the following data fields:
Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient.
Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TestSandwich.java (provided):
class TestSandwich
{
public static void main (String args[])
{
Sandwich sandwich = new Sandwich();
sandwich.setMainIngredient("tuna");
sandwich.setBread("wheat");
sandwich.setPrice(4.99);
System.out.println("You have ordered a " +
sandwich.getMainIngredient() + " sandwich on " +
sandwich.getBread() + " bread, and the price is " +
sandwich.getPrice());
}
}
If you have any doubts, please give me comment...
class Sandwich{
private String MainIngredient;
private String Bread;
private Double Price;
Sandwich(){
}
Sandwich(String m_ing, String bread, Double price){
MainIngredient = m_ing;
Bread = bread;
Price = price;
}
/**
* @return the mainIngredient
*/
public String getMainIngredient() {
return MainIngredient;
}
/**
* @return the bread
*/
public String getBread() {
return Bread;
}
/**
* @return the price
*/
public Double getPrice() {
return Price;
}
/**
* @param mainIngredient the mainIngredient to set
*/
public void setMainIngredient(String mainIngredient) {
MainIngredient = mainIngredient;
}
/**
* @param bread the bread to set
*/
public void setBread(String bread) {
Bread = bread;
}
/**
* @param price the price to set
*/
public void setPrice(Double price) {
Price = price;
}
}
public class TestSandwich {
public static void main(String args[]) {
Sandwich sandwich = new Sandwich();
sandwich.setMainIngredient("tuna");
sandwich.setBread("wheat");
sandwich.setPrice(4.99);
System.out.println("You have ordered a " + sandwich.getMainIngredient() + " sandwich on " + sandwich.getBread()
+ " bread, and the price is " + sandwich.getPrice());
}
}