Overview
In Lab 5, you designed and programmed your own implementation of the JobManager abstract data type (ADT). In this lab, you're given a buggy implementation of the JobManager, and you're tasked with formulating its representation invariant (RI) and programming this RI in the form of a checkRep method, which ensures all conditions of the RI are met. Using your checkRep method, you should find & fix all the bugs in the given implementation. You are not allowed to change the representation (fields) of the implementation in any way.
The
JobManagerADT Operations (Same as Lab 5)
A
JobManageris seeded withn(the number of jobs it is managing). Each of thesenjobs are initially “unassigned” (i.e., not assigned to a robot). One can then addRobots, assign jobs to theRobots, and move jobs among theRobots.
The principal operations that the
JobManagerADT supports are as follows:
Create Instance
JobManager(int n)→ Create a newJobManagerinstance withnjobs with all IDs in[1, n].Add robot
addRobot(Robot robot)→ Add a new robot to the pool of robots that can complete jobs.Check has robot
hasRobot(Robot robot)→ Check ifrobotexists in the pool ofRobotsmaintained.Remove robot
removeRobot(Robot)→ Remove a robot from the pool of robots; all jobs assigned to the robot that was removed will be “unassigned”.Assign jobs
assignJobs(Robot robot, int jobId)→ Assign all unassigned jobs with idjobIdtorobot.Check if job is assigned
isAssigned(int jobId)→ Checks if the job with id =jobIdis assigned to anyRobotin the pool.Move some jobs
moveJobs(Robot srcRobot, dstRobot, int jobId)→ Move all jobs with idjobIDfromsrcRobottodstRobot.Move all jobs
moveJobs(Robot srcRobot, dstRobot)→ Move all jobs fromsrcRobottodstRobot.Get assigned robot
getRobot(int jobId)→ Identify theRobotthat is responsible for the job withjobIdas its id.Get highest priority job assigned to a robot
getHighestPriorityJob(Robot robot, int jobId)→ Assuming that the job id indicates priority (higher id ⇒ higher priority), obtain the highest priority job assigned torobotwith job idjobId.Check equality
equals(Object o)→ checks for equality with another object based on a specified notion of representation equality.Get hashcode
hashCode()→ returns a hashcode that obeys the contract withequals()and that tries to minimize hash collisions through aRobotid-aware hash function.
Tasks
You are tasked with completing the following. You are not allowed to change the representation (fields) of the provided implementation in any way.
Implement the
checkRep()method to throw anAssertionErrorif and only if the representation invariant (RI) for the provided implementation ofJobManageris violated.You cannot use the
assertkeyword incheckRep(). Instead, you mustthrow new AssertionError()if the representation invariant is violated. Otherwise, you will not pass the tests on PrairieLearn.
Run the provided test suite for
checkRep()inCheckRepTests.javaOnce you have
checkRep()working, callthis.checkRep()before all exit points in other methods likeassignJobs.Now, you can use
checkRep()to help you find bugs in the implementation.Hint: Look at which test cases were previously passing but started failing right after you added calls to
checkRep()in the relevant methods.
Fix all the bugs in the implementation, and ensure you’re passing all provided test cases in
JobManagerTests.java.Once you’re confident in your solution, submit your code on PrairieLearn.
Answer the questions in the second part of the lab on PrairieLearn. (Or you can start with this if you like too.)
Grading
You will answer some questions on PrairieLearn and you will submit your implementation to PrairieLearn as well. This programming task is worth 6 points and the other questions on PrairieLearn are worth 3 points.