Question: Write a procedural program in JAVA for the same Object Oriented Program above. public class Football { / / Attributes private String playerName; private int

Write a procedural program in JAVA for the same Object Oriented Program above.
public class Football {
// Attributes
private String playerName;
private int totalPassingYards;
// Default Constructor
public Football(){
this.playerName ="";
this.totalPassingYards =0;
}
// Overloaded Constructor
public Football(String playerName, int totalPassingYards){
this.playerName = playerName;
this.totalPassingYards = totalPassingYards;
}
// Getters
public String getPlayerName(){
return playerName;
}
public int getTotalPassingYards(){
return totalPassingYards;
}
// Setters
public void setPlayerName(String playerName){
this.playerName = playerName;
}
public void setTotalPassingYards(int totalPassingYards){
this.totalPassingYards = totalPassingYards;
}
// toString Method
@Override
public String toString(){
return "Player: "+ playerName +", Passing Yards: "+ totalPassingYards;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FootballDriver {
public static void main(String[] args){
Football[] footballPlayers = new Football[10]; // assuming 10 players
int playerCount =0;
String fileName = "football.txt";
// Reading file and populating the array
try {
Scanner fileScanner = new Scanner(new File(fileName));
// Skip header
if (fileScanner.hasNextLine()){
fileScanner.nextLine();
}
// Read player names and yards
while (fileScanner.hasNextLine() && playerCount < footballPlayers.length){
String playerName = fileScanner.nextLine();
int passingYards = Integer.parseInt(fileScanner.nextLine());
footballPlayers[playerCount]= new Football(playerName, passingYards);
playerCount++;
}
fileScanner.close();
} catch (FileNotFoundException e){
System.out.println("File not found: "+ fileName);
return;
}
// Menu options
Scanner inputScanner = new Scanner(System.in);
int option;
do {
System.out.println("Menu:");
System.out.println("1. Highest Passing Yards");
System.out.println("2. Lowest Passing Yards");
System.out.println("3. Average Passing Yards");
System.out.println("4. Quit");
System.out.print("Choose an option: ");
option = inputScanner.nextInt();
switch (option){
case 1:
highestPassingYards(footballPlayers);
break;
case 2:
lowestPassingYards(footballPlayers);
break;
case 3:
averagePassingYards(footballPlayers);
break;
case 4:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid option. Please try again.");
}
} while (option !=4);
inputScanner.close();
}
// Method to find highest passing yards
public static void highestPassingYards(Football[] footballPlayers){
Football highestPlayer = footballPlayers[0];
for (Football player : footballPlayers){
if (player != null && player.getTotalPassingYards()> highestPlayer.getTotalPassingYards()){
highestPlayer = player;
}
}
System.out.println("Highest Passing Yards: "+ highestPlayer);
}
// Method to find lowest passing yards
public static void lowestPassingYards(Football[] footballPlayers){
Football lowestPlayer = footballPlayers[0];
for (Football player : footballPlayers){
if (player != null && player.getTotalPassingYards()< lowestPlayer.getTotalPassingYards()){
lowestPlayer = player;
}
}
System.out.println("Lowest Passing Yards: "+ lowestPlayer);
}
// Method to find average passing yards
public static void averagePassingYards(Football[] footballPlayers){
int totalYards =0;
int count =0;
for (Football player : footballPlayers){
if (player != null){
totalYards += player.getTotalPassingYards();
count++;
}
}
double average =(double) totalYards / count;
System.out.printf("Average Passing Yards: %.2f
", average);
}
}
Football.txt file
Player Most Pass

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!