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

Create a class hierarchy for BaseballPlayer by extending it to a class called Fi

ID: 647920 • Letter: C

Question

Create a class hierarchy for BaseballPlayer by extending it to a class called Fielder and another called Pitcher. Class Pitcher should contain an extra float instance variable for the pitcher?s earned run average (ERA). Each line of the input file for the project will start with a single letter that should be either ?P? or ?F? for pitcher and fielder respectively. An input line for a pitcher will have the additional value for the ERA at the end, as follows: F,37,Smith,John,.351 P,23,iones,Chris,.023,2.45 Remember to put appropriate calls to super() in your constructors. Create a linked list of BaseballPlayers to store the players in sorted order (rather than using an array). Every time you insert a new baseball player into the list, it should be inserted in the proper place in the list to keep it sorted by number. That is, if the linked list were three-letter words, such as Bat - > Cat - > Fat - > Sat - > Rat And you inserted the word Hat, the list would appear as Bat - > Cat - > Fat - > Hat - > Sat - > Rat Add a third column to the GUI, and display the unsorted players in the first column, the Fielders in the second column sorted by number (from the linked list) and a list of Pitchers sorted by number in the third column (from the same linked list).

Explanation / Answer

//BaseballPlayer.java

package baseball;

public class BaseballPlayer

{

                String lastName;

String firstName;

int number;

float rating;

   public BaseballPlayer()

{

   }

   public BaseballPlayer(String l, String f, int a, float r)

   {

       lastName = l;

       firstName = f;

       number = a;

       rating = r;

   }

   public String toString()

   {

       return firstName+"-"+lastName;

   }

}

//Fielder.java

package baseball;

public class Fielder extends BaseballPlayer{

   public Fielder() {

   }

   public Fielder(int a, String l, String f, float r)

   {

       super(l, f, a, r);

   }  

}

//Pitcher.java

package baseball;

public class Pitcher extends BaseballPlayer{

float earnedRunAverage;  

   public Pitcher()

   {

   }

   public Pitcher(int a, String l, String f, float r, float era)

   {

       super(l, f, a, r);

       earnedRunAverage = r;

   }

   public char getType()

   {

       return 'P';

   }

}

//baseball.java

package baseball;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Scanner;

public class baseball {

private static Scanner inFile;

   public static void main(String[] args) throws FileNotFoundException

{  

       inFile = new Scanner(new File("input.txt"));

       ArrayList<BaseballPlayer> bp = new ArrayList<BaseballPlayer>();

       while(inFile.hasNext())

       {

           String line = inFile.next();

           String[] results = line.split(",")

           if(results[0].equals("F"))

           {

               bp.add(new Fielder(Integer.parseInt(results[1]), results[2], results[3], Float.parseFloat(results[4])));

           }

           else

           {

               bp.add(new Pitcher(Integer.parseInt(results[1]), results[2], results[3], Float.parseFloat(results[4]), Float.parseFloat(results[5])));

           }

       }

       Collections.sort(bp, new Comparator<BaseballPlayer>()

{

              @Override

              public int compare(BaseballPlayer lhs, BaseballPlayer rhs)

              {

              return Integer.signum(lhs.number-rhs.number);

              }

          

       });

       System.out.println("Sorted Order all data: " + bp);

       ArrayList<BaseballPlayer> ArrayList<BaseballPlayer>();

       onlyFielders.addAll(bp);

       for (BaseballPlayer baseballPlayer : onlyFielders) {

           if(baseballPlayer.getClass() == (new Pitcher()).getClass())

               onlyFielders.remove(baseballPlayer);

       }

       System.out.println("Sorted Order of Fielders Data: " + onlyFielders);

       ArrayList<BaseballPlayer> ArrayList<BaseballPlayer>();

       onlyPitchers.addAll(bp);

       for (BaseballPlayer baseballPlayer : onlyPitchers) {

           if(baseballPlayer.getClass() == (new Fielder()).getClass())

           {

               onlyPitchers.remove(baseballPlayer);

           }

       }

       System.out.println("Sorted Order of Pitchers Data: " + onlyPitchers);      

   }

}

//input.txt

F,37,Smith,John,.351

P,23,Jones,Chris,.023,2.45

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote