SoftballTeam.java Requirements and Design: The SoftballTeam class provides metho
ID: 3695831 • Letter: S
Question
SoftballTeam.java Requirements and Design: The SoftballTeam class provides methods for reading in the data file and generating reports. Design: In addition to the specifications in Project 10, the existing readPlayerFile method must be modified to catch following: InvalidCategoryException and NumberFormatException. o readPlayerFile has no return value and accepts the data file name as a String. Remember to include the throws IOException clause in the method declaration. This method creates a Scanner object to read in the file and then reads it in line by line. The first line contains the team name and each of the remaining lines contains the data for a player. After reading in the team name, the “player” lines should be processed as follows. A player line is read in, a second scanner is created on the line, and the individual values for the player are read in. After the values on the line have been read in, an “appropriate” SoftballPlayer object created. If there is room on the roster, the player is added to the roster array and the player count is incremented. Any player lines/records read from the file after the limit of MAX_PLAYERS players has been reached should be added to the excluded array with appropriate prefix message (Maximum player count of ___ exceeded for: where the blank is MAX_PLAYERS) and its count should be incremented. If excluded array is full, the line/record should just be skipped. The data file is a “comma separated values” file; i.e., if a line contains multiple values, the values are delimited by commas. So when you set up the scanner for the player lines, you need to set the delimiter to use a “,” by calling the useDelimiter(",") method on the Scanner object. Each player line in the file begins with a category for the softball player (O, I, P, and R are valid categories for softball players indicating Outfielder, Infielder, Pitcher, and ReliefPitcher respectively. The second field in the record is the player’s number, followed by the data for the name, position, specialization factor, and batting average. The last items correspond to the data needed for the particular category (or subclass) of SoftballPlayer. For each incorrect line scanned (i.e., a line of data contains an invalid category or invalid numeric data), your method will need to handle the invalid items properly. If the line of data begins with an invalid category, your program should throw an InvalidCategoryException (see description above). If a line of data has a valid category, but includes invalid numeric data (e.g., the value for battingAvg contains an alphabetic character), a NumberFormatException (see notes on last page) will be thrown automatically by the Java Runtime Environment (JRE). The code that checks for player category should be in a try statement and the code that adds a record with an invalid player category to the excluded records array should now be placed in the catch clause the follows the try statement. That is, your readPlayerFile method should catch and handle InvalidCategoryException and NumberFormatException as follows. In each catch clause, a String object should be created consisting of e + " in: " + line where e is the exception and line is the line with the invalid data. The String object should be added to the excludedRecords array.
Explanation / Answer
//Player.java public class Player { String category; int playerNumber; String name; int position; double specializationFactor; double battingAverage; public Player(String c, int num, String nam, int pos, double factor, double avg) { category=c; playerNumber=num; name=nam; position=pos; specializationFactor=factor; battingAverage=avg; } }
//SoftballTeam.java
package softballteam;
import java.io.FileReader;
import java.util.Scanner;
public class SoftballTeam {
public static void main(String[] args) {
Player softballplayers[]=new Player[20];
Player excludedPlayers[]=new Player[10];
int MAX_PLAYER=4;
readPlayerFile("players.txt",softballplayers,excludedPlayers,MAX_PLAYER);
}
private static void readPlayerFile(String file,Player []softballplayers, Player[]excludedPlayers, int MAX_PLAYER) {
try{
Scanner scan=new Scanner(new FileReader(file));
System.out.println("Team Name: "+scan.nextLine());
while(scan.hasNextLine())
{
String line=scan.nextLine();
String data[]=line.split(",");
int k=0;
if(!data[0].equals("O")||!data[0].equals("I")||!data[0].equals("P")||!data[0].equals("R"))
{
throw new Exception("Invalid category exception");
}
int flag=0;
for(int i=0;i<data[5].length();i++)
{
if(!Character.isDigit(data[5].charAt(i)))
{
flag=1;
break;
}
}
if(flag==1)
{
throw new Exception("NumberFormatException ");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.