$25
CS300: PROGRAMMING II
Pair Programming is NOT allowed for this assignment.
This Assignment involves implementing a vending machine that provides a discount on whichever
item will expire soonest. You will be using an array-based heap data structure to implement this
functionality.
OBJECTIVES AND GRADING CRITERIA
The main goals of this assignment include gaining experience implementing operations on an arraybased 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. Create a new Java8 project in Eclipse. You can name this project whatever you like, but Vending
Machine is a descriptive choice.
THE ITEM CLASS
P09 VENDING MACHINE
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. Create a class called Item in a file named Item.java.
The only fields in this class should be:
You should add public accessors for each of these fields. For a field called xyz, the accessor
should be named getXyz. You should also add at least one constructor to this class with the
following signature: public Item(int expirationDay, String description). This constructor should
initialize the expiration day and description fields based on the provided arguments.
THE VENDING MACHINE CLASS
2. This is the actual class where you will implement the heap operations. You should create a class
named VendingMachine, saved in a file named VendingMachine.java. The only fields in this class
should be:
Create a constructor public VendingMachine(int capacity) which initializes the items array to a
new array containing capacity-number of null Item references, and initializes the itemCount field
to zero.
HELPER METHODS
3. Implement the following private helper methods in the VendingMachine class.
1
2
private int expirationDay; // starting at day 0, which represents Jan 1, 2018
private String description; // a human readable description of this item
1
2
3
4
private Item[] items; // store items in a min-heap
private 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 int getParent(int childIndex) {
// return the parent index of the given child index
return -1;
}
private int getLeftChild(int parentIndex) {
// return the left child index of the given parent index
return -1;
}
private int getRightChild(int parentIndex) {
// return the right child index of the given parent index
return -1;
}
private void swap(int itemOneIndex, int itemTwoIndex) {
// swaps the Item references at itemOneIndex and itemTwoIndex in the items array
}
private void addHelper(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 indexes passed into and returned from these methods can either make use of either
convention: the top-index is zero, or the top-index is one. However they should all make use of
the same convention. You’ll make use of these helper methods to implement the core methods
listed in the next step.
THE CORE METHODS
4. You are expected to implement the following core methods in the VendingMachine class
according to the descriptions below:
In the next 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 void removeHelper(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 void addItem(Item item) {
// Add the given item to the items array and perform the necessary
// actions to maintain the min-heap properties.
}
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 null;
}
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 null;
}
public Item dispenseItemAtIndex(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 null;
}
public Item getItemAtIndex(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 null;
}
5. Create a new class named Main with a public static void main(String[] args) method. Use this
driver to create Item object, add them to your VendingMachine, and test the various methods
within your VendingMachine class. You do not need to submit this Main.java file.
6. You are expected to handle the following problematic uses of your VendingMachine class by
throwing exceptions with with the messages described below:
When adding an item to a VendingMachine that is already full to capacity, your code should
throw an IllegalStateException with the message “WARNING: 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 dispenseNextItem(), getNextItem(), dispenseItemAtIndex(), or
getItemAtIndex() on an empty VendingMachine, your code should throw an
IllegalStateException with the message “WARNING: 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 dispenseItemAtIndex() or getItemAtIndex() with an invalid index on a nonempty VendingMachine, your code should throw an IndexOutOfBoundsException with the
message “WARNING: Operation not allowed. Index 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 exceptions. You should instead prevent
them from encountering any problems, by only calling them when they can successfully
complete their intended computation.
7. Congratulations on finishing this CS300 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 zybooks. 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.