Answer to Question #311753 in Java | JSP | JSF for awd

Question #311753

Create an application named TestSandwich_[your name] that instantiates one Sandwich object and demonstrates the use of the set and get methods. 

1
Expert's answer
2022-03-16T14:49:07-0400
import java.util.ArrayList;

public class Sandwich {
    private String bread;
    private ArrayList<String> filling = new ArrayList<>();
    private double price;

    public void setBread(String typeOfBread){
        this.bread = typeOfBread;
    }

    public void setFilling(String ingredient){
        this.filling.add(ingredient);
    }

    public void setPrice(double amountOfMoney){
        this.price = amountOfMoney;
    }

    public String getBread(){
        return bread;
    }

    public ArrayList<String> getFilling(){
        return filling;
    }

    public Double getPrice(){
        return price;
    }
}

public class Test {
    public static void main(String args[]){
        Sandwich sandwichStandart = new Sandwich();
        sandwichStandart.setBread("wheat");
        sandwichStandart.setFilling("lettuce");
        sandwichStandart.setFilling("ham");
        sandwichStandart.setFilling("cheese");
        sandwichStandart.setFilling("tomato");
        sandwichStandart.setPrice(2.75);
        System.out.println("Bread: " + sandwichStandart.getBread());
        System.out.println("Filling: " + sandwichStandart.getFilling());
        System.out.println("Price: " + sandwichStandart.getPrice());
    }
}

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