Question 1 · free_response
9 marksThis question involves a bicycle tour organizer who coordinates group rentals from different bike hubs throughout a city. A network of bike hubs is represented by the BikeHubNetwork class.
public class BikeHubNetwork
{
/**
* Returns the number of functional bikes, always greater than 0, available
* at the hub specified by hubId
* Precondition: 0 <= hubId <= 50
/
public int numAvailableBikes(int hubId)
{ / implementation not shown */ }
/**
* Decreases the inventory of available bikes at hubId by bikesTaken
* Preconditions: 0 <= hubId <= 50
* bikesTaken > 0
/
public void checkoutBikes(int hubId, int bikesTaken)
{ / implementation not shown / }
/ There may be instance variables, constructors,
and methods that are not shown. */
}
A tour guide leads excursions and is represented by the TourGuide class. You will write two methods of the TourGuide class.
public class TourGuide
{
/** The maximum group capacity this guide can supervise per tour stop */
private int maxCapacity;
/** The hub network this guide operates within */
private BikeHubNetwork network;
/**
* Assigns max to maxCapacity and net to network
* Precondition: max > 0
/
public TourGuide(int max, BikeHubNetwork net)
{ / implementation not shown */ }
/**
* Reserves and retrieves bikes for a tour stop at the hub specified by hubId,
* as described in part (a)
* Preconditions: 0 <= hubId <= 50
* maxCapacity > 0
/
public int organizeTour(int hubId)
{ / to be implemented in part (a) */ }
/**
* Leads a sequence of tours from startHubId to endHubId, inclusive,
* and returns the total compensation earned, as described in part (b)
* Preconditions: 0 <= startHubId <= endHubId <= 50
* maxCapacity > 0
/
public int guideShift(int startHubId, int endHubId)
{ / to be implemented in part (b) / }
/ There may be instance variables, constructors,
and methods that are not shown. */
}
Part (a)
Write the organizeTour method, which updates hub inventory and returns the number of bikes reserved by the tour guide for the hub specified by hubId.
The helper method numAvailableBikes in BikeHubNetwork returns the number of bikes currently ready at a given hub. The tour guide takes as many bikes as are available at the hub, up to a maximum of maxCapacity.
The helper method checkoutBikes in BikeHubNetwork must be invoked to deduct the reserved bikes from the hub's inventory so that other guides cannot claim the same bikes.
Complete method organizeTour below. You must use numAvailableBikes and checkoutBikes appropriately to receive full credit.
/**
* Reserves and retrieves bikes for a tour stop at the hub specified by hubId,
* as described in part (a)
* Preconditions: 0 <= hubId <= 50
* maxCapacity > 0
*/
public int organizeTour(int hubId)
Part (b)
Write the guideShift method, which processes a series of tour stops across consecutive hub IDs from startHubId through endHubId, inclusive, and returns the total payment earned in dollars.
For each hub visited during the shift:
For example, if maxCapacity is 4, and the guide visits hubs 8 through 11 with the following bike counts:
Total compensation returned would be \(37 + 16 + 21 + 37 = 111\) dollars.
Complete method guideShift below. Assume that organizeTour works as specified, regardless of what you wrote in part (a). You must use organizeTour appropriately to receive full credit.
/**
* Leads a sequence of tours from startHubId to endHubId, inclusive,
* and returns the total compensation earned, as described in part (b)
* Preconditions: 0 <= startHubId <= endHubId <= 50
* maxCapacity > 0
*/
public int guideShift(int startHubId, int endHubId)
public class BikeHubNetwork
{
/**
* Returns the number of functional bikes, always greater than 0, available
* at the hub specified by hubId
* Precondition: 0 <= hubId <= 50
/
public int numAvailableBikes(int hubId)
{ / implementation not shown */ }
/**
* Decreases the inventory of available bikes at hubId by bikesTaken
* Preconditions: 0 <= hubId <= 50
* bikesTaken > 0
/
public void checkoutBikes(int hubId, int bikesTaken)
{ / implementation not shown / }
/ There may be instance variables, constructors,
and methods that are not shown. */
}
A tour guide leads excursions and is represented by the TourGuide class. You will write two methods of the TourGuide class.
public class TourGuide
{
/** The maximum group capacity this guide can supervise per tour stop */
private int maxCapacity;
/** The hub network this guide operates within */
private BikeHubNetwork network;
/**
* Assigns max to maxCapacity and net to network
* Precondition: max > 0
/
public TourGuide(int max, BikeHubNetwork net)
{ / implementation not shown */ }
/**
* Reserves and retrieves bikes for a tour stop at the hub specified by hubId,
* as described in part (a)
* Preconditions: 0 <= hubId <= 50
* maxCapacity > 0
/
public int organizeTour(int hubId)
{ / to be implemented in part (a) */ }
/**
* Leads a sequence of tours from startHubId to endHubId, inclusive,
* and returns the total compensation earned, as described in part (b)
* Preconditions: 0 <= startHubId <= endHubId <= 50
* maxCapacity > 0
/
public int guideShift(int startHubId, int endHubId)
{ / to be implemented in part (b) / }
/ There may be instance variables, constructors,
and methods that are not shown. */
}
Part (a)
Write the organizeTour method, which updates hub inventory and returns the number of bikes reserved by the tour guide for the hub specified by hubId.
The helper method numAvailableBikes in BikeHubNetwork returns the number of bikes currently ready at a given hub. The tour guide takes as many bikes as are available at the hub, up to a maximum of maxCapacity.
The helper method checkoutBikes in BikeHubNetwork must be invoked to deduct the reserved bikes from the hub's inventory so that other guides cannot claim the same bikes.
Complete method organizeTour below. You must use numAvailableBikes and checkoutBikes appropriately to receive full credit.
/**
* Reserves and retrieves bikes for a tour stop at the hub specified by hubId,
* as described in part (a)
* Preconditions: 0 <= hubId <= 50
* maxCapacity > 0
*/
public int organizeTour(int hubId)
Part (b)
Write the guideShift method, which processes a series of tour stops across consecutive hub IDs from startHubId through endHubId, inclusive, and returns the total payment earned in dollars.
For each hub visited during the shift:
- The base compensation is $8 per bike organized at that hub.
- An additional bonus of $5 is awarded for that hub stop if at least one of the following is true:
- The number of bikes organized equals maxCapacity.
- The hubId falls within the downtown zone of 10 through 20, inclusive.
For example, if maxCapacity is 4, and the guide visits hubs 8 through 11 with the following bike counts:
- Hub 8: 4 bikes organized → \(4 \times 8 + 5 = 37\) dollars (capacity reached)
- Hub 9: 2 bikes organized → \(2 \times 8 = 16\) dollars
- Hub 10: 2 bikes organized → \(2 \times 8 + 5 = 21\) dollars (downtown zone)
- Hub 11: 4 bikes organized → \(4 \times 8 + 5 = 37\) dollars (both conditions met, bonus applied once)
Total compensation returned would be \(37 + 16 + 21 + 37 = 111\) dollars.
Complete method guideShift below. Assume that organizeTour works as specified, regardless of what you wrote in part (a). You must use organizeTour appropriately to receive full credit.
/**
* Leads a sequence of tours from startHubId to endHubId, inclusive,
* and returns the total compensation earned, as described in part (b)
* Preconditions: 0 <= startHubId <= endHubId <= 50
* maxCapacity > 0
*/
public int guideShift(int startHubId, int endHubId)
Show answer & marking schemeHide answer & marking scheme
Worked solution
Part (a) Solution
public int organizeTour(int hubId)
{
int available = network.numAvailableBikes(hubId);
int toTake = available;
if (toTake > maxCapacity)
{
toTake = maxCapacity;
}
network.checkoutBikes(hubId, toTake);
return toTake;
}
Part (b) Solution
public int guideShift(int startHubId, int endHubId)
{
int totalEarned = 0;
for (int hub = startHubId; hub <= endHubId; hub++)
{
int bikes = organizeTour(hub);
int stopPay = bikes * 8;
if (bikes == maxCapacity || (hub >= 10 && hub <= 20))
{
stopPay += 5;
}
totalEarned += stopPay;
}
return totalEarned;
}
Marking scheme
Part (a): organizeTour (4 Points)
- Point 1: Calls BikeHubNetwork method(s) on network (1 pt)
- Do not award if: calls methods without a reference object or on something other than network.
- Point 2: Compares available bikes and maxCapacity to determine the number of bikes to organize (1 pt)
- Point 3: Calls numAvailableBikes with hubId and checkoutBikes with hubId and correct number of bikes to take (1 pt)
- Point 4: Returns calculated integer number of bikes organized (1 pt)
Part (b): guideShift (5 Points)
- Point 5: Iterates through all hub IDs from startHubId to endHubId, inclusive (1 pt)
- Point 6: Calls organizeTour with int parameter inside the loop (1 pt)
- Do not award if: called multiple times per iteration, which would cause unintended multiple inventory deductions.
- Point 7: Calculates base compensation ($8 per bike organized) for each hub stop (1 pt)
- Point 8: Correctly determines bonus condition (evaluates whether bikes organized equals maxCapacity OR current hub ID is between 10 and 20 inclusive, adding $5 at most once) (1 pt)
- Point 9: Accumulates total pay across all iterations and returns the accumulated total (1 pt)