Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class ABC { public interface Runner{ public double run(); } // Set up Swimmer

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

public class ABC {

public interface Runner{

public double run();

}

// Set up Swimmer

public interface Swimmer{

public double swim();

}

// Set up Biker

public interface Biker{

public double bike();

}

// Create Abstract Class

public abstract class Athlete{

private String name;

private String type;

private String lastName;

public String getName(){

return name;

}

public void setName(String name) {

this.name = name;

}

public String getType(){

return type;

}

public void setType(String type){

this.type = type;

}

public String getLastName(){

return lastName;

}

public void setLastName(String lastName){

this.lastName = lastName;

}

// ABSTRACT method disciplines()

public abstract String disciplines();

}

// Create subclass Triathlete

public class Triathlete extends Athlete implements Runner, Swimmer, Biker{

private double runSpeed;

private double swimSpeed;

private double bikeSpeed;

// Triathlete constructor to Take the name and the speed according to their performances

public Triathlete(String name,String lastName, double swimSpeed, double runSpeed, double bikeSpeed) {

this.setName(name);

this.setLastName(lastName);

this.setType("Triathlete");

this.runSpeed = runSpeed;

this.swimSpeed= swimSpeed;

this.bikeSpeed = bikeSpeed;

}

// add implement method

@Override

public double run() {

return runSpeed ;

}

@Override

public double bike() {

return bikeSpeed ;

}

@Override

public double swim() {

return swimSpeed ;

}

// Override method in the abstract Athlete class

@Override

public String disciplines() {

String disciplines = "During the Ironman triathlon, I swim 2.4 miles, bike 112 miles, then run "

+ "26.2 miles";

return disciplines;

}

}// end subclass Triathlete

// create subclass Duathlete

public class Duathlete extends Athlete implements Runner, Biker{

private double runSpeed;

private double bikeSpeed;

//Constructor to Take the name and the speed according to their performances

public Duathlete(String name,String lastName, double runSpeed, double bikeSpeed) {

this.setName(name);

this.setLastName(lastName);

this.setType("Duathlete");

this.runSpeed = runSpeed;

this.bikeSpeed = bikeSpeed;

}

// Add implements method

@Override

public double bike() {

return bikeSpeed ;

}

@Override

public double run() {

return runSpeed ;

}

// Override method in the abstract Athlete class

@Override

public String disciplines() {

String disciplines = "I run, bike, then sometimes run again. In a long-distance duathlon, I run 6.2"

+ "miles, bike 93 miles, then run 18.6 miles.";

return disciplines;

}

}// end subclass Duathlete

// Create Marathoner subclass

public class Marathoner extends Athlete implements Runner{

private double runSpeed;

//Constructor to Take the name and the speed according to their performances

public Marathoner(String name,String lastName, double runSpeed) {

this.setName(name);

this.setLastName(lastName);

this.setType("Marathoner");

this.runSpeed = runSpeed;

} // end constructor

// Add implements method

@Override

public double run() {

return runSpeed ;

}

// Override method in the abstract Athlete class

@Override

public String disciplines() {

String disciplines = "During a full marathon I run 26.2 miles! ";

return disciplines;

}

}// End Subclass Marathoner

// Create Subclass Aquathlete

public class Aquathlete extends Athlete implements Runner, Swimmer{

private double runSpeed;

private double swimSpeed;

// Begin Constructor to Take the name and the speed according to their performances

public Aquathlete(String name,String lastName, double runSpeed, double swimSpeed) {

this.setName(name);

this.setLastName(lastName);

this.setType("Aquathlete");

this.runSpeed = runSpeed;

this.swimSpeed = swimSpeed;

}// End Constructor

// Add implements method

@Override

public double swim() {

return swimSpeed ;

}

@Override

public double run() {

return runSpeed ;

}

// Override method in the abstract Athlete class

@Override

public String disciplines() {

String disciplines ="I run, swim then run again. In the Swedish OTILLO Championship the race " + "take place over 24 islands and requires" + "!";

return disciplines;

}

}// End Subclass Aquathlete

public static void main(String[] args) throws Exception{

ABC ag3 = new ABC();

Athlete [] athletes;

// Access the provided file

File athleteFile = new File("athletes.txt");

try {

// Read the file

BufferedReader reader = new BufferedReader(new FileReader(athleteFile));

// Read the file line by line

String line = reader.readLine();

// Convert Spring type contain Int to Int

int size = Integer.parseInt(line);

// Declare and created Array Size

athletes = new Athlete[size];

int i = 0;

// start while loop

while((line = reader.readLine())!= null) {

String [] lineElements = line.split(" ");

double runSpeed = Double.parseDouble(lineElements[1]);

double swimSpeed = Double.parseDouble(lineElements[2]);

double bikeSpeed = Double.parseDouble(lineElements[3]);

String athleteName = lineElements[4];

String lastName = (lineElements[5]);

// Switch Statement

switch(lineElements[0]) {

case "t":

athletes[i] = ag3.new Triathlete(athleteName, lastName, runSpeed, swimSpeed, bikeSpeed);

break;

case "d":

athletes[i] = ag3.new Duathlete(athleteName, lastName, runSpeed, bikeSpeed);

break;

case "a":

athletes[i] = ag3.new Aquathlete(athleteName, lastName, runSpeed, swimSpeed);

break;

case "m":

athletes[i] = ag3.new Marathoner(athleteName, lastName, runSpeed);

break;

}// end switch

i++;

}//end while loop

//Athletes that BIKE

System.out.println("ATHLETES THAT BIKE");

System.out.println("----------------------------------------------------- ");

ArrayList biker = findBiker(athletes);

for(Athlete newAthlete: biker) {

System.out.println(newAthlete.getName() + " "+ newAthlete.getLastName() + " is a " + newAthlete.getType() +" and is an athlete that bike at ");

displaySpeed(newAthlete);

System.out.println(" ");

}// end for loop

// end Athlete that Bike

//Athletes DO NOT Swim

System.out.println("ATHLETE THAT DO NOT SWIM");

System.out.println("----------------------------------------------------- ");

ArrayList noSwim = findDoNotSwim(athletes);

for(Athlete newAthlete: noSwim) {

System.out.println(newAthlete.getName() + " "+ newAthlete.getLastName() + " is a " + newAthlete.getType() + " and does not swim");

displaySpeed(newAthlete);

System.out.println(" ");

}// end for loop

// end Athlete do not bike

// Athlete Speed Calculation

System.out.println("THE SLOWEST RUNNER");

System.out.println("----------------------------------------------------- ");

int slowestRunner = findSlowestRunner(athletes);

System.out.println(" The Slowest Runner Is " + athletes[slowestRunner].getName() + " the " + athletes[slowestRunner].getType());

displaySpeed(athletes[slowestRunner]);

}// Calculation End

catch (IOException e)

{

e.printStackTrace();

}

}//end try

//List of those who bike

public static ArrayList findBiker(Athlete[] athletes){

ArrayList list = new ArrayList();

for(int i =0; i

if((athletes[i] instanceof Biker)) {

list.add(athletes[i]);

}// end if

}// end for loop

return list;

} // end list

//List of those who do not swim

public static ArrayList findDoNotSwim(Athlete[] athletes){

ArrayList list = new ArrayList();

for(int i = 0; i

if(!(athletes[i] instanceof Swimmer)) {

list.add(athletes[i]);

}// end if

}// end for loop

return list;

} // end list

// Find Slowest Runner Method

public static int findSlowestRunner(Athlete[] athletes){

double speed = 0;

double slowestSpeed =0;

int returnIndex = 0;

for(int i = 0; i< athletes.length; i++){

if(athletes[i] instanceof Runner){

speed += ((Runner)athletes[i]).run();

}

if(slowestSpeed >= speed){

slowestSpeed = speed;

returnIndex = i;

}

speed = 0;

}

return returnIndex;

}// end findSlowestRunner

public static void displaySpeed(Athlete athlete){

if(athlete instanceof Biker){

System.out.println(((Biker)athlete).bike() + " mph! ");

}

if(athlete instanceof Runner){

System.out.println( athlete.disciplines());

}

}

}

// I could not display correct Runner Speed as the number on the athletes.txt and could not display correctly the slowest runner. Please help me on this issue!

athletes.txt following:

7 t 9.52 2.88 25.84 Dave Scott d 8.32 0 23.47 Kenny Souza a 9.01 2.04 0 Hannah Kitchen m 6.67 0 0 Will Ferrell t 9.57 3.00 26.35 Mark Allen a 8.90 2.54 0 Thomas Roos m 7.01 0 0 George Bush

Final result following:

ATHLETES THAT BIKE! ---------------------- Dave Scott is a Triathlete and is an athlete that bikes at 25.84 mph During the Ironman triathlon, I swim 2.4 miles, bike 112 miles, then run 26.2 miles. Kenny Souza is a Duathlete and is an athlete that bikes at 23.47 mph I run, bike, then sometimes run again. In a long distance duathlon, I run 6.2 miles, bike 93 miles, then run 18.6 miles Mark Allen is a Triathlete and is an athlete that bikes at 26.35 mph During the Ironman triathlon, I swim 2.4 miles, bike 112 miles, then run 26.2 miles. ATHLETES THAT DO NOT SWIM! -------------------------- Kenny Souza is a Duathlete and does not swim I run, bike, then sometimes run again. In a long distance duathlon, I run 6.2 miles, bike 93 miles, then run 18.6 miles Will Ferrell is a Marathoner and does not swim During a full marathon I run 26.2 miles! George Bush is a Marathoner and does not swim During a full marathon I run 26.2 miles! SLOWEST RUNNER! -------------------------- Slowest runner is Will Ferrell who is a Marathoner with a running speed of 6.67 mph

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions