Link Search Menu Expand Document

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 (,, and 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:

    • an Item has two fields, one for an Item’s price, and one for its unique id.
    • has a single simple constructor and two basic “getters” for its fields.
    • A Payment represents a sum of money. Its fields track how many dollars ($1.00), quarters ($0.25), and dimes ($0.10) the Payment consists of.
    • has two methods:
      • one for calculating the value of the dollars and coins in the Payment
      • one for returning a new Payment with enough dollars, quarters, and dimes to represent the difference in value between the current Payment and some other amount of money.
  • represents 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 Transaction will always have some Payment and some Item associated with it, as well as a boolean flag that indicates whether or not the Transaction resulted in a successful purchase.
    • A successful Transaction represents a completed purchase, and should contain the change that the user receives from their purchase as a Payment, the Item they bought, and have the isSuccessful flag set to true.
    • An unsuccessful Transactions represent a purchase attempt where the user did not provide enough money in their Payment. The Item in the Transaction should still be the Item that the user attempted to purchase, and the isSuccessful flag should be set to false.
  • represents the Vending Machine itself.
    • A VendingMachine has one field: an array of Items that represents their inventory
    • has two crucial methods.
      • selectItemByNumber searches through the inventory, checking to see which Item matches the provided ID and returns that item.
      • attemptPurchase performs the function of having a customer enter an Item id, provide some money as a Payment, and return a Transaction representing what happens next. If the customer gave enough money, the Transaction should 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.

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
    • public double calculateValue()
    • public Payment makeChangeForPurchase(double itemCost)
  • In
    • public 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 21 characters.
  • 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: both 7777777 and 0000007 would 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)


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 by 1
  • "down" decreases the y-coordinate by 1.
  • "right" increases the x-coordinate of the robot by 1
  • "left" decreases the x-coordinate of the robot by 1.
  • any other String not 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)


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 (,, and 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.