From the free course AP Computer Science A on Lykke

AP Practice 3

A class-design response earns credit by turning a precise specification into a usable Java class: private data, a constructor that establishes a valid object, and methods whose behavior matches the stated contract.

AP Practice 3

A class-design response earns credit by turning a precise specification into a usable Java class: private data, a constructor that establishes a valid object, and methods whose behavior matches the stated contract.

Task type: Class Design

In this original, unofficial practice task, a community center records reservations for study rooms. Each reservation stores a room name, a starting hour, and a duration. The class does not store a student name; all required fields and methods refer only to the reservation itself.

Prompt

Write a Java class named RoomReservation with the following requirements:

  • It has three private instance variables:
    • room, a String
    • startHour, an int
    • duration, an int
  • Its constructor accepts a room name, a starting hour, and a duration, storing those values in the corresponding instance variables.
  • getEndHour() returns the hour at which the reservation ends. The ending hour is calculated as $startHour + duration$.
  • isInRoom(String roomName) returns true if the reservation is for the specified room and false otherwise.
  • overlaps(RoomReservation other) returns true when the two reservations are for the same room and share at least one hour. Otherwise, it returns false.
  • move(int newStartHour) changes the reservation’s starting hour to newStartHour.
  • You may assume that all durations are positive and that all hour values are valid.

Timing strategy

Allow approximately 25 minutes for a class-design response. Spend the first few minutes extracting the required state and behavior. Then write the constructor and simplest methods first, test boundary cases mentally, and reserve time to check access modifiers, parameter types, and return types.

A useful contract map is:

Requirement Code location Main risk
Store room, start, duration Private instance variables Accidentally using static
Initialize one reservation Constructor Forgetting this
Compute ending time getEndHour() Using subtraction
Compare room names isInRoom and overlaps Using == for String objects
Detect shared time overlaps Treating touching reservations as overlapping
Change start time move Creating a new object instead of updating this one

Worked solution

public class RoomReservation
{
    private String room;
    private int startHour;
    private int duration;

    public RoomReservation(String roomName, int startingHour, int length)
    {
        room = roomName;
        startHour = startingHour;
        duration = length;
    }

    public int getEndHour()
    {
        return startHour + duration;
    }

    public boolean isInRoom(String roomName)
    {
        return room.equals(roomName);
    }

    public boolean overlaps(RoomReservation other)
    {
        boolean sameRoom = room.equals(other.room);

        boolean thisStartsBeforeOtherEnds =
            startHour < other.getEndHour();

        boolean otherStartsBeforeThisEnds =
            other.startHour < getEndHour();

        return sameRoom
            && thisStartsBeforeOtherEnds
            && otherStartsBeforeThisEnds;
    }

    public void move(int newStartHour)
    {
        startHour = newStartHour;
    }
}

Why the solution works

The constructor creates a complete reservation. For example:

RoomReservation first =
    new RoomReservation("Blue", 9, 2);

The object represents room Blue from hour $9$ through, but not including, hour $11$. Therefore, first.getEndHour() returns $11$.

The overlap test uses the half-open intervals $[startHour, endHour)$. Two reservations overlap exactly when:

$$ start_1 < end_2 \quad \text{and} \quad start_2 < end_1 $$

They must also have the same room. The strict < comparisons matter: a reservation from $9$ to $11$ does not overlap one from $11$ to $13$, because the first reservation has already ended when the second begins.

For example:

RoomReservation a = new RoomReservation("Blue", 9, 2);
RoomReservation b = new RoomReservation("Blue", 10, 1);
RoomReservation c = new RoomReservation("Blue", 11, 2);
RoomReservation d = new RoomReservation("Red", 10, 3);

a.overlaps(b) returns true: both use Blue, and hour $10$ is shared. a.overlaps(c) returns false: the intervals only touch at hour $11$. a.overlaps(d) returns false: their times intersect, but their rooms differ.

Rubric-aligned reasoning

A strong response should visibly satisfy these scoring targets:

  1. Class and state: The class is named correctly, and all three required variables are instance variables with the private modifier.
  2. Constructor: The constructor has the required parameter types and assigns every parameter to the correct field.
  3. Accessor behavior: getEndHour() returns the sum of the starting hour and duration.
  4. String comparison: Room names are compared with .equals, not ==.
  5. Overlap logic: The method requires both equal room names and intersecting time intervals.
  6. Mutation: move updates the existing object’s startHour.
  7. Java correctness: Method headers, braces, types, and return statements are syntactically valid.

Common misconception: == compares String contents

In Java, == compares whether two object references point to the same object, not whether two String objects contain the same characters. Use:

room.equals(roomName)

rather than:

room == roomName

Common misconception: touching intervals overlap

If one reservation ends at hour $11$ and another begins at hour $11$, they do not share an hour. Replacing < with <= would incorrectly classify adjacent reservations as overlapping.

Error-review routine

After writing your response, trace these three cases:

  • Same room, overlapping times: expected true
  • Same room, adjacent times: expected false
  • Different rooms, overlapping times: expected false

Then label every missed point as a specification error—you misunderstood a requirement—or an implementation error—you understood it but wrote incorrect Java. Fix the category, not only the line of code.

AP Practice 3 - AP Computer Science A - image 1
AP Practice 3 - AP Computer Science A - image 1
AP Practice 3 - AP Computer Science A - diagram 1
AP Practice 3 - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis