Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING JAVA Define D i to be a dock i and B j to be boat j where ; 1<= i<=4, and

ID: 645347 • Letter: U

Question

USING JAVA

Define Di to be a dock i and Bj to be boat j where ; 1<= i<=4, and Bj: 1<=j<=7. Assume that all docks are of the same length, and all boats are also of the same length, hence, the same number of boats could be docked in each dock. Only 5 boats can be docked in each dock. However, different docks have different docking rules according to their data structures, as are listed in the table below:

Dock

Structure

D1

Array

D2

Single Linked List

D3

Stack

D4

Queue

When a boat arrives to the marina, the marina's docking staff docks the boat in one of docks. However, customers may also ask the staff to undock their boats before they leave a trip. Keep in mind that any (docking/undocking) operation (corresponding to moving any boat into or out of any docks) cost 1 USD.

Test the program on each data structure using the following dock operations

Dock operation

Boat Number

Dock        

B1

Dock        

B4

Undock    

B1

Dock        

B5

Dock        

B7

Dock        

B3

Dock        

B2

Undock    

B2

Dock        

B1

Undock    

B7

Dock        

B6

Undock    

B1

Assume that the boats have priority based on their index numbering. i.e. B1 has highest and B7 has the lowest.

  Indicate the expected time and space complexity of your program

Dock

Structure

D1

Array

D2

Single Linked List

D3

Stack

D4

Queue

Explanation / Answer

javascript

let orbisStation, elliniaStation,
toOrbisWait, toElliniaWait,
toOrbis, toEllinia,
toOrbisCabin, toElliniaCabin;

function init(attachment) {
let remainingTimeUntilDeparture = untilNextQuarterHour();
if (remainingTimeUntilDeparture > 5 * 60 * 1000) {
//boat is undocked until last five minutes to departure
undock(remainingTimeUntilDeparture - 5 * 60 * 1000, false);
} else if (remainingTimeUntilDeparture > 60 * 1000) {
//boat is docked and boarding until last minute to departure
dock(remainingTimeUntilDeparture - 60 * 1000, false);
} else {
//doors closed until takeoff
closeDoors(remainingTimeUntilDeparture, false);
}
orbisStation = event.getMap(200000111);
elliniaStation = event.getMap(101000300);
toOrbisWait = event.getMap(101000301);
toElliniaWait = event.getMap(200000112);
toOrbis = event.getMap(200090010);
toEllinia = event.getMap(200090000);
toOrbisCabin = event.getMap(200090011);
toElliniaCabin = event.getMap(200090001);
}

function untilNextQuarterHour() {
let now = new Date();
return ((15 - now.getMinutes() % 15) * 60 - now.getSeconds()) * 1000 - now.getMilliseconds();
}

function undock(remainingTimeUntilArrival, transition) {
event.setVariable("board", false);
event.setVariable("0docked", false); //ellinia and orbis station ships
event.setVariable("1docked", false); //balrog ship
event.startTimer("dock", remainingTimeUntilArrival);

if (transition) {
orbisStation.showUndockShip();
elliniaStation.showUndockShip();

//warp players from waiting room to ship
toOrbisWait.transferPlayers(toOrbis.getId());
toElliniaWait.transferPlayers(toEllinia.getId());

//50% chance of invasion occurring
if (Math.floor(Math.random() * 2) == 0)
//spawn balrog a minute after departure
event.startTimer("balrog", remainingTimeUntilArrival - 9 * 60 * 1000);
}
}

function dock(remainingTimeUntilDoorsClosed, transition) {
event.setVariable("board", true);
event.setVariable("0docked", true); //ellinia and orbis station ships
event.setVariable("1docked", false); //balrog ship
event.startTimer("closedoors", remainingTimeUntilDoorsClosed);

if (transition) {
orbisStation.showDockShip();
elliniaStation.showDockShip();

//warp players from ship to stations
toOrbis.transferPlayers(orbisStation.getId());
toOrbisCabin.transferPlayers(orbisStation.getId());
toEllinia.transferPlayers(elliniaStation.getId());
toElliniaCabin.transferPlayers(elliniaStation.getId());
}
}

function closeDoors(remainingTimeUntilDeparture, transition) {
event.setVariable("board", false);
event.setVariable("0docked", true); //ellinia and orbis station ships
event.setVariable("1docked", false); //balrog ship
event.startTimer("takeoff", remainingTimeUntilDeparture);

if (transition) {
//reset maps to a clean state before every run
//respawn crates - sBoxItem0 (Reactor 9102000)
toOrbisCabin.resetReactors();
toElliniaCabin.resetReactors();

//clear any remnants of Crimson Balrogs invasion
toOrbis.clearMobs();
toEllinia.clearMobs();
}
}

function dockInvasion() {
event.setVariable("1docked", true);
toOrbis.showBalrogShip();
toEllinia.showBalrogShip();
toOrbis.spawnMob(8150000, 485, -221);
toOrbis.spawnMob(8150000, 485, -221);
toEllinia.spawnMob(8150000, -590, -221);
toEllinia.spawnMob(8150000, -590, -221);
}

function timerExpired(key) {
switch (key) {
case "dock":
//doors close in 4 minutes
dock(4 * 60 * 1000, true);
break;
case "closedoors":
//undock in a minute
closeDoors(60 * 1000, true);
break;
case "takeoff":
//next boat will arrive in 10 minutes
undock(10 * 60 * 1000, true);
break;
case "balrog":
dockInvasion();
break;
}
}