Create the following classes. 1. Create a class named FootballPlayer. Create thr
ID: 3563620 • Letter: C
Question
Create the following classes.
1. Create a class named FootballPlayer. Create three variables - a name, a position, and an age. Create set and get methods to access each of these variables. Finally, create a toString method that returns a String representation of objects of this class.
2. Create a class named FootballTeam. Create two variables - a team name, and an array of 10 FootballPlayer objects. Fill up the array of FootballPlayer objects with names, positions, ages of your choice (make it up).
3. Create a Driver class that instantiates one Football team named the Packers. Print out the names of all of the players to the command line.
Explanation / Answer
====================
Class for FootballPlayer.java
====================
public class FootballPlayer {
private String name;
private String position;
private String age;
public FootballPlayer() {
super();
}
public FootballPlayer(String name, String position, String age) {
super();
setName(name);
setPosition(position);
setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String toString(){
return ("Player Name: "+getName()+" Position:"+getPosition() +" Age:"+getAge()) ;
}
}
==========================
Class for FootballTeam.java
===========================
public class FootballTeam {
private String teamName;
private FootballPlayer[] footballPlayers = new FootballPlayer[10];
public FootballTeam() {
super();
setTeamName("Brazil");
footballPlayers[0] = new FootballPlayer("Hernanes","MF","25");
footballPlayers[1] = new FootballPlayer("Mercilo","DF","30");
footballPlayers[2] = new FootballPlayer("Paulinho","MF","29");
footballPlayers[3] = new FootballPlayer("Oscar","FW","30");
footballPlayers[4] = new FootballPlayer("David","DF","35");
footballPlayers[5] = new FootballPlayer("Huck","FW","30");
footballPlayers[6] = new FootballPlayer("Neymar","FW","38");
footballPlayers[7] = new FootballPlayer("Cris","DF","25");
footballPlayers[8] = new FootballPlayer("Danilo","MF","28");
footballPlayers[9] = new FootballPlayer("Eder","LB","33");
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public FootballPlayer[] getFootballPlayers() {
return footballPlayers;
}
public void setFootballPlayers(FootballPlayer[] footballPlayers) {
this.footballPlayers = footballPlayers;
}
}
=========================
class for Driver.java
=======================
public class Driver {
public static void main(String[] args) {
FootballTeam footballTeam = new FootballTeam();
//get all players
FootballPlayer[] footballPlayers = footballTeam.getFootballPlayers();
System.out.println("Team Name is:"+footballTeam.getTeamName());
//print all players
for(int i=0;i<footballPlayers.length;i++) {
System.out.println(footballPlayers[i]);
}
}
}
===========
Output:
===========
Team Name is:Brazil
Player Name: Hernanes Position:MF Age:25
Player Name: Mercilo Position:DF Age:30
Player Name: Paulinho Position:MF Age:29
Player Name: Oscar Position:FW Age:30
Player Name: David Position:DF Age:35
Player Name: Huck Position:FW Age:30
Player Name: Neymar Position:FW Age:38
Player Name: Cris Position:DF Age:25
Player Name: Danilo Position:MF Age:28
Player Name: Eder Position:LB Age:33
Comment
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.