Exam Rules
The exam is due at 3:30PM Eastern on October 14th. You can take as long as you need on the exam until the due date.
No collaboration is permitted.
The exam is open note, meaning that you can use any resources from the course website, any HW or recitation work you’ve already completed, and any notes that you have generated yourself. You can also refer to JavaDocs as you need. No other sources are permitted, including Google and StackOverflow.
Submit your code to Gradescope. You should submit only the files containing methods that you are asked to write (VendingMachine.java, Payment.java, and PartTwo.java). Do not change methods than the specified six that you’ll find highlighted in red throughout this document. Add fields at your own risk!
Contrary to previous statements, you don’t actually need to submit any “readmes” or justification for the work that you’ve done.
You are responsible for verifying on Gradescope that your submission compiles. Submissions that do not compile will receive substantial deductions.
Finally, good luck! STUB FILES HERE
Part 1 (Vending Machine)
You have been provided four classes that implement the functionality for a Vending Machine from which a user can enter a product number and some payment and have the machine dispense the requested product.
The classes are:
Item.java:- an
Itemhas two fields, one for anItem’s price, and one for its uniqueid. Item.javahas a single simple constructor and two basic “getters” for its fields.
- an
Payment.java:- A
Paymentrepresents a sum of money. Its fields track how many dollars ($1.00), quarters ($0.25), and dimes ($0.10) thePaymentconsists of. Payment.javahas two methods:- one for calculating the value of the dollars and coins in the
Payment - one for returning a new
Paymentwith enough dollars, quarters, and dimes to represent the difference in value between the currentPaymentand some other amount of money.
- one for calculating the value of the dollars and coins in the
- A
Transaction.javarepresents the way that money and items are returned from the Vending Machine to the user. This class is provided for you in its entirety.- Note that a
Transactionwill always have somePaymentand someItemassociated with it, as well as abooleanflag that indicates whether or not theTransactionresulted in a successful purchase. - A successful
Transactionrepresents a completed purchase, and should contain the change that the user receives from their purchase as aPayment, theItemthey bought, and have theisSuccessfulflag set totrue. - An unsuccessful
Transactionsrepresent a purchase attempt where the user did not provide enough money in theirPayment. TheItemin theTransactionshould still be theItemthat the user attempted to purchase, and theisSuccessfulflag should be set tofalse.
- Note that a
VendingMachine.javarepresents the Vending Machine itself.- A
VendingMachinehas one field: an array ofItems that represents their inventory VendingMachine.javahas two crucial methods.selectItemByNumbersearches through the inventory, checking to see whichItemmatches the provided ID and returns that item.attemptPurchaseperforms the function of having a customer enter anItemid, provide some money as aPayment, and return aTransactionrepresenting what happens next. If the customer gave enough money, theTransactionshould contain the customer’s change, their requested item, and be marked as successful. Otherise, it should contain the insufficient payment the customer provided, the requested item, and be marked as not successful.
- A
Fill in the specified methods. Your code must compile, and the methods that you fill in should conform to the specification outlined above and repeated in the method headers in the files you receive. Do not change any fields or method signatures whatsoever.
Methods to fill in:
- In
Payment.javapublic double calculateValue()public Payment makeChangeForPurchase(double itemCost)
- In
VendingMachine.javapublic Item selectItemByNumber(int itemID)public Transaction attemptPurchase(int itemID, Payment currencyProvided)
Part 2 (Individual Functions)
Checking Product Key Validity
Sometimes, software that you download and install will require you to provide a product activation key in order to actually use that software. This is a measure intended to reduce software piracy.
You’re tasked with validating product keys for CIT591-OS, an operating system just for MCIT students. The product keys are provided as Strings, and must have the following format:
- They must have exactly
21characters. - The first three characters must represent an integer between
0-400, inclusive. - The next three characters should be exactly
"591". - The following three characters should all be letter characters exactly equal to
"CIT". - The next seven characters should represent an integer divisible by
7. Note that the number may have leading zeroes: both7777777and0000007would be valid character sequences for this region. - The final five characters should be all digit characters, i.e. characters
'0'-'9'. It does not matter what the value of this underlying integer is.
Methods available in the Character class like Character.isDigit(char c), in the String class (length(), charAt()), and in the Integer class like Integer.valueOf(String s) will all be helpful for you here.
🚨 🚨 🚨 Required function signature 🚨 🚨 🚨
public static boolean isValidProductKey(String key)
Examples
| Input | Output | Reason |
|---|---|---|
| 110591CIT777776312345 | true |
all conditions met |
| 110591CIT7777763123456 | false |
the String is too long (22 characters) |
| 411591CIT777776312345 | false |
first three digits don’t represent an integer between 0-400 |
| 001110CIT777776312345 | false |
Green group of digits don’t represent exactly 591 |
| 001591CIS777776312345 | false |
Purple substring is not exactly "CIT" |
| 001591CIT777771012345 | false |
Blue substring is not divisible by 7 |
| 001591CIT0000007000P5 | false |
Orange substring has a non-numeric character 'P' inside it. |
Interpreting Robot Behavior
A popular application of robotics is the automatic vaccuum cleaner. These robots leave from a charging station and take a semi-randomized path throughout their cleaning area. Many of the robots are not designed to return to their charging station, but it can be troublesome in large spaces to locate where the robot ended up after its cleaning cycle.
In order to find the robot, it broadcasts back to its charging station which move it makes every step of the way. The move history can be used to determine the exact coordinate location of the robot when it stops.
For this problem, assume that the robot’s possible moves are traveling one foot "up", "down", "left", and "right" on an imaginary grid imposed over the cleaning area. Further assume that the robot starts at the origin, i.e. x = 0 and y = 0 before the robot makes any moves.
"up"increases the y-coordinate of the robot by1"down"decreases the y-coordinate by1."right"increases the x-coordinate of the robot by1"left"decreases the x-coordinate of the robot by1.- any other
Stringnot exactly equal to one of these results from random noise and should not be interpreted as a move from the robot (no change in position.)
Given an array of Strings that represent the robot’s completed moves (only "up", "down", "left", or "right"), return a two-element array containing the x-position and then the y-position of the robot.
🚨 🚨 🚨 Required function signature 🚨 🚨 🚨
public static int[] parseMovementList (String[] moves)
Examples
| Input | Output |
|---|---|
{ "up", "up", "left", "right", "left" } |
{ -1, 2 } |
{ "down", "up", "left", "right" } |
{ 0, 0 } |
{ "left", "down", "right", "up" } |
{ 0, 0 } |
{ "up", "up", "up", "up", "left", "left" } |
{ -2, 4 } |
{ "down", "right", "LEFT" } |
{ 1, -1 } |
{ "down", "sjflksd", "right", "uP", "rIgHt", "left" } |
{ 0, -1 } |
Wrap Up
Submit your code to Gradescope. You should submit only the files containing methods that you are asked to write (VendingMachine.java, Payment.java, and PartTwo.java). Other files will be ignored. Again, take care that you have not changed any function signatures or changed anything about the other classes that were provided to you.