Submit 1 java file corresponding to the main program as requested below. To begi
ID: 3567368 • Letter: S
Question
Submit 1 java file corresponding to the main program as requested below.
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
Program code:
//abstract class that contains an abstract method
//display
public abstract class Snack
{
abstract void display();
}
//Salty class that extends an abstract class Snack
//and defines the method display
public class Salty extends Snack
{
public void display()
{
System.out.println("These are salty.");
}
}
//Sugary class that extends an abstract class Snack
//and defines the method display
public class Sugary extends Snack
{
public void display()
{
System.out.println("These are sugary.");
}
}
//MandMs class that inherits the properties of Sugary class
public class MandMs extends Sugary
{
float cost=42f;
int calories=1;
//constructor
public MandMs()
{}
//toString method that calls the display method of
//Sugary class and also returns a string
public String toString()
{
String s;
System.out.println("MandMs: ");
display();
s="M&Ms contains "+calories+" cal "+
"The cost of each M&Ms pack is: "+cost+"oz";
return s;
}
}
//PopCorn class that inherits the properties of Salty class
public class PopCorn extends Salty
{
float cost=5.98f;
int calories=375;
//constructor
public PopCorn()
{}
//toString method that calls the display method of
//Sugary class and also returns a string
public String toString()
{
String s;
System.out.println("PopCorn: ");
display();
s="Popcorn contains "+calories+" cal "+
"The cost of each pack of popcorn is: "+cost+"oz";
return s;
}
}
//Snickers class that inherits the properties of Sugary class
public class Snickers extends Sugary
{
float cost=4.2f;
int calories=120;
//constructor
public Snickers()
{}
//toString method that calls the display method of
//Salty class and also returns a string
public String toString()
{
String s;
System.out.println("Snickers: ");
display();
s="Snickers contains "+calories+" cal "+
"The cost of each Snickers pack is: "+cost+"oz";
return s;
}
}
//Gum class that inherits the properties of Sugary class
public class Gum extends Sugary
{
float cost=0.2f;
int calories=15;
//constructor
public Gum()
{}
//toString method that calls the display method of
//Sugary class and also returns a string
public String toString()
{
String s;
System.out.println("Gum: ");
display();
s="Chewing gum contains "+calories+" cal "+
"The cost of each Chewing Gum is: $"+cost;
return s;
}
}
//Crackers class that inherits the properties of Salty class
public class Crackers extends Salty
{
float cost=5.98f;
int calories=55;
//constructor
public Crackers()
{}
//toString method that calls the display method of
//Salty class and also returns a string
public String toString()
{
String s;
System.out.println("Crackers: ");
display();
s="Crackers contains "+calories+" cal "+
"The cost of each pack of cracker is: $"+cost;
return s;
}
}
//Chips class that inherits the properties of Salty class
public class Chips extends Salty
{
float cost=2.3f;
int calories=312;
//constructor
public Chips()
{}
//toString method that calls the display method of
//Salty class and also returns a string
public String toString()
{
String s;
System.out.println("Chips: ");
display();
s="Chips contains "+calories+" cal "+
"The cost of each Chips pack is: $"+cost;
return s;
}
}
---------------------------------------------------------------------------------------------------------------------
Sample Output:
MandMs:
These are sugary.
M&Ms contains 1 cal
The cost of each M&Ms pack is: 42.0oz
PopCorn:
These are salty.
Popcorn contains 375 cal
The cost of each pack of popcorn is: 5.98oz
Gum:
These are sugary.
Chewing gum contains 15 cal
The cost of each Chewing Gum is: $0.2
Crackers:
These are salty.
Crackers contains 55 cal
The cost of each pack of cracker is: $5.98
Chips:
These are salty.
Chips contains 312 cal
The cost of each Chips pack is: $2.3
Snickers:
These are sugary.
Snickers contains 120 cal
The cost of each Snickers pack is: 4.2oz