Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your task for this assignment is to define multiple classes via inheritance. You

ID: 3686792 • Letter: Y

Question

Your task for this assignment is to define multiple classes via inheritance. Your classes should implement various "snacks" including "M&Ms", "Popcorn", etc. To begin, create an abstract "Snack" class. Then, create two classes "Salty" and "Sugary" that inherit from the "Snack" class. Then create classes "M&Ms", "Popcorn", "Snickers", "Gum", "Crackers", and "Chips" that inherit from "Salty" or "Sugary" as appropriate. Once you have your inheritance hierarchy defined, implement the toString method for each class. Finally, let's presume that each snack has two additional properties: "calories" and "cost". Calories is an integer that represents how many calories the snack contains, and cost is a floating-point number that represents how much the snack costs to buy. Implement these properties within your hierarchy as appropriate.

Explanation / Answer

Solutoion:

package com.chegg.nancy.inheritance;

public abstract class Snack {
   int calories;
   float cost;
}

package com.chegg.nancy.inheritance;

public class Sugary extends Snack{
   Sugary(){
       this.calories = 5;
       this.cost = 50;
   }
  
   Sugary(Gum obj){
       this.calories = 5;
       this.cost = 50;
   }
   Sugary(Snickers obj){
       this.calories = 5;
       this.cost = 50;
   }
   Sugary(MandMs obj){
       this.calories = 5;
       this.cost = 50;
   }
}

package com.chegg.nancy.inheritance;

public class Salty extends Snack {
   Salty(){
       this.calories = 1;
       this.cost = 10;
   }
   Salty(Chips obj){
       this.calories = 1;
       this.cost = 10;
   }
  
   Salty(Crackers obj){
       this.calories = 2;
       this.cost = 20;
   }
  
   Salty(Popcorn obj){
       this.calories = 3;
       this.cost = 30;
   }
  
}

package com.chegg.nancy.inheritance;

public class Chips extends Salty{
   static Chips s = new Chips();
   Chips(){
       super(s);
   }
  
   @Override
   public String toString() {
       return "For Chips : Calories = " + this.calories + " and cost = " + this.cost;
   }
}

package com.chegg.nancy.inheritance;

public class Crackers extends Salty{
   static Crackers s = new Crackers();
   Crackers(){
       super(s);
   }

   @Override
   public String toString() {
       return "For Crackers : Calories = " + this.calories + " and cost = " + this.cost;
   }
}

package com.chegg.nancy.inheritance;

public class Gum extends Sugary{
   static Gum s = new Gum();
   Gum(){
       super(s);
   }
  
   @Override
   public String toString() {
       return "For Gum : Calories = " + this.calories + " and cost = " + this.cost;
   }
}

package com.chegg.nancy.inheritance;

public class MandMs extends Sugary {
   static MandMs s = new MandMs();
   MandMs(){
       super(s);
   }
  
   @Override
   public String toString() {
       return "For MandMs : Calories = " + this.calories + " and cost = " + this.cost;
   }
}

package com.chegg.nancy.inheritance;

public class Popcorn extends Salty{
   static Popcorn s = new Popcorn();
   Popcorn(){
       super(s);
   }
  
   @Override
   public String toString() {
       return "For Popcorn : Calories = " + this.calories + " and cost = " + this.cost;
   }
}

package com.chegg.nancy.inheritance;

public class Snickers extends Sugary{
   static Snickers s = new Snickers();
   Snickers(){
   super(s);
   }
  
   @Override
   public String toString() {
       return "For Snickers : Calories = " + this.calories + " and cost = " + this.cost;
   }
}

package com.chegg.nancy.inheritance;

public class Driver {

   public static void main(String[] args) {
       Chips chips = new Chips();
       Snickers snickers = new Snickers();
       System.out.println(chips);
       System.out.println(snickers);
      
   }
}