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

JAVA- Submit 1 java file corresponding to the main program as requested below. Y

ID: 674887 • Letter: J

Question

JAVA-

Submit 1 java file corresponding to the main program as requested below.

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

public class VendingMachine

{

private int Snackcount;

private double moneyCredit;

private double Snackcost;

private final double Snickers = .95;

private final double Gum = .75;

private final double Mnms = .85;

private final double Popcorn = 1.25;

private final double Crackers = 1.00;

private final double Chips = 1.15;

private double returnAmount;

public VendingMachine()

{

Snackcount = 100;

moneyCredit = 0.0;

returnAmount = 0.0;

}

public void restock()

{

Snackcount = 100;

moneyCredit = 0.0;

}

public void addMoney ( double amount )

{

moneyCredit += amount;

System.out.println ( "Total inserted: " + moneyCredit );

}

public void vend()

{

double returnAmount = 0.0;

if ( moneyCredit >= Snackcost() )

{

   System.out.println ( "Here is your snack" );

   returnAmount -= ( moneyCredit - Snackcost );

   if ( returnAmount >= 0.0 )

System.out.println ( "Your change is: " + ( moneyCredit - Snackcost ) " cents." );

   moneyCredit = 0.0;

}

else

System.out.println( "Vending machine only has " + moneyCredit + " cents.Please insert more." );

}

}

class VendingMachineTester

{

public static void main( String[] args )

{

boolean doAgain = true;

char choice;

VendingMachine myMachine = new VendingMachine();

Snack mySnack = new Snack();

while ( doAgain )

{

   JOptionPane.showMessageDialog(null, "Enter your choice of snack!: " +

                                        "1) Exit /n2) Incert Money /n3) Snickers" +

                                                    "4) Gum /n5) M&ms /n6)

PopCorn" +

                                           "7) Crackers /n8)Chips/n9) Restock" +

   /*System.out.println();

   System.out.println( "Options:" );

   System.out.println();

   System.out.println( "1] Exit" );

   System.out.println( "2] Insert money" );

   System.out.println( "3] Vend Snickers" );

   System.out.println( "4] Vend Gum" );

    System.out.println( "5] Vend M&ms" );

   System.out.println( "6] Vend Popcorn" );

    System.out.println( "7] Vend Crackers" );

   System.out.println( "8] Vend Chips" );

   System.out.println( "9] Restock vending machine" );

   System.out.println();

   System.out.print( "Choice: " );

    */

   choice = Keyboard.readChar()

   if(choice == '1')

    doAgain = false;

if( choice == '2' )

   {

    System.out.print( "Enter amount of cents to insert: " );

    double cents = Keyboard.readDouble();

    myMachine.addMoney ( cents );

   }

   if ( choice == '3' )

   {

    System.out.println( "Vend a Snickers and get change." );

    myMachine.vend();

   }

   if ( choice == '4' )

   {

     System.out.println ( "Vend a Gum and get change." );

     myMachine.vend();

   }

    if ( choice == '3' )

   {

    System.out.println( "Vend M&ms and get change." );

    myMachine.vend();

}

   if ( choice == '4' )

   {

     System.out.println ( "Vend PopCorn and get change." );

     myMachine.vend();

   }

   if ( choice == '3' )

   {

    System.out.println( "Vend Crackers and get change." );

    myMachine.vend();

   }

   if ( choice == '8' )

   {

     System.out.println ( "Vend Chips and get change." );

     myMachine.vend();

   }

   if ( choice == '9' )

   {

    System.out.println ( "Restock vending machine." );

    myMachine.restock();

   }

}

System.out.println();

System.out.println();

System.out.println( "Thanks for using my vending machine." );

}

}

abstract class Snack

{

int calories;

double cost;

public Snack()

{

calories = 0;

cost = 0.0;

}

public double Snackcost()

{

return cost;

}

public int calories()

{

return calories;

}

}

class Salty extends Snack

{

public Salty(int cal, double amt)

{

super();

calories = cal;

cost = amt;

}

}

class Sugary extends Snack

{

public Sugary(int cal, double amt)

{

super();

calories = cal;

cost = amt;

}

}

class Snickers extends Sugary

{

public Snickers()

{

setCalories(15);

setCost(.95);

}

}

class Gum extends Sugary

{

public Gum()

{

setCalories(5);

setCost(.75);

}

}

class Mnms extends Sugary

{

public Mnms()

{

setCalories(20);

setCost(.85);

}

}

class Popcorn extends Salty

{

public Popcorn()

{

setCalories(50);

setCost(1.25);

}

}

class Crackers extends Salty

{

public Crackers()

{

setCalories(25);

setCost(1.00);

}

}

class Chips extends Salty

{

public Chips()

{

setCalories(30);

setCost(1.15);

}

}