The purpose of this assignment is to read in a file called nfl.txt which has dif
ID: 3532710 • Letter: T
Question
The purpose of this assignment is to read in a file called nfl.txt which has different types of players in it, quarterbacks and recievers which have 4 of the same fields, then quarterback has 3 others and reciever 2. Make three classes, Player, Quarterback extends Player, and Reciever extends Player. Then a test program called Prog6. The problem I am running into is I think on the Prog6 tester... It will not output the information I want it to, actually when I run the program it just terminates without any errors. I need this formatted in a specific format but I would just be happy with getting an output of the players and their stats.
(Prog6
(Quarterback http://codepad.org/wblgYZAh )
(nfl.txt http://codepad.org/8Pz3N5Qz )
Explanation / Answer
Here you go :
import java.util.*;
import java.io.*;
public class Prog6
{
public static void main(String[] args)
{
String fName;
String lName;
String position;
String team;
int completions = 0;
int attempts = 0;
int yards = 0;
int receptions = 0;
final int MAX_PLAYERS = 20;
Scanner inFile = null;
Player players[] = new Player[MAX_PLAYERS];
try
{
inFile = new Scanner( new File ( "nfl.txt" ) );
}
catch( FileNotFoundException e)
{
System.err.println( "Error: file not found" );
}
int count=0;
while( inFile.hasNext() )
{
fName = inFile.next();
lName = inFile.next();
team = inFile.next();
position = inFile.next();
if( position.equals("QB") )
{
completions = inFile.nextInt();
attempts = inFile.nextInt();
yards = inFile.nextInt();
players[count]=new Quarterback(fName,lName,team,position,completions,attempts,yards);
}
else
{
receptions = inFile.nextInt();
yards = inFile.nextInt();
players[count]=new Receiver(fName,lName,team,position,receptions,yards);
}
for(int i = 0; i < players.length ; i++)
{
if( players[i] instanceof Quarterback )
{
System.out.println( players[i].getfName() + players[i].getlName() + players[i].getTeam() + players[i].getPosition() + ( (Quarterback) players[i]).getCompletions() + (( Quarterback ) players[i]).getAttempts() + ((Quarterback) players[i]).getYards());
}
}
for( int j = 0; j < players.length ; j++)
{
if( players[j] instanceof Receiver)
{
System.out.println( players[j].getfName() + players[j].getlName() + players[j].getTeam() + players[j].getPosition() + ((Receiver) players[j]).getReceptions() + ((Receiver) players[j]).getYards());
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.