Answer to Question #262314 in Java | JSP | JSF for XXXXXXX

Question #262314

Consider a class representing a ClubSandwich. A sandwich is made of some number of bread slices, cheese slices, meat patties and tomato slices. The club sandwich might also contain (or not) mustard, ketchup, iceburg. The sandwich may or may not be grilled. Different composition of the sandwich will cost different prices according to what it contains A sample of the class is given on moellim. You can use the same file and provide the missing things. You are required to provide the following 1. A parameterized constructor taking 4 arguments for bread slices, cheese slices, meat patty number and tomato slices. The values of the rest of the variables will be initialized by default to true 2. Another parameterized constructor that takes argument for each of these variables and initializes them accordingly 3. Another parameterized constructor that takes argument for each of these variables and initializes them accordingly 4. Provide setters for mustard, ketchup, iceberg and gilled


1
Expert's answer
2021-11-07T14:53:06-0500


SOLUTION TO THE ABOVE QUESTION


SOLUTION CODE


package com.company;

class sandwich{
    private int bread_slices;
    private int cheese_slices;
    private int meat_patties;
    private int tomato_slices;
    //may or not
    private String mustard;
    private  String ketchup;
    private String iceberg;
    //may or not
    private boolean grilled;
    //A parameterised constructor that takes arguments
    // and initializes the four variables accordingly
    public sandwich(int b, int c, int m, int t)
    {
        this.bread_slices = b;
        this.cheese_slices = c;
        this.meat_patties = m;
        this.tomato_slices = t;
    }

    //setters for mustard, ketchup, iceberg and grilled

    //set the mustard used
    public void setMustard(String m)
    {
        this.mustard = m;
    }
    //set ketchup used
    public void setKetchup(String k)
    {
        this.ketchup = k;
    }
    //set the iceberg used
    public void setIceburg(String ice)
    {
        this.iceberg = ice;
    }
    //set if the sandwich is grilled or not
    public void setGrilled(boolean t)
    {
        this.grilled = t;
    }

    //getters for mustard, ketchup, iceberg and grilled

    //get the mustard used
    public  String getMustard()
    {
        return mustard;
    }
    //get ketchup used
    public  String getKetchup()
    {
        return ketchup;
    }
    //get the iceberg used
    public String getIceberg()
    {
        return iceberg;
    }
    public boolean getGrilled()
    {
        return grilled;
    }

    //write a display method
    public void display()
    {
        System.out.println("\nThe following are the ingridients used to make the sandwich");
        System.out.println(bread_slices+" slices of bread used");
        System.out.println(cheese_slices+" slices of cheese used");
        System.out.println(meat_patties+" slices of meat used");
        System.out.println(tomato_slices+" slices of tomato used");
        System.out.println(getMustard()+" mustard is used");
        System.out.println(getKetchup()+" ketchup is used");
        System.out.println(getIceberg()+" icerberg is used");

        if(getGrilled()==true)
        {
            System.out.println("The sandwich is grilled");
        }
        else
        {
            System.out.println("The sandwich is not grilled");
        }

    }

}

public class Main {


    public static void main(String[] args) {
     sandwich s = new sandwich(4,2,12,4);
     s.setMustard("Honey");
     s.setKetchup("tomato sauce");
     s.setIceburg("Shredded");
     s.setGrilled(true);
     s.display();
    }
}



SAMPLE OUTPUT PROGRAM



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS