Answer the following questions. Bring hardcopies of your answers to class, hand written or printouts. All homeworks are due before class on the due date. Please remember that the GMU Honor Code is in effect: You may discuss the assignment with each other, the professor, or the GTA, but each homework should be done individually.
Use the following class vendingMachine for the questions below. The complete program vendingMachine.java is available for your reference.
// A Java implementation of VendingMachine. import java.util.*; import java.io.*; public class vendingMachine { private int credit; private LinkedListstock; // Maximum size of vendingMachine private static final int MAX = 10; //************************************************ // Constructor // vendingmachine starts empty. //************************************************ vendingMachine() { credit = 0; stock = new LinkedList (); // Empty stock. } //************************************************ // A coin is given to the vendingMachine. // Must be a dime, quarter or dollar. // Ignores invalid input //************************************************ public void coin (int coin) { if (coin != 10 && coin != 25 && coin != 100) return; if (credit >= 90) return; credit = credit + coin; return; } //************************************************ // User asks for a chocolate. // Returns the change and the sets the // parameter StringBuffer variable Choc. // If not enough money or no chocolates, // returns 0 and a blank string. //************************************************ // Necessary because strings are immutable // C++ version returned both choc and change as parameters public int getChoc (StringBuffer choc) { int change; if (credit < 90 || stock.size() <= 0) { change = 0; choc.replace (0, choc.length(), ""); return (change); } change = credit - 90; credit = 0; choc.replace (0, choc.length(), (String) stock.removeFirst()); return (change); } //************************************************ // Adds one new piece of chocolate to the machine // If machine is full, nothing happens //************************************************ public void addChoc (String choc) { if (stock.size() >= MAX) return; stock.add (choc); return; } } // End class vendingMachine