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 eg Backstroke first arriving swimmers must
wait if entrance door is occupied.
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 Color.black
Breaststroke new Color
Butterfly Color.magenta
Freestyle Color.red; private final double strokeTime;
private final int order; in minutes
private final Color colour;
SwimStrokeint 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;
Swimmerint 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.returnStartingBlockteam;
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.enterStadiummyLocation;
sleep; Wait a bit at the door, look around
finally
entranceSemaphore.release; Release semaphore after entering
public void goToStartingBlocks throws InterruptedException
int xst start.getX;
int yst start.getY;
Move to the starting blocks
while currentBlock start
sleepmovingSpeed ; Not rushing
currentBlock stadium.moveTowardscurrentBlock xst yst myLocation;
Wait until all backstroke swimmers are at their starting blocks
if isBackstroke
backstrokeQueue.putthis; Add to queue
backstrokeLatch.countDown; Decrement the latch
backstrokeLatch.await; Wait for all backstroke swimmers to be ready
else
backstrokeLatch.await; Ensure nonbackstroke swimmers wait for backstroke swimmers
private void dive throws InterruptedException
int x currentBlock.getX;
int y currentBlock.getY;
currentBlock stadium.jumpTocurrentBlock x y myLocation;
private void swimRace throws InterruptedException
int x currentBlock.getX;
while currentBlockgetY
currentBlock stadium.moveTowardscurrentBlock x myLocation;
sleepintmovingSpeed swimStroke.strokeTime; Swim
while currentBlockgetYStadiumGridstarty
currentBlock stadium.moveTowardscurrentBlock x StadiumGrid.starty myLocation;
sleepintmovingSpeed 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
