10:53 AM a bbprod.hwcc.edu 00 Verizon Create a program called TennisArrayList.java This program will open a file, tennisplayers.txt, for reading. This input file contains tennis player information. Your program will create a method called fillTennisArrayList) that will read from the file and create a TennisPlayer ArrayList. Then, you will create a method called printTennisPlayers) that will print the contents of the ArrayList. Files to submit: TennisPlayer.java (the most recent one, from Lecture Module 10) TennisArrayList.java TennisArrayList.java main0 Create an ArrayList of type TennisPlayer. Then call two methods, fillTennisArrayList0 and printTennisPlayersO with the ArrayList as the argument. fillTennisArrayList0 This method defines one parameter, an ArrayList of type TennisPlayer The input file includes information about a number of tennis players. For each player, their name, country, rank, age, wins and losses are provided, each on its own line The file provided includes information for seven players, but the file may contain information for any number of players. However, the format of six lines for each player will be the same and the information will be in the same order Iterate through the file using a while loop. For each tennis player, obtain all the information for that player, create a TennisPlayer object using the information from the file, and add the TennisPlayer object to the ArrayList.
Expert Answer
TennisPlayer.java:
public class TennisPlayer {
private String name, country;
private int rank, age, wins, losses;
public TennisPlayer(String name, String country, int rank, int age,
int wins, int losses) {
this.name = name;
this.country = country;
this.rank = rank;
this.age = age;
this.wins = wins;
this.losses = losses;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLosses() {
return losses;
}
public void setLosses(int losses) {
this.losses = losses;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(“——————————n”);
sb.append(“Tennis Player: ” + name + “n”);
sb.append(“Country: ” + country + “nn”);
sb.append(“Current Rank: ” + rank + “nn”);
sb.append(“Age: ” + age + “nn”);
sb.append(“Wins: ” + wins + “n”);
sb.append(“Losses: ” + losses + “nn”);
double percentage = Math.floor((double)wins * 10000/(wins + losses))/100;
sb.append(“Winning Percentage: ” + percentage + “%n”);
sb.append(“——————————“);
return sb.toString();
}
}
TennisArrayList.java:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class TennisArrayList {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<TennisPlayer> playersList = fillTennisArrayList();
printTennisPlayers(playersList);
}
public static void printTennisPlayers(ArrayList<TennisPlayer> playersList) {
for(TennisPlayer player: playersList) {
System.out.println(player);
}
}
/*
* Below method reads the data from a file named as players.txt in the same directory
*
*/
private static ArrayList<TennisPlayer> fillTennisArrayList() throws FileNotFoundException {
final String FILE_NAME = “players.txt”;
System.out.println(“reading file ” + FILE_NAME + ” from directory: ” + System.getProperty(“user.dir”));
ArrayList<TennisPlayer> playersList = new ArrayList<>();
Scanner reader = new Scanner(new FileReader(FILE_NAME));
while(reader.hasNextLine()) {
String name = reader.nextLine();
String country = reader.nextLine();
int rank = Integer.parseInt(reader.nextLine());
int age = Integer.parseInt(reader.nextLine());
int wins = Integer.parseInt(reader.nextLine());
int losses = Integer.parseInt(reader.nextLine());
// create new player
TennisPlayer tp = new TennisPlayer(name, country, rank, age, wins, losses);
// add player to list
playersList.add(tp);
}
System.out.println(“List of players populated successfully”);
reader.close();
return playersList;
}
}
players.txt:
Player1
Country1
5
12
13
4
Player2
Country2
3
11
16
3