Question: With this code how would you go about and fix it in order to After the start button is pressed, swimmers enter through the entrance

With this code how would you go about and fix it in order to After the start button is pressed, swimmers enter through the entrance door one at a
time, in the race order of their swim stroke (e.g. Backstroke first), arriving swimmers must
wait if entrance door is occupied.
6. Swimmers line up at the starting blocks for their team in their race order (order of the
swim stroke). package medleySimulation;
import java.awt.Color;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
public class Swimmer extends Thread {
public static StadiumGrid stadium; // shared
private FinishCounter finish; // shared
private GridBlock currentBlock;
private Random rand;
private int movingSpeed;
private PeopleLocation myLocation;
private int ID; // thread ID
private int team; // team ID
private GridBlock start;
private CountDownLatch startLatch;
private Semaphore entranceSemaphore; // Semaphore for controlling entrance access
private boolean isBackstroke; // Flag to identify backstroke swimmers
private BlockingQueue backstrokeQueue; // Queue for backstroke synchronization
private CountDownLatch backstrokeLatch; // Latch to synchronize backstroke swimmers public enum SwimStroke {
Backstroke(1,2.5, Color.black),
Breaststroke(2,2.1, new Color(255,102,0)),
Butterfly(3,2.55, Color.magenta),
Freestyle(4,2.8, Color.red); private final double strokeTime;
private final int order; // in minutes
private final Color colour;
SwimStroke(int order, double sT, Color c){
this.strokeTime = sT;
this.order = order;
this.colour = c;
}
public int getOrder(){ return order; }
public Color getColour(){ return colour; }
}
private final SwimStroke swimStroke;
Swimmer(int ID, int t, PeopleLocation loc, FinishCounter f, int speed, SwimStroke s, CountDownLatch startLatch, Semaphore entranceSemaphore, BlockingQueue backstrokeQueue, CountDownLatch backstrokeLatch){
this.swimStroke = s;
this.ID = ID;
this.movingSpeed = speed;
this.myLocation = loc;
this.team = t;
this.start = stadium.returnStartingBlock(team);
this.finish = f;
this.rand = new Random();
this.startLatch = startLatch;
this.entranceSemaphore = entranceSemaphore;
this.isBackstroke =(s == SwimStroke.Backstroke);
this.backstrokeQueue = backstrokeQueue;
this.backstrokeLatch = backstrokeLatch;
}
public int getX(){ return currentBlock.getX(); }
public int getY(){ return currentBlock.getY(); }
public int getSpeed(){ return movingSpeed; }
public SwimStroke getSwimStroke(){ return swimStroke; }
public void enterStadium() throws InterruptedException {
entranceSemaphore.acquire(); // Acquire semaphore to enter
try {
currentBlock = stadium.enterStadium(myLocation);
sleep(200); // Wait a bit at the door, look around
} finally {
entranceSemaphore.release(); // Release semaphore after entering
}
}
public void goToStartingBlocks() throws InterruptedException {
int x_st = start.getX();
int y_st = start.getY();
// Move to the starting blocks
while (currentBlock != start){
sleep(movingSpeed *3); // Not rushing
currentBlock = stadium.moveTowards(currentBlock, x_st, y_st, myLocation);
}
// Wait until all backstroke swimmers are at their starting blocks
if (isBackstroke){
backstrokeQueue.put(this); // Add to queue
backstrokeLatch.countDown(); // Decrement the latch
backstrokeLatch.await(); // Wait for all backstroke swimmers to be ready
} else {
backstrokeLatch.await(); // Ensure non-backstroke swimmers wait for backstroke swimmers
}
}
private void dive() throws InterruptedException {
int x = currentBlock.getX();
int y = currentBlock.getY();
currentBlock = stadium.jumpTo(currentBlock, x, y -2, myLocation);
}
private void swimRace() throws InterruptedException {
int x = currentBlock.getX();
while (currentBlock.getY()!=0){
currentBlock = stadium.moveTowards(currentBlock, x,0, myLocation);
sleep((int)(movingSpeed * swimStroke.strokeTime)); // Swim
}
while (currentBlock.getY()!=(StadiumGrid.start_y -1)){
currentBlock = stadium.moveTowards(currentBlock, x, StadiumGrid.start_y, myLocation);
sleep((int)(movingSpeed * swimStroke.strokeTime)); // Swim
}
}
public void exitPool() throws InterruptedException {
int bench = stadium.getMaxY()- swimStroke.getOrder(); // They line up

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!