public class LeagueDriver { public static void main(String[] args) { Scanner sc = null; try { sc = new Scanner(new File("Players.txt")); } catch (FileNotFoundException e) { System.err.println("No such file"); System.exit(-1); }
League l = new League(); while(sc.hasNext()) { String temp[] = sc.nextLine().split(", "); l.addPlayer(new Player(temp[0],temp[1],Integer.parseInt(temp[2]))); } l.printLeague(); System.out.println(); l.printAverageScore(); } }
Player.java:
public class Player { private String name,team; private int score; public Player(String n,String t,int s) { setName(n); setTeam(t); setScore(s); } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the team */ public String getTeam() { return team; } /** * @param team the team to set */ public void setTeam(String team) { this.team = team; } /** * @return the score */ public int getScore() { return score; } /** * @param score the score to set */ public void setScore(int score) { this.score = score; }
@Override public String toString() { String s = String.format("%s",name); return s; } }
League.java:
import java.util.ArrayList;
public class League { private ArrayList<Player> players;
public League() { players = new ArrayList<Player>(); }
void printAverageScore() { double sum = 0; for(Player p:players) { sum += p.getScore(); } System.out.printf("the average field goals made for the whole league: %.2f",(sum/(float)players.size())); } }
Players.txt
Steph Curry, Golden State Warriors, 498, LeBron James, Cleveland Cavaliers, 484, Russell Westbrook, Oklahoma City Thunder, 459, James Harden, Houston Rockets, 449, Jayson Tatum, Boston Celtics, 450, Bruce Brown, Brooklyn Nets, 470,
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.