Question

In: Computer Science

** Only bold needs to be answered In Java, implement the Athlete, Swimmer, Runner, and AthleteRoster...

** Only bold needs to be answered

In Java, implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class must be in separate file. Draw an UML diagram with the inheritance relationship of the classes.

1. The Athlete class
a. All class variables of the Athlete class must be private.

b.is a class with a single constructor: Athlete(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender). All arguments of the constructor should be stored as class variables. There should only be getter methods for first and last name variables. There should be no getters or setters for birthYear, birthMonth, birthDay.

c. Getter and setter methods for the gender of the athlete. The method setGender accepts a character and store it as a class variable. Characters 'm', 'M' denotes male, 'f' or 'F' denotes male or female. Any other char will be treated as undeclared. The method getGender return either the word "male" or "female" or “undeclared”.

  1. The method computeAge() takes no arguments and returns the athletes computed age (as of today's date) as a string of the form "X years, Y months and Z days". Hint: Use the LocalDate and Period classes.

    i. e.g. "21 years, 2 months and 3 days". ii. e.g. "21 years and 3 days".

    iii. e.g. "21 years and 2 months". iv. e.g. "21 years".

    v. e.g. "2 months and 3 days".

  2. The method public long daysSinceBirth() takes no arguments and returns a long which is the number of days since the athlete’s date of birth to the current day. Hint: Use the LocalDate and ChronoUnit classes.

  3. The toString method returns a string comprised of the results ofgetFname, getLName and computeAge. E.g.

    i. “Bunny Bugs is 19 years and 1 day old”

  4. The equals method returns true if the name and date of birth of this athlete and the compared other athlete are the same, otherwise return false.

2. The Swimmer class
a. All class variables of the Swimmer class must be private.

  1. inherits from the Athlete class and has a single constructor,

    Swimmer(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender, String team). There should be a class variable for team. There should be a getter method for team.

  2. The Swimmer class stores swim events for the swimmer. There should be a class variable for events. A swimmer participates in one or more of these events. The addEvent method is oveloadedand either adds a single event for the swimmer public boolean addEvent(String singleEvent) or adds a group of events for the swimmer public boolean addEvent(ArrayList multiEvents). Each event is of type String. Duplicate events are not stored and return false if duplicate found.

  3. There should be a getter method that returns the class variable events.

  4. The overridden toString method returns a string comprised of the concatenation of the parent’s toString return plus " and is a swimmer for team: XXXXX in the following events: YYYYY; ZZZZZZ." E.g.

    i. “Missy Franklin is 24 years and 4 months old and is a swimmer for team: Colorado Stars. She participates in the following events: [100m freestyle, 100m backstroke, 50m backstroke]”

3. The Runner class
a. All class variables of the Runner class must be private.

  1. inherits from the Athlete class and has a single constructor,

    Runner(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender, String country). There should be a class variable for country. There should be a getter method for country.

  2. The Runner class stores race events for the runner. There should be a class variable for events. Each event is a Distance. The list of valid events is given below:
    M100, M200, M400, M3000, M5000, M10000. A runner participates inone or more of these events. The addEvent method is oveloadedand either adds a single event for the runner public boolean addEvent(String singleEvent) or adds a group of events for the runner public boolean addEvent(ArrayList multiEvents). Each event is of type String. Duplicate events are not stored and return false if duplicate found.

  1. There should be a getter method that returns the class variable events.

  2. The toString method returns a String in the form: " AAA BBBB is XX years, YY months and ZZ days. He is a citizen of CCCCCCC and is a DDDDDDDD who participates in these events: [MJ00, MK00, ML00]”. If she does not participate in M3000 or M5000 or M10000 then she is a sprinter. If she does not participate in M100 or M200 or M400 then she is a long-distance runner. Otherwise she is a super athlete. E.g.

    i. “Bunny Bugs is 19 years and 1 day old. His is a citizen of USA and is a long-distance runner who participates in these events: [M10000]”

4. The AthleteRoster class
a. All class variables of the AthleteRoster class must be private.

  1. Does not inherits from the Athlete class. The AthleteRoster class has a single constructor, AthleteRoster(String semster, int year). There should be class variables for semester and year. There should be getter methods for semester and year.

  2. The AthleteRoster class has only one method for adding Athlete to the roster, by using the boolean addAthlete(Athlete a)method. The method returns true if the athlete was added successfully, it returns false if the athlete object already exists in the roster and therefore was not added.

  3. Your AthleteRoster class will have only one class level data structure, an ArrayList, for storing athlete objects.

  4. The String allAthletesOrderedByLastName() method returns a string object with of all the names of the athletes (Swimmers, Runners, etc.) in ascending order of last names(a-z).

  5. The String allAthletesOrderedByAge() method returns a string object with of all the names of the athletes (Swimmers, Runners, etc.) in descending order of age(100-0).

  6. The String allRunnersOrderedByNumberOfEvents() method returns a string object with of all the names of the Runners only in ascending order of number of events they participate in (0-100).

Complete the code before the due date. Submission of the completed eclipse project is via github link posted on the class page. Add your UML drawing to the github repo. ________________________________________________________________________ Example output:

__________Example from AthleteDriver shown below

Gender is undeclared
Gender is female
Gender is male
ComputeAge method says 19 years and 4 days
First name is : Duck

DaysSinceBirth method says 6943 days Last name is : Daffy
Output of our toString correct?:
Duck Daffy is 19 years and 4 days old =======================================

Did we add M10000 successfully?: true
Did we unsuccessfully try to add M10000 again?: false
Did we successfully add multiple events?: true
Did we unsuccessfully try to add multiple events?: false
How many events does Bugs participate in?: 3
Gender is male
Output of our toString correct?:
Bunny Bugs is 19 years and 3 days old. His is a citizen of USA and is a super athlete who participates in these events: [M10000, M100, M3000] =======================================

Solutions

Expert Solution

//Java code

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class Athlete {
    private String lastName;
    private String firstName;
    private  int birthYear;
    private  int birthMonth;
    private  int birthDay;
    private  char gender;

    //Constructor

    public Athlete(String lastName, String firstName, int birthYear, int birthMonth, int birthDay, char gender) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.birthYear = birthYear;
        this.birthMonth = birthMonth;
        this.birthDay = birthDay;
        setGender(gender);
    }
    /**
     *  getter methods for first and last name variables.
     */
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
    /**
     * Getter and setter methods for the gender of the athlete.
     */
    public String getGender() {
        if(gender=='u')
            return "undeclared";
        else if(gender =='f'|| gender =='F')
            return "Female";
        else
            return "Male";

    }

    public void setGender(char gender) {
        if(gender=='m'||gender=='M')
        this.gender = gender;
        else if(gender=='F'|| gender=='m')
            this.gender =gender;
        else
            this.gender ='u';
    }
    /**
     * method computeAge() takes no arguments and returns
     * the athletes computed age (as of today's date)
     * as a string of the form "X years, Y months and Z days".
     * Hint: Use the LocalDate and Period classes.
     */
    public String computeAge()
    {
        LocalDate localDate = LocalDate.of(birthYear,birthMonth,birthDay);
        LocalDate localDateNow = LocalDate.now();
        Period diff = Period.between(localDate,localDateNow);
        if(diff.getYears()==0)
            return diff.getMonths()+" months  and "+diff.getDays()+" days.";
        else if(diff.getDays()==0)
            return diff.getYears()+" years and "+diff.getMonths()+" months.";
        else if(diff.getMonths()==0 && diff.getDays()==0)
            return diff.getYears()+" years.";
        else if(diff.getMonths()==0)
            return diff.getYears()+" years and "+diff.getDays()+" days.";
        else
            return diff.getYears()+" years, "+diff.getMonths()+" months, and "+diff.getDays()+" days.";
    }
    /**
     *  public long daysSinceBirth() takes no arguments
     *  and returns a long which is the number of days
     *  since the athlete’s date of birth to the current day.
     *  Hint: Use the LocalDate and ChronoUnit classes.
     */
    public long daysSinceBirth()
    {
        LocalDate localDate = LocalDate.of(birthYear,birthMonth,birthDay);
        LocalDate localDateNow = LocalDate.now();
        ChronoUnit chronoUnit= ChronoUnit.DAYS;
        long days=chronoUnit.between(localDate,localDateNow);
        return days;
    }

    @Override
    public String toString() {
        return firstName+" "+lastName+ " is "+computeAge();
    }

    /**
     * equals method returns true if the name and date of birth
     * of this athlete and the
     * compared other athlete are the same, otherwise return false.
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        Athlete athlete = ((Athlete)obj);
        return (this.firstName.equalsIgnoreCase(athlete.firstName) && this.computeAge().equalsIgnoreCase(athlete.computeAge()));
    }
}

//=============================================================

import java.util.ArrayList;

public class Swimmer extends Athlete{
     private String team;
     //store events

    private ArrayList<String> multiEvents;
     //constructor

    public Swimmer(String lastName, String firstName, int birthYear, int birthMonth, int birthDay, char gender, String team) {
        super(lastName, firstName, birthYear, birthMonth, birthDay, gender);
        this.team = team;
       multiEvents = new ArrayList<>();

    }
    //getter

    public String getTeam() {
        return team;
    }

    /**
     * adds a single event for the swimmer
     * @param singleEvent
     * @return
     */
    public boolean addEvent(String singleEvent)
    {
        for (String event:multiEvents
             ) {
            if(event.equalsIgnoreCase(singleEvent))
                return false;
        }
        multiEvents.add(singleEvent);
        return true;

    }
    /**
     * adds a group of events for the swimmer
     */
    public boolean addEvent(ArrayList<String> multiEvents)
    {
        boolean status =false;
        for (String event:multiEvents
             ) {
            if(this.multiEvents.contains(event))
                status = false;
            else
            {
                this.multiEvents.add(event);
                status = true;
            }
        }
      return status;
    }

    public ArrayList<String> getMultiEvents() {
        return multiEvents;
    }



    @Override
    public String toString() {
        String eventInfo="";
        if(getGender().equalsIgnoreCase("Female"))
        {

                eventInfo="She participates in the following events: [";
                for (String event:multiEvents
                     ) {
                    eventInfo+=event;
                    if(event!=null)
                        eventInfo+=", ";
                }
                eventInfo="].";

        }
        else
        {


                eventInfo="He participates in the following events: [";
                for (String event:multiEvents
                ) {
                    eventInfo+=event;
                    if(event!=null)
                        eventInfo+=", ";
                }
                eventInfo="].";

        }
        return super.toString()+ " and is a swimmer of team: "+team+". "+eventInfo;
    }
}

//===========================================================

import java.util.ArrayList;

public class Runner extends Athlete {
    private String country;

    private ArrayList<String> multiEvents;
    private String[] validEvents ={"M100", "M200", "M400", "M3000", "M5000", "M10000"};
    //Constructor

    public Runner(String lastName, String firstName, int birthYear, int birthMonth, int birthDay, char gender, String country) {
        super(lastName, firstName, birthYear, birthMonth, birthDay, gender);
        this.country = country;
        multiEvents = new ArrayList<>();

    }
    //getter

    public String getCountry() {
        return country;
    }



    public ArrayList<String> getMultiEvents() {
        return multiEvents;
    }

    public String[] getValidEvents() {
        return validEvents;
    }

    public boolean addEvent(String singleEvent)
    {
        for (String event:multiEvents
        ) {
            if(event.equalsIgnoreCase(singleEvent))
                return false;
        }
        multiEvents.add(singleEvent);
        return true;

    }
    public boolean addEvent(ArrayList<String> multiEvents)
    {
        boolean status =false;
        for (String event:multiEvents
        ) {
            if(this.multiEvents.contains(event))
                status = false;
            else
            {
                this.multiEvents.add(event);
                status = true;
            }
        }
        return status;
    }
    private String runnerTag()
    {
        String tag="";
                for(String singleEvent: multiEvents) {
                    if (singleEvent != "M3000" && singleEvent != "M5000" && singleEvent != "M10000")
                        tag = "is a sprinter";
                    else if (singleEvent != "M100" && singleEvent == "M200" && singleEvent == "M400")
                        tag = "is a long-distance runner";
                    else
                        tag = " is a super athlete. ";
                }

        return tag;
    }
    @Override
    public String toString() {
        String eventInfo="";
        if(getGender().equalsIgnoreCase("Female"))
        {

                eventInfo="She is a citizen of USA and "+runnerTag()+" who participates in event: [";
                for (String event:multiEvents
                ) {
                    eventInfo+=event;
                    if(event!=null)
                        eventInfo+=", ";
                }
                eventInfo+="].";

        }
        else
        {

                eventInfo="He is a citizen of USA and "+runnerTag()+" who participates in event: [";
                for (String event:multiEvents
                ) {
                    eventInfo+=event;
                    if(event!=null)
                        eventInfo+=", ";
                }
                eventInfo+="].";
            }

        return super.toString()+ ". "+eventInfo;
    }
}

//========================================================

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class AthleteRoster {
    private String semester;
    private int year;
    ArrayList<Athlete> athletes;
    //Constructor

    public AthleteRoster(String semester, int year) {
        this.semester = semester;
        this.year = year;
        athletes= new ArrayList<>();
    }
    //getters

    public String getSemester() {
        return semester;
    }

    public int getYear() {
        return year;
    }
    public  boolean addAthlete(Athlete a)
    {
        boolean status = true;
        for (Athlete athlete:athletes
             ) {
            if(athlete.equals(a))
                status = false;

        }
        if(status)
            athletes.add(a);
        return status;
    }
    public String allAthletesOrderedByLastName()
    {
        String result ="";
        Collections.sort(athletes, new Comparator<Athlete>() {
            @Override
            public int compare(Athlete o1, Athlete o2) {
                return o1.getLastName().compareTo(o2.getLastName());
            }
        });
        for (Athlete a:athletes
             ) {
            result+=a+"\n";
        }
        return result;
    }
    public String allAthletesOrderedByAge()
    {
        String result ="";
        Collections.sort(athletes, new Comparator<Athlete>() {
            @Override
            public int compare(Athlete o1, Athlete o2) {
                return o1.computeAge().compareTo(o2.computeAge());
            }
        });
        for (Athlete a:athletes
        ) {
            result+=a+"\n";
        }
        return result;
    }
    public String allRunnersOrderedByNumberOfEvents()
    {
        String result ="";
        ArrayList<Runner> runners = new ArrayList<>();
        for (Athlete a:athletes
             ) {
            if(a instanceof Runner)
                runners.add((Runner) a);
        }
        Collections.sort(runners, new Comparator<Runner>() {
            @Override
            public int compare(Runner o1, Runner o2) {

                    if (o1.getMultiEvents().size() > o2.getMultiEvents().size())
                        return 1;
                    else if ( o1.getMultiEvents().size() < o2.getMultiEvents().size())
                        return -1;
                    else
                        return 0;

            }
        });
        for (Runner a:runners
        ) {

            result+=a+"\n";
        }
        return result;
    }
}

//======================================================

import java.util.ArrayList;
import java.util.Arrays;

public class AthleteDriver {
    public static void main(String args[]) {

        //Check Athlete Class
        Athlete a1 = new Athlete("Daffy","Duck", 2000, 9, 7,'j'); System.out.println("Gender is "+a1.getGender());//undeclared
        a1.setGender('F');
        System.out.println("Gender is "+a1.getGender());//female
        a1.setGender('m');
        System.out.println("Gender is "+a1.getGender());//male
        System.out.println("ComputeAge method says "+a1.computeAge());
        System.out.println("First name is : "+a1.getFirstName()); //Daffy
        System.out.println("DaysSinceBirth method says "+a1.daysSinceBirth()+" days"); System.out.println("Last name is : "+a1.getLastName()); //Duck
        System.out.println("Output of our toString correct?: \n"+a1);
        System.out.println("=======================================\n");

        //Check Runner Class
        Runner r5 = new Runner("Bugs","Bunny", 2000, 9, 8, 'm',"USA");
        System.out.println("Did we add M10000 successfully?: "+r5.addEvent("M10000")); //true
        System.out.println("Did we unsucessfully try to add M10000 again?: "+r5.addEvent("m10000")); //false
        ArrayList<String> temp = new ArrayList<String>(Arrays.asList("M100", "M3000"));
        System.out.println("Did we successfully add mutiple events?: "+r5.addEvent(temp));//true
        System.out.println("Did we unsucessfully try to add mutiple events?: "+r5.addEvent(temp));//false
        System.out.println("How many events does Bugs participate in?: "+r5.getMultiEvents().size());//3
        System.out.println("Gender is "+r5.getGender());//male
        System.out.println("Output of our toString correct?: \n"+r5);
        System.out.println("=======================================\n");

        //Check Swimmer Class
        Swimmer s1 = new Swimmer("Franklin", "Missy", 1995, 5, 10, 'F', "Colorado Stars");
        System.out.println("Did we add 100m backstoke successfully?: "+s1.addEvent("100m backstoke")); //true
        System.out.println("Did we unsucessfully try to add 100m backstoke again?: " +s1.addEvent("100M Backstoke")); //false
        temp = new ArrayList<String>(Arrays.asList( "200m backstoke","200m freestyle"));
        System.out.println("Did we successfully add mutiple events?: "+s1.addEvent(temp));//true
        System.out.println("Did we unsucessfully try to add mutiple events?: "+s1.addEvent(temp));//false
        System.out.println("How many events does s1 participate in?: "+s1.getMultiEvents().size());//4 System.out.println("Gender is "+s1.getGender());//female
        System.out.println("Output of our toString correct?: \n"+s1);
        System.out.println("=======================================\n");

        //Check AthleteRoster
        Swimmer s2 = new Swimmer("Ruele", "Naomi", 1997, 1, 13, 'F',"Florida International University");
          s2.addEvent(new ArrayList<String>(Arrays.asList("100m backstoke","50m backstoke","100m freestyle")));

        Runner r1 = new Runner("Bolt", "Usain", 1986, 8, 21, 'M',"Jamaica");
      r1.addEvent("M100"); r1.addEvent("M200");

           Runner r2 = new Runner("Griffith", "Florence", 1959, 12, 21, 'F',"United States of America"); r2.addEvent("M100"); r2.addEvent("M200");
           r2.addEvent("M400"); r2.addEvent("M10000"); r2.addEvent("M3000");
           r2.addEvent("M5000");

          AthleteRoster ar = new AthleteRoster("Fall",2019);
           ar.addAthlete(a1);
          ar.addAthlete(s1);
          ar.addAthlete(r1);
          ar.addAthlete(r2);
          ar.addAthlete(s2);
           ar.addAthlete(r5);
           System.out.println(ar.allRunnersOrderedByNumberOfEvents());
        System.out.println("=======================================\n");

           System.out.println(ar.allAthletesOrderedByAge());
        System.out.println("=======================================\n");

          System.out.println(ar.allAthletesOrderedByLastName());
        System.out.println("=======================================\n");


    }

}

//Output

//If you need any help regarding this solution ............. please leave a comment ........... thanks


Related Solutions

**Only need the bold answered In Java, implement the Athlete, Swimmer, Runner, and AthleteRoster classes below....
**Only need the bold answered In Java, implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class must be in separate file. Draw an UML diagram with the inheritance relationship of the classes. 1. The Athlete class a. All class variables of the Athlete class must be private. b.is a class with a single constructor: Athlete(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender). All arguments of the constructor should be stored as class variables. There...
**Only need the bold answered Implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class...
**Only need the bold answered Implement the Athlete, Swimmer, Runner, and AthleteRoster classes below. Each class must be in separate file. Draw an UML diagram with the inheritance relationship of the classes. 1. The Athlete class a. All class variables of the Athlete class must be private. b.is a class with a single constructor: Athlete(String lName, String fName, int birthYear, int birthMonth, int birthDay, char gender). All arguments of the constructor should be stored as class variables. There should only...
Using Java, write the the following code. Only the bold needs to be answered Here is...
Using Java, write the the following code. Only the bold needs to be answered Here is the driver: package assign public class A3Driver { public static void main(String[] args) { //Test GSUStudent object GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234); gsw.setGPA(3.7); gsw.setHourlyRate(10.00); gsw.setTotalHours(30); System.out.println(gsw); GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413); gsw2.setGPA(4.0); gsw2.setHourlyRate(10.00); gsw2.setTotalHours(45); System.out.println(gsw2); //Test GSUStoreSupervisor GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8); gss.setMonthlyPay(4000); System.out.println(gss); //test GSUSuperStudent GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9,...
***GOVERNMENT*** Only one is needs to be answered, 2 paragraphs with 6-9 sentences each. 1. You...
***GOVERNMENT*** Only one is needs to be answered, 2 paragraphs with 6-9 sentences each. 1. You are a local government official in City X that needs to raise more income for the city,at the same time that you are fighting a major problem with crime. What would be your approach to these challenges? Discuss 2. Do you think that "No Child Left Behind" is a good policy for Texas schools? Discuss. 3. How should Texas deal with increasing rates of...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold font. I also need help with Question 6 1) What criteria must sales transactions meet in order for the seller to recognize revenues before collecting cash? a. The revenues must be earned (the firm must have achieved substantial performance). b. The amount to be received must qualify as an asset (there must be a future economic benefit and the amount must be measured with...
NOT SURE WHAT NEEDS TO BE EDITED??? AS STATED IN THE FIRST LINE, THE BOLD IN...
NOT SURE WHAT NEEDS TO BE EDITED??? AS STATED IN THE FIRST LINE, THE BOLD IN EACH QUESTION IS WHAT NEEDS TO BE PICKED FROM TO COMPLETE THE SENTENCE. NOT SURE HOW I CAN EXPLAIN THIS MORE CLEAR. THE PICK TO COMPLETE THE QUESTIONS IS BOLD AND SOME HAVE 2 OR 3 OPTIONS TO PICK FROM. I HOPE THIS HELPS. Discussion of Reported Costs and Variances from the Two Systems -- *****the bold in each question is what needs to...
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
Economics of forestry - what is the basic question that needs to be answered? What is...
Economics of forestry - what is the basic question that needs to be answered? What is the answer to that question? How is the harvesting of forests affected by various factors that enter the Faustmann formula?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT