Structural Patterns  «Prev  Next»
Lesson 9 Course Project, part 5
ObjectiveWrite a Class that contains the vehicles currently backed up at the intersection

Class Vehicle Intersection

In order to manage a traffic light intersection efficiently, a comprehensive understanding of the traffic flow is indispensable. Below is a Java class named `TrafficIntersection` that encapsulates the core aspects of an intersection, along with its queues for pedestrians, bicycles, cars, and buses in each direction.
import java.util.LinkedList;
import java.util.Queue;

public class TrafficIntersection {
    
    private Queue<String> northQueue;
    private Queue<String> southQueue;
    private Queue<String> eastQueue;
    private Queue<String> westQueue;

    // Initialize the intersection with empty queues
    public TrafficIntersection() {
        this.northQueue = new LinkedList<>();
        this.southQueue = new LinkedList<>();
        this.eastQueue = new LinkedList<>();
        this.westQueue = new LinkedList<>();
    }

    // Method to add a vehicle to a queue
    public void addVehicle(String direction, String vehicleType) {
        switch (direction) {
            case "north":
                northQueue.add(vehicleType);
                break;
            case "south":
                southQueue.add(vehicleType);
                break;
            case "east":
                eastQueue.add(vehicleType);
                break;
            case "west":
                westQueue.add(vehicleType);
                break;
            default:
                throw new IllegalArgumentException("Invalid direction");
        }
    }
 
    // Method to determine which direction's light should be green
    public String optimalGreenLight() {
        int northSize = northQueue.size();
        int southSize = southQueue.size();
        int eastSize = eastQueue.size();
        int westSize = westQueue.size();

        // Evaluate which direction has the maximum traffic
        int maxTraffic = Math.max(Math.max(northSize, southSize), Math.max(eastSize, westSize));

        // Return the direction where the light should be green
        if (maxTraffic == northSize) return "north";
        if (maxTraffic == southSize) return "south";
        if (maxTraffic == eastSize) return "east";
        return "west";
    }

    // Method to clear the queue in the direction where the light turns green
    public void clearQueue(String direction) {
        switch (direction) {
            case "north":
                northQueue.clear();
                break;
            case "south":
                southQueue.clear();
                break;
            case "east":
                eastQueue.clear();
                break;
            case "west":
                westQueue.clear();
                break;
            default:
                throw new IllegalArgumentException("Invalid direction");
        }
    }
}

Component Explanations

  1. Queues for Each Direction (`northQueue`, `southQueue`, `eastQueue`, `westQueue`):
    These queues represent the line-up of vehicles in each direction at the intersection. They are implemented using Java's `LinkedList` class to ensure efficient enqueuing and dequeuing operations.
  2. Method `addVehicle(String direction, String vehicleType)`:
    This method is responsible for adding a vehicle to a specific direction's queue. The `direction` parameter accepts the direction ("north", "south", "east", "west"), and `vehicleType` accepts the type of vehicle ("pedestrian", "bicycle", "car", "bus").
  3. Method `optimalGreenLight()`:
    This critical method evaluates the current state of traffic in each direction and determines where the light should turn green for optimal flow. It returns the direction as a `String`.
  4. Method `clearQueue(String direction)`:
    This method clears the queue for the given direction once the light turns green, symbolizing that the traffic has cleared.

Optimal Strategy for Traffic Light Control

The method `optimalGreenLight()` is designed to ensure that the light turns green in the direction with the maximum number of queued vehicles. This is a straightforward yet effective strategy for ensuring that the largest backlog of traffic is cleared first, facilitating smooth overall traffic flow. This technique addresses both the condition of optimal queue clearance and the stipulation that the light should be green more often in the direction with more traffic. By implementing the class and methods as described, the `TrafficIntersection` class serves as a robust and efficient model for managing a real-world traffic light intersection.
Each direction at the intersection has a queue of pedestrians, bicycles, cars, buses, and so forth. Ideally, the lights in each direction should be green long enough for the queue to empty out. Furthermore, the light should be green more often for the direction with more traffic. When the light is green more often in the direction of more traffic, the traffic flows smoothly.
If this is not the case, you have a traffic jam. Figure out the optimal strategy for turning the lights green and red. For this portion of the class, concentrate on the vehicles that are queued at the intersection.
One of the purposes of a simulation like this is to figure out what the optimal strategy is for turning the lights green and red. For this part of the course project, you will concentrate on the vehicles that are queued at the intersection. You will need to create a VehicleQueue class that contains the vehicles.

An intersection is the junction at grade of two or more roads either meeting or crossing. An intersection may be
  1. three-way (a T junction or Y junction (the latter also known as a fork)),
  2. four-way (a crossroads), or
  3. have five or more arms.

Vehicle Intersection
Vehicle traffic at an intersection

Backedup Vehicles Intersection - Exercise

Now it is time to continue your course project.
Backedup Vehicles Intersection - Exercise