Is intended to be an abstract and simplified represent of a drone racing Among o
ID: 3799361 • Letter: I
Question
Is intended to be an abstract and simplified represent of a drone racing Among other things this object should be able to list the outcome of the competition. Array of racing drones. number of drone racers in the competition. The number of drone racers (count) must always be accurate. must be enough to hold at least 20 drone racers. Public Getter for count public (drone racer) {} public void show Results {} {} Displays a ist of the racing drones in order of fastest to complete, followed by drone A* aw not complete the competition. Include what number competitor the drone the time completed, and the name. For example: Rank L Seed Racer (Contestant #3) @ 49.453 seconds Rank 2. Minerva (Contestant #7) @ 51.011 seconds Rank 3. Racing Drone (Contestant #2) @> 67.332 seconds Rank 4 Racing Drone (Contestant #1) - Did not complete Rank 5. Racing Drone (Contestant #4) - Did not complete Rank 6. Smirnoff (Contestant #5) - Did not complete Rank 7 Racing Drone (Contestant #6) - Did not completeExplanation / Answer
Drone.java:
public class Drone {
// drone class has a name and speed of drone
private String name;
private int speed;
public Drone(String name, int speed) {
this.speed = speed;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
Competition.java:
public class Competition {
private Drone[] drones = new Drone[20];
private int count = 0;
// get the count of the racers in the competition
public int getCount() {
return count;
}
// add a racer to competition
public void addRacer(Drone racer) {
if (count == 20) {
throw new IllegalArgumentException(
"20 Racers have already been added.");
}
drones[count++] = racer;
}
public void showResults() {
// lets assume race is for 10000m. We will compute the time based on the
// speed of each drone
// find the top 3 drones index, returns -1 as index if element doesn't
// exist
int a = find1stDrone();
int b = find2ndDrone(a);
int c = find3rdDrone(a, b);
int rank = 1;
// expression Math.floor(((double) 100/ drones[a].getSpeed())*100)/100 just gives the value for time..
// we are assuming a 100 m distance . SO distance/speed gives us the time.
// print the information for top 3 contestant if available
if (a != -1) {
System.out.println("Rank " + rank++ + ". " + drones[a].getName()
+ "(Contestant #" + (a + 1) + ") @" + Math.floor(((double) 100/ drones[a].getSpeed())*100)/100 + " seconds");
}
if (b != -1) {
System.out.println("Rank " + rank++ + ". " + drones[b].getName()
+ "(Contestant #" + (b + 1) + ") @" + Math.floor(((double) 100/ drones[b].getSpeed())*100)/100 + " seconds");
}
if (c != -1) {
System.out.println("Rank " + rank++ + ". " + drones[c].getName()
+ "(Contestant #" + (c + 1) + ") @" + Math.floor(((double) 100/ drones[c].getSpeed())*100)/100 + " seconds");
}
// print information now for all other Drones
for (int i=0; i< count; i++) {
Drone d = drones[i];
if ((i != a) && (i != b) && (i != c)) {
System.out.println("Rank " + rank++ + ". " + d.getName()
+ "(Contestant #" + (i + 1) + ") - Did not complete");
}
}
}
// find the top speed drone
private int find1stDrone() {
int maxSpeed = 0;
int maxIndex = -1;
for (int i = 0; i < count; i++) {
Drone d = drones[i];
if (d.getSpeed() > maxSpeed) {
maxSpeed = d.getSpeed();
maxIndex = i;
}
}
return maxIndex;
}
// find the 2nd top speed drone
private int find2ndDrone(int indexOfFirstDrone) {
int maxSpeed = 0;
int maxIndex = -1;
for (int i = 0; i < count; i++) {
Drone d = drones[i];
if (i == indexOfFirstDrone) {
i++;
continue;
}
if (d.getSpeed() > maxSpeed) {
maxSpeed = d.getSpeed();
maxIndex = i;
}
i++;
}
return maxIndex;
}
// find the 3rd top speed drone
private int find3rdDrone(int indexOfFirstDrone, int indexOfSecondDrone) {
int maxSpeed = 0;
int maxIndex = -1;
for (int i = 0; i < count; i++) {
Drone d = drones[i];
if ((i == indexOfFirstDrone) || (i == indexOfSecondDrone)) {
i++;
continue;
}
if (d.getSpeed() > maxSpeed) {
maxSpeed = d.getSpeed();
maxIndex = i;
}
i++;
}
return maxIndex;
}
}
RaceDemo.java:
public class RaceDemo {
public static void main(String[] args) {
// creating different kind of drones with different speeds
Drone d1 = new Drone("Racing Drone", 20);
Drone d2 = new Drone("Racing Drone", 30);
Drone d3 = new Drone("Racing Drone", 40);
Drone d4 = new Drone("Minerva", 27);
Drone d5 = new Drone("Racing Drone", 47);
Drone d6 = new Drone("SmirnOff", 12);
// adding them to a competition
Competition competition = new Competition();
competition.addRacer(d1);
competition.addRacer(d2);
competition.addRacer(d5);
competition.addRacer(d4);
competition.addRacer(d6);
competition.addRacer(d3);
// printing the results
competition.showResults();
}
}
Sample Output:
Rank 1. Racing Drone(Contestant #3) @2.12 seconds
Rank 2. Racing Drone(Contestant #1) @5.0 seconds
Rank 3. SmirnOff(Contestant #5) @8.33 seconds
Rank 4. Racing Drone(Contestant #2) - Did not complete
Rank 5. Minerva(Contestant #4) - Did not complete
Rank 6. Racing Drone(Contestant #6) - Did not complete
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.