Question

In: Computer Science

Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...

Create a class named Sandwich that contains the following data fields:

  • MainIngredient - of type String
  • Bread - of type String
  • Price - of type Double

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());
}
}


Solutions

Expert Solution

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());

    }

}


Related Solutions

Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
in c#: Create a class named Square that contains fields for area and the length of...
in c#: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named DemoSquares that instantiates an array of ten Square objects...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the...
Introduction to Inheritance (Exercise 1) Write the class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, finish the subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Run the provided DemoHorses application that demonstrates using objects of each class. DemoHorses.java public class DemoHorses { public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT