Using JAVA: -use static variables and methods only if needed -use Arrays NOT Arr
ID: 3784506 • Letter: U
Question
Using JAVA:
-use static variables and methods only if needed
-use Arrays NOT Array Lists.
-encapsulation: make sure you protect your class variables and provide access to them though methods
Create 3 classes, app, football player and football team. The application (app) will use the two other classes (football player and football team).
The app will:
create 11 football players using an Array, create a football team using the players above use the football team instance (object) to display the information requested in the lab
a football player class has at least 5 attributes from your choice
a method that returns the complete info about the player
a football team class has a name and
a mascot 11 football players any other attributes (optional)
a method that: displays all the information about a team including: name mascot, and information on each player in the team and assigns a random score to each team.
a method that: display information about a specific player in the team using an input parameter such as the player position or player number for instance.
For instance, from team A, displays information about the quarterback, or display information about player number 5.
Explanation / Answer
//FootballPlayer.java
import java.util.Random;
public class FootballPlayer
{
// Player Attributes
private String firstName;
private String lastName;
private int age;
private String position;
private int weight;
// Constructor
FootballPlayer(String pl_firstName, String pl_lastName, int pl_age, String pl_position, int pl_weight)
{
firstName = pl_firstName;
lastName = pl_lastName;
age = pl_age;
position = pl_position;
weight = pl_weight;
}
// FootballPlayer method
FootballPlayer()
{
this.firstName = "";
this.lastName = "";
this.age = 0;
this.position = "";
this.weight = 0;
}
// Print info on player and their stats
void playerAttr()
{
System.out.println("First Name: " + getFirstName());
System.out.println("Last Name: " + getLastName());
System.out.println("Player Age: " + getAge());
System.out.println("Player Position: " + getPosition());
System.out.println("Player Weight: " + getWeight());
System.out.println("Player's Rushing Yards: " + rushingYard());
}
// Method for displaying random rushing yards
int rushingYard()
{
int val = 0;
double rushVal = Math.random();
val = (int) (rushVal * 100.0)+ 1;
return val;
}
/**
* @return the firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName()
{
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/**
* @return the age
*/
public int getAge()
{
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age)
{
this.age = age;
}
/**
* @return the position
*/
public String getPosition()
{
return position;
}
/**
* @param position the position to set
*/
public void setPosition(String position)
{
this.position = position;
}
/**
* @return the weight
*/
public int getWeight()
{
return weight;
}
/**
* @param weight the weight to set
*/
public void setWeight(int weight)
{
this.weight = weight;
}
}
//FootballTeam.java
import java.util.ArrayList;
public class FootballTeam
{
// Attributes
private String teamName;
private String mascot;
private int instantYards;
private ArrayList<FootballPlayer> player;
// Constructor
FootballTeam(String tm_teamName, String tm_mascot, ArrayList<FootballPlayer> tm_player)
{
teamName = tm_teamName;
mascot = tm_mascot;
player = tm_player;
}
// Displaying Team Info
void teamInfo()
{
System.out.println("Team Name: " + getTeamName());
System.out.println("Team Mascot: " + getMascot());
System.out.println("Instant Rushing Yards: " + instantYards());
System.out.println("");
for (int i = 0; i < getPlayer().size(); i++)
{
getPlayer().get(i).playerAttr();
System.out.println("");
}
}
// Method for Instant Yards
public int instantYards()
{
int yardsTotal = 0;
for (int i = 0; i < getPlayer().size(); i++)
{
yardsTotal += getPlayer().get(i).rushingYard();
instantYards = yardsTotal;
}
return instantYards;
}
//Player information method Based on search by position
String playerInfo(String searchPosition)
{
FootballPlayer searchPlayer = new FootballPlayer();
boolean validPlayer = false;
//For loop to check players by position entry
for (int j = 0; j < getPlayer().size(); j++)
{
searchPlayer = getPlayer().get(j);
if (searchPosition.equals(searchPlayer.getPosition()))
{
searchPlayer.playerAttr();
System.out.println("");
validPlayer = true;
}
}
if (!validPlayer)
{
System.out.println("You entered an invalid position");
}
return searchPosition;
}
/**
* @return the teamName
*/
public String getTeamName()
{
return teamName;
}
/**
* @param teamName the teamName to set
*/
public void setTeamName(String teamName)
{
this.teamName = teamName;
}
/**
* @return the mascot
*/
public String getMascot()
{
return mascot;
}
/**
* @param mascot the mascot to set
*/
public void setMascot(String mascot)
{
this.mascot = mascot;
}
/**
* @param instantYards the instantYards to set
*/
public void setInstantYards(int instantYards)
{
this.instantYards = instantYards;
}
/**
* @return the player
*/
public ArrayList<FootballPlayer> getPlayer()
{
return player;
}
/**
* @param player the player to set
*/
public void setPlayer(ArrayList<FootballPlayer> player)
{
this.player = player;
}
}
//App.java
import java.util.*;
public class App
{
public static void main(String[] args)
{
// Declaring Array list of Football Players
ArrayList<FootballPlayer> player = new ArrayList();
// list of 11 players
player.add(new FootballPlayer("Antonio", "Brown", 27, "WR", 180));
player.add(new FootballPlayer("Julian", "Edelman", 29, "WR", 200));
player.add(new FootballPlayer("Tom", "Brady", 38, "QB", 225));
player.add(new FootballPlayer("J.J.", "Watt", 26, "DE", 289));
player.add(new FootballPlayer("Calvin", "Johnson", 30, "WR", 239));
player.add(new FootballPlayer("Marshawn", "Lynch", 29, "RB", 215));
player.add(new FootballPlayer("Rob", "Gronkowski", 26, "TE", 265));
player.add(new FootballPlayer("Richard", "Sherman", 27, "CB", 195));
player.add(new FootballPlayer("Julio", "Jones", 26, "WR", 220));
player.add(new FootballPlayer("Clay", "Matthews", 29, "LB", 260));
// Creating a team with the above players
FootballTeam team = new FootballTeam("NFL Elite", "Bald Eagle", player);
// User prompt for position desired
System.out.print("Enter a position that you want information for: ");
Scanner scnr = new Scanner(System.in);
String positionDesired = scnr.nextLine();
// Dot notation for displaying information on player including Rush Yards
team.playerInfo(positionDesired);
System.out.println("");
// Displaying Team Info with Summary of Rush Yards
team.teamInfo();
}
}
/*
output:
Enter a position that you want information for: 11
You entered an invalid position
Team Name: NFL Elite
Team Mascot: Bald Eagle
Instant Rushing Yards: 606
First Name: Antonio
Last Name: Brown
Player Age: 27
Player Position: WR
Player Weight: 180
Player's Rushing Yards: 35
First Name: Julian
Last Name: Edelman
Player Age: 29
Player Position: WR
Player Weight: 200
Player's Rushing Yards: 76
First Name: Tom
Last Name: Brady
Player Age: 38
Player Position: QB
Player Weight: 225
Player's Rushing Yards: 95
First Name: J.J.
Last Name: Watt
Player Age: 26
Player Position: DE
Player Weight: 289
Player's Rushing Yards: 88
First Name: Calvin
Last Name: Johnson
Player Age: 30
Player Position: WR
Player Weight: 239
Player's Rushing Yards: 66
First Name: Marshawn
Last Name: Lynch
Player Age: 29
Player Position: RB
Player Weight: 215
Player's Rushing Yards: 27
First Name: Rob
Last Name: Gronkowski
Player Age: 26
Player Position: TE
Player Weight: 265
Player's Rushing Yards: 9
First Name: Richard
Last Name: Sherman
Player Age: 27
Player Position: CB
Player Weight: 195
Player's Rushing Yards: 90
First Name: Julio
Last Name: Jones
Player Age: 26
Player Position: WR
Player Weight: 220
Player's Rushing Yards: 52
First Name: Clay
Last Name: Matthews
Player Age: 29
Player Position: LB
Player Weight: 260
Player's Rushing Yards: 7
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.