Baseball Player Salaries Activity.
Eclipse, extract this file anywhere(
- go to file-import-existing projects into workspace
- select the directory you extracted
- choose copy into workspace
Bluej users, you will need to open this file and add the files from
the src directory of file and the list.
It includes (Here) the list
of National League baseball player salaries from 2005. The file
includes the 439 players from the league.
- Now update League to include an array of Players. This array
will be filed with all players.
- For HW, create these methods:
- findPlayers - it will take in a salary and it will print
out all players that make that specified salary
- findAvg - returns the average salary
- getHighestSalary - prints out and returns the PLAYER with
the highest salary
- getLowestSalary- prints out and returns the PLAYER with
the lowest salary
- printAllPlayers(int cutOff) -print all players who have
a salary above that specified salary
- printTeam (String team) - takes in a String team and prints
all those who are on that team
For high fliers:
- sortBySalary (extra credit)
- sortByName(extra credit)
Code so far:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class League {
private Scanner scan;
private Player[] allPlayers=new Player[439];
public League()
{
try
{
scan=new Scanner (new File("nlbs2005.txt"));
}
catch (IOException e)
{}
int count=0;
while(scan.hasNext()) //for every line in file
{
String line=scan.nextLine();//gets the line
String[] splits=line.split(",");//splits it by , into an array
String team = splits[0];
String lastName=splits[1];
String firstName=splits[2];
int salary=Integer.parseInt(splits[3]);
String position=splits[4];
//now I print it out to test
//System.out.println(team+"-"+firstName+" "+ lastName+"-"+ salary +"-"+position);
Player p=new Player(team, lastName,firstName,position, salary);
allPlayers[count]=p;
count++;
}
}
public void printAll()
{
for (Player p:allPlayers)
System.out.println(p.getFirstName() + " " +p.getLastName() + " " + p.getSalary());
}
public void findPlayers (int salary)
{
for (Player p:allPlayers)
if (p.getSalary()==salary)
System.out.println(p.getFirstName() + " " + p.getLastName());
}
public static void main(String[] args) {
League l = new League();
l.findPlayers(1000000);
}
}
|