Hey guys im completely new to programming and im complete stuck and any help to comlete my assingment would be greatly appreciated. heres my assingment.
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.

Recommended Answers

All 5 Replies

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);
 }
}
commented: This isn't your code. You copied it from jones13. -1

This is what i have so far.

commented: YOu just cpoies someone else's work. Zero marks for that! -3

It would be nice if the code successfully compiled. ;->

I like the constructors in Salty and Sugary classes, but I would do that constructor in the Snack root/base class, and chain constructors in all subclasses. (That is, you need to use "super(parm1,parm2)" as the first line of child class constructors.)

You can't say new Snack(); it's abstract. You need a plan for creating instances of snacks in the VendingMachine class. I might have the VendingMachine class hold a Map<Character, List<Snack>> or something like it -- for each input, you'd have a List of Snack instances to sell. Or maybe it should be Map<Snack, Integer> -- one of each kind of Snack, each with a count of how many of those you have in the machine. You need to figure out how you want to think about it.

I see what you did there. You copied jones13's partly completed program from a year ago. You really need to figure out how to write your own programs. Otherwise, you're just not going to make it.

This is what i have so far.

Please don't think we are fools. We know where you copied that from, and so will your teacher after 15 seconds with Google.
Copying someone else's answers is likely to get you an automatic fail of your course, and possibly being thrown out (quite rightly!).
The only way to learn is to do it yourself. If you make that effort people here will help you.

commented: Quite rightly. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.