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: 3686981 • 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

PLEASE HELP FINISH!!!!!!!

public static void main(String[] args) { Snacks s1 = new MMs(); System.out.println(s1); } } abstract class Snacks { private int calories; private double cost; private String type; public Snacks(int calories, double cost, String type) { this.calories = calories; this.cost = cost; this.type = type; } public Snacks(int calories, double cost) { this(calories, cost, "general snack"); } public String toString() { return "This is a " + this.type; } public int getCalories() { return calories; } public double getCost() { return cost; } } abstract class Sugary extends Snacks { private String name; public Sugary(int calories, double cost) { super(calories, cost, "sugary snack"); } public Sugary(int calories, double cost, String name) { this(calories, cost); this.name = name; } public String toString() { return super.toString() + " named " + this.name + ". It costs $" + getCost() + " and it has " + getCalories() + " calories."; } } class MMs extends Sugary { public MMs() { super(142, 0.99, "M&Ms"); } }

Explanation / Answer

Here as each snack has different amount of calories and different cost I different snack object with different values.We can also Initialize the super class varible from subclass by using super keyword.But her if we have done that then each snack will be displayed with same calories and same cost.

I used the Inheritance property in this program that super class object can refer to that sub class object.

Snacks.java

package org.students;

public abstract class Snacks
{
   int calories;
   float cost;
   @Override
   public String toString() {
       return "Snacks [calories=" + calories + ", cost=" + cost + "]";
   }
  }

__________________________________________________________________________________________

Salty.java

package org.students;

public abstract class Salty extends Snacks
{
   int calories;
   float cost;
   @Override
   public String toString() {
       return "Salty [calories=" + calories + ", cost=" + cost + "]";
   }

}

_________________________________________________________________________________________

Sugary.java

package org.students;

public class Sugary extends Snacks
{
   int calories;
   float cost;
   @Override
   public String toString() {
       return "Sugary [calories=" + calories + ", cost=" + cost + "]";
   }
  
}

_____________________________________________________________________________________________

MAndMs.java

package org.students;

public class MAndMs extends Salty {
   int calories;
   float cost;
  
  


   public MAndMs(int calories, float cost) {
       super();
       this.calories = calories;
       this.cost = cost;
   }


   @Override
   public String toString() {
       return "MAndMs [calories=" + calories + ", cost=" + cost + "]";
   }
  

}

__________________________________________________________________________________________

Gum.java

package org.students;

public class Gum extends Sugary {
   int calories;
   float cost;
   public Gum(int calories, float cost) {

       super();
       this.calories = calories;
       this.cost = cost;
   }

@Override

   public String toString() {
       return "Gum [calories=" + calories + ", cost=" + cost + "]";
   }
  

}

__________________________________________________________________________________________

Crakers.java

package org.students;

public class Crakers extends Salty {
   int calories;
   float cost;

   public Crakers(int calories, float cost) {
       super();
       this.calories = calories;
       this.cost = cost;
   }

   @Override
   public String toString() {
       return "Crakers [calories=" + calories + ", cost=" + cost + "]";
   }

}

___________________________________________________________________________________________

Chips.java

package org.students;

public class Chips extends Salty {
   int calories;
   float cost;

   public Chips(int calories, float cost) {
       super();
       this.calories = calories;
       this.cost = cost;
   }

   @Override
   public String toString() {
       return "Chips [calories=" + calories + ", cost=" + cost + "]";
   }

}

______________________________________________________________________________________________

Snickers.java

package org.students;

public class Snickers extends Sugary {
   int calories;
   float cost;

   public Snickers(int calories, float cost) {
       super();
       this.calories = calories;
       this.cost = cost;
   }

   @Override
   public String toString() {
       return "Snickers [calories=" + calories + ", cost=" + cost + "]";
   }

}

___________________________________________________________________________________________

Test.java

package org.students;

public class Test {

   public static void main(String[] args) {
   //created Super Class Reference Variable
   Snacks sn;
   //Super class reference variable can refers to subclass object.
   //This Due to the concept of inheritance.
   sn=new MAndMs(250,50);
   System.out.println(sn.toString());
  
   sn=new Snickers(200,35);
   System.out.println(sn.toString());
  
   sn=new PopCorn(100,25);
   System.out.println(sn.toString());
  
   sn=new Gum(30,5);
   System.out.println(sn.toString());
  
   sn=new Crakers(50,20);
   System.out.println(sn.toString());
  
   sn=new Chips(75,25);
   System.out.println(sn.toString());
  
  
  

   }

}

__________________________________________________________________________________________

output:

MAndMs [calories=250, cost=50.0]
Snickers [calories=200, cost=35.0]
PopCorn [calories=100, cost=25.0]
Gum [calories=30, cost=5.0]
Crakers [calories=50, cost=20.0]
Chips [calories=75, cost=25.0]

_______________________________________________________________________________________________