Create a class named Sandwhich. Include Bread field and a SandwhichFilling field
ID: 3666537 • Letter: C
Question
Create a class named Sandwhich. Include Bread field and a SandwhichFilling field. Include a constructor that takes paremeters for each field needed in the two objects and assign them to each object's constructor. Write an application named Test SandwhichFlling to instantiate three Snadwhich objects with different values, and then display all data for each object, including the total calories in a Sandwhich, assuming that each Sandwhich is made using two slices of Bread. Save both the Sandwhich.java and TestSandwhich.java files.
Explanation / Answer
//Sandwitch.java
public class Sandwhich {
Bread bread;
SandwhichFilling sanfFilling;
public Sandwhich(int num, int breadCalori, int sandFillingCalori) {
bread = new Bread(num, breadCalori);
sanfFilling = new SandwhichFilling(sandFillingCalori);
}
public String toString(){
return "Total calori from sandwitch is : "+((bread.number*bread.caloriPerUnit)+
sanfFilling.calori);
}
}
class Bread{
int number;
int caloriPerUnit;
public Bread(int num,int calori){
this.number = num;
this.caloriPerUnit = calori;
}
}
class SandwhichFilling{
int calori;
public SandwhichFilling(int calori){
this.calori = calori;
}
}
//TestSandwhich.java
public class TestSandwhich{
public static void main(String[] args) {
Sandwhich s1 = new Sandwhich(2, 13, 10);
Sandwhich s2 = new Sandwhich(2, 21, 15);
Sandwhich s3 = new Sandwhich(2, 7, 17);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
/*
Output:
Total calori from sandwitch is : 36
Total calori from sandwitch is : 57
Total calori from sandwitch is : 31
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.