Question 51
A school system models course requests using the CourseRequest class. A request has a course name, a priority value, and a flag indicating whether the request has already been fulfilled.
public class CourseRequest
{
private String courseName;
private int priority;
private boolean fulfilled;
public CourseRequest(String name, int p, boolean done)
{
courseName = name;
priority = p;
fulfilled = done;
}
public String getCourseName()
{
return courseName;
}
public int getPriority()
{
return priority;
}
public boolean isFulfilled()
{
return fulfilled;
}
}
public class CourseQueue
{
private ArrayList<CourseRequest> requests;
public CourseQueue(ArrayList<CourseRequest> data)
{
requests = data;
}
public ArrayList<String> topOpenRequests(int minPriority)
{ /* to be written */ }
}
Write the method topOpenRequests, which returns a new ArrayList<String> containing the names of all unfulfilled requests whose priority is at least minPriority and is equal to the highest priority among all such requests. Names must appear in the same order as the corresponding requests appear in requests. If no request qualifies, the method returns an empty list.
Main Solution
Sample Solution
public ArrayList<String> topOpenRequests(int minPriority)
{
ArrayList<String> result = new ArrayList<String>();
int best = 0;
boolean found = false;
for (int i = 0; i < requests.size(); i++)
{
CourseRequest req = requests.get(i);
if (!req.isFulfilled() && req.getPriority() >= minPriority)
{
if (!found || req.getPriority() > best)
{
best = req.getPriority();
found = true;
}
}
}
if (!found)
{
return result;
}
for (int i = 0; i < requests.size(); i++)
{
CourseRequest req = requests.get(i);
if (!req.isFulfilled() && req.getPriority() == best)
{
result.add(req.getCourseName());
}
}
return result;
}
Scoring (9 pts): Points are earned for creating a result list, traversing all requests, using accessor methods, filtering by unfulfilled status and minimum priority, finding the best qualifying priority, handling no qualifying requests, collecting all ties in original order, adding course names rather than request objects, and returning the result.
Answer images

