Boats pass marks at times according to boat speed and distance of mark from start.

This commit is contained in:
cbt24
2017-03-06 16:20:20 +13:00
parent 4f6ab7b4bc
commit 7c0d9203a5
8 changed files with 105 additions and 48 deletions
+16 -7
View File
@@ -12,16 +12,25 @@ public class App
test.add(5);
test.add(1, 8);
Boat boat1 = new Boat("ORACLE TEAM USA");
Boat boat2 = new Boat("Artemis Racing");
Boat boat3 = new Boat("Emirates Team New Zealand");
Boat boat4 = new Boat("Groupama Team France");
Boat boat5 = new Boat("Land Rover BAR");
Boat boat6 = new Boat("SoftBank Team Japan");
Boat boat1 = new Boat("ORACLE TEAM USA", 10);
Boat boat2 = new Boat("Artemis Racing", 8);
Boat boat3 = new Boat("Emirates Team New Zealand", 12);
Boat boat4 = new Boat("Groupama Team France", 11);
Boat boat5 = new Boat("Land Rover BAR", 10);
Boat boat6 = new Boat("SoftBank Team Japan", 9);
RaceMarker mark1 = new RaceMarker("Start", 0);
RaceMarker mark2 = new RaceMarker("Mark", 120);
RaceMarker mark3 = new RaceMarker("Leeward Gate", 240);
RaceMarker mark4 = new RaceMarker("Windward Gate", 360);
RaceMarker mark5 = new RaceMarker("Leeward Gate", 480);
RaceMarker mark6 = new RaceMarker("Finish", 600);
Boat[] all_boats = {boat1, boat2, boat3, boat4, boat5, boat6};
Boat[] boats = {all_boats[0], all_boats[1]};
RaceMarker[] marks = {mark1, mark2, mark3, mark4, mark5, mark6};
Race race = new Race(boats);
Race race = new ConstantVelocityRace(boats, marks);
race.simulateRace();
}
+7 -1
View File
@@ -7,8 +7,10 @@ import java.util.ArrayList;
*/
public class Boat {
private String name;
private double velocity;
public Boat(String name){
public Boat(String name, double velocity){
this.velocity = velocity;
this.name = name;
}
@@ -16,6 +18,10 @@ public class Boat {
return name;
}
public double getVelocity() {
return velocity;
}
public String toString(){
return getName();
}
@@ -0,0 +1,22 @@
package seng302;
import java.util.Collections;
/**
* Created by cbt24 on 6/03/17.
*/
public class ConstantVelocityRace extends Race {
public ConstantVelocityRace(Boat[] boats, RaceMarker[] marks) {
super(boats, marks);
generateRace();
}
private void generateRace() {
for(Boat boat : boats) {
for(RaceMarker raceMarker : marks) {
events.add(new Event(raceMarker, boat, (int)(raceMarker.getDistance() / boat.getVelocity())));
}
}
Collections.sort(events, (o1, o2) -> o1.getTime() - o2.getTime());
}
}
+4 -4
View File
@@ -8,12 +8,12 @@ import java.util.Collections;
* Created by fwy13 on 3/03/17.
*/
public class Event {
private String name;
private RaceMarker raceMarker;
private Boat boat;
private int time;
public Event(String name, Boat boat, int time){
this.name = name;
public Event(RaceMarker raceMarker, Boat boat, int time){
this.raceMarker = raceMarker;
this.boat = boat;
this.time = time;
}
@@ -27,6 +27,6 @@ public class Event {
}
public String toString() {
return boat.getName() + " passed " + name + " at " + time;
return boat.getName() + " passed " + raceMarker.toString() + " at " + time + " seconds.";
}
}
+5 -19
View File
@@ -8,16 +8,16 @@ import java.util.*;
*/
public class Race {
Boat[] boats;
RaceMarker[] marks;
LinkedList<Event> events = new LinkedList<Event>();
public Race(Boat[] boats){
public Race(Boat[] boats, RaceMarker[] marks){
this.boats = boats;
this.marks = marks;
}
public void simulateRace(){
String[] marks = {"Mark", "Leeward Gate", "Windward Gate", "Leeward Gate", "Finish"};
randomiseRace(marks);
//printRace();
//printRace()
for (Event event: events) {
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@@ -25,22 +25,8 @@ public class Race {
public void run(){
System.out.println(event);
}
}, event.getTime() * 1000);
}, event.getTime() * 100);
}
}
public void randomiseRace(String[] eventNames){
Random rand = new Random();
for (Boat boat: this.boats){
events.add(new Event("Start", boat, 0));
int prevTime = 0;
for (String markName: eventNames){
int time = rand.nextInt(12) + 6;
events.add(new Event(markName, boat, prevTime + time));
prevTime += time;
}
}
Collections.sort(events, (o1, o2) -> o1.getTime() - o2.getTime());
}
}
+22
View File
@@ -0,0 +1,22 @@
package seng302;
/**
* Created by cbt24 on 6/03/17.
*/
public class RaceMarker {
String name;
double distance;
public RaceMarker(String name, double distance) {
this.name = name;
this.distance = distance;
}
public double getDistance() {
return distance;
}
public String toString() {
return name;
}
}
+28
View File
@@ -0,0 +1,28 @@
package seng302;
import java.util.Collections;
import java.util.Random;
/**
* Created by cbt24 on 6/03/17.
*/
public class RandomisedRace extends Race {
public RandomisedRace(Boat[] boats, RaceMarker[] marks) {
super(boats, marks);
generateRace();
}
private void generateRace() {
Random rand = new Random();
for (Boat boat: this.boats){
events.add(new Event(new RaceMarker("Start", 0), boat, 0));
int prevTime = 0;
for (RaceMarker raceMarker: marks){
int time = rand.nextInt(12) + 6;
events.add(new Event(raceMarker, boat, prevTime + time));
prevTime += time;
}
}
Collections.sort(events, (o1, o2) -> o1.getTime() - o2.getTime());
}
}