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 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...
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....
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG),...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and 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 subclass named LabCourse that adds $50 to the course fee....
Draw UML diagram Define a class named Document that contains a member variable of type string...
Draw UML diagram Define a class named Document that contains a member variable of type string named text that stores any textual content for the document. Create a function named getText that returns the text field and a way to set this value. Next, define a class for Email that is derived from Document and that includes member variables for the sender , recipient , and title of an e-mail message. Implement appropriate accessor and mutator functions. The body of...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT