CS300 P09 VENDING MACHINE solved

$29.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

This Assignment involves implementing a vending machine that provides a discount on whichever item will epire soonest. You will be using an arraybased heap data structure to implement this functionality.
OBJECTIVES AND GRADING CRITERIA
The main goals of this assignment include gaining eperience implementing operations on an array based heap data structure.
25 points
5 zyBooks Tests: automated grading test results are visible upon submission, and allow multiple opportunities to correct the organization and functionality of your code. Your highest scoring submission prior to the deadline will be recorded.
25 points
5 Hidden Tests: automated grading tests are run after the assignment’s deadline. They check for similar functionality and organizational correctness as the zyBooks Tests. But you will NOT be able to resubmit corrections for extra points, and should therefore consider and test your own code even more thoroughly.
GETTING STARTED
0. reate a new ava proect in Eclipse. You can name this proect whatever you like, but ending achine is a descriptive choice.
THE ITEM CLASS
LECTURE NOTES
1. You are building a vending machine that dispenses items. Thus, the first step is to create a class with appropriate fields to represent an item. reate a class called Item in a file named Item.ava. The only fields in this class should be:
You should add public accessors for each of these fields. For a field called y, the accessor should be named gety. You should also add at least one constructor to this class with the following signature: public Itemint epirationDay, String description. This constructor should initialie the epiration day and description fields based on the provided arguments.
THE VENDING MACHINE CLASS
. This is the actual class where you will implement the heap operations. You should create a class named endingachine, saved in a file named endingachine.ava. The only fields in this class should be:
reate a constructor public endingachineint capacity which initialies the items array to a new array containing capacitynumber of null Item references, and initialies the itemount field to ero.
HELPER METHODS
. Implement the following private helper methods in the endingachine class.
1 2
private private int int expirationDay; // starting at day 0, which represents Jan 1, 2018 private private String String description; // a human readable description of this item
1 2 3 4
private private Item[] items;  // store items in a min-heap private private int int itemCount; // number of items in this heap // Note use of min-heap here, to prioritize the smallest (soonest) expiration day. // You may decide to use either 0 or 1 as the top-index in this items array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
private private int int getParent(int int childIndex) {     // return the parent index of the given child index     return return -1; }   private private int int getLeftChild(int int parentIndex) {     // return the left child index of the given parent index     return return -1; }   private private int int getRightChild(int int parentIndex) {     // return the right child index of the given parent index     return return -1; }   private private void void swap(int int itemOneIndex, int int itemTwoIndex) {     // swaps the Item references at itemOneIndex and itemTwoIndex in the items array }   private private void void addHelper(int int index) {     // Propagates the min-heap order property from the node at position index,     // up through it’s ancestor nodes. Assumes that only the node at position
The indees passed into and returned from these methods can either make use of either convention: the topinde is ero, or the topinde is one. However they should all make use of the same convention. Youll make use of these helper methods to implement the core methods listed in the net step.
THE CORE METHODS
. You are epected to implement the following core methods in the endingachine class according to the descriptions below:
In the net step, you will further test your implementations of these core methods.
TESTING AND ERROR CHECKING
23 24 25 26 27 28 29 30 31 32
// index may be in violation of this property. This is useful when adding     // a new item to the bottom of the heap. }   private private void void removeHelper(int int index) {     // Propagates the min-heap order property from the node at position index,     // down through it’s highest priority descendant nodes. Assumes that the     // children of the node at position index conform to this heap property.     // This is useful when removing an item from the top of the heap. }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
public public void void addItem(Item item) {     // Add the given item to the items array and perform the necessary     // actions to maintain the min-heap properties. }   public public Item dispenseNextItem() {     // Dispense the item with the smallest expiration date from this     // vending machine, while maintaining the min-heap properties.     // This method removes the item returned from the heap.     return return null null; }      public public Item getNextItem() {     // This method returns a reference to the next item that will be dispensed.     // This method does NOT change the state of the Vending Machine or its heap.     return return null null; }   public public Item dispenseItemAtIndex(int int index) {     // Dispense the item from a particular array index, while maintaining     // the min-heap properties.  This method removes that item from the heap.     // This index parameter assumes the top-index is zero.  So you’ll need to     // add one to this index, if you are using the top-index = 1 convention.     return return null null; }
public public Item getItemAtIndex(int int index) {     // This method returns a reference to the item at position index.     // This method does not change the state of the vending machine.     // This index parameter assumes the top-index is zero. So you’ll need to     // add one to this index, if you are using the top-index = 1 convention.         return return null null; }
. reate a new class named ain with a public static void mainString args method. se this driver to create Item obect, add them to your endingachine, and test the various methods within your endingachine class. You do not need to submit this ain.ava file. 6. You are epected to handle the following problematic uses of your endingachine class by throwing eceptions with with the messages described below: When adding an item to a endingachine that is already full to capacity, your code should throw an IllegalStateEception with the message WARNIN: Item not added. This vending machine is already filled to capacity. The state of this vending machine should not change when add is called under these circumstances. When calling dispenseNetItem, getNetItem, dispenseItemAtInde, or getItemAtInde on an empty endingachine, your code should throw an IllegalStateEception with the message WARNIN: Operation not allowed. This vending machine is empty. The state of this vending machine should not change when these methods are called under these circumstances. When calling dispenseItemAtInde or getItemAtInde with an invalid inde on a non empty endingachine, your code should throw an IndeOutOfoundsEception with the message WARNIN: Operation not allowed. Inde is invalid. The state of this vending machine should not change when these methods are called under these circumstances. Your private helper methods should not throw any eceptions. You should instead prevent them from encountering any problems, by only calling them when they can successfully complete their intended computation. 7. ongratulations on finishing this S00 assignment After verifying that your work is correct, and written clearly in a style that is consistent with the course style guide, you should submit your work through ybooks. The most recent of your highest scoring submissions prior to the deadline of 17:00 on Thursday, November 23rd will be used as part of your score for this assignment. Additional grading tests will then be run against your highest scoring submission, to determine the rest of your assignment grade.