Objective: Understand Inheritance First, download the test class TestFootballPla
ID: 3678871 • Letter: O
Question
Objective: Understand Inheritance
First, download the test class TestFootballPlayer
Write a class FootballPlayer. This class should have the following (private) instance variables & methods:
String name
int gamesPlayed
int numberOfInjuries
A constructor with the following header:
public FootbalPlayer(String name, int gamesPlayed, int numberOfInjuries)
A toString() method that describes a football player using the values of the instance variables.
Make sure to include getters/setters for each instance variable.
Now, write two classes that extend FootballPlayer: OffensivePlayer andDefensivePlayer.
An OffensivePlayer should have the additional instance variables int totalYards & int numberOfTDs - with corresponding getters and setters - along with a constructor of the form:
OffensivePlayer(String name, int gamesPlayed, int numberOfInjuries, int totalYards, int numberOfTDs)
A DefensivePlayer should have the additional instance variables int numberOfInterceptions & int numberOfTackles - with corresponding getters and setters - along with a constructor of the form:
DefensivePlayer(String name, int gamesPlayed, int numberOfInjuries, int numberOfInterceptions, int numberOfTackles)
Also, create a QuarterBack as a subclass of OffensivePlayer, with the additional instance variables int interceptionsThrown & double completionPercentage, along with a constructor of the form:
Quarterback(String name, int gamesPlayed, int numberOfInjuries, int totalYards, int numberOfTDs, int interceptionsThrown, double completionPercentage)
As always, include the appropriate getters and setters for each new instance variable.
Finally, each class should have a toString method that returns a description (as a String) of the instance using the values of the instance variables.
Testing:
To test your code, move TestFootballPlayer.java to the directory where your .java files are located, then compile & run it.
Finally:
Upload your source code as a zipfile, Football.zip, to the CSE dropbox. Your zipfile should contain a total of 4 files. YOU DO NOT NEED TO RESUBMIT TestFootballPlayer.java
test class TestFootballPlayer:
Explanation / Answer
/**
*
* creating FootBallplayer class
*/
public class FootballPlayer {
private String name;
private int gamesPlayed;
private int numberOfInjuries;
public FootballPlayer(String name, int gamesPlayed, int numberOfInjuries){
}
public String toString()
{
return "Name of the player:" + this.name + ",, "
+ "number of games Played:" + this.gamesPlayed + ",, " + "number of injuries he had:"
+ this.numberOfInjuries;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gamesPlayed
*/
public int getGamesPlayed() {
return gamesPlayed;
}
/**
* @param gamesPlayed the gamesPlayed to set
*/
public void setGamesPlayed(int gamesPlayed) {
this.gamesPlayed = gamesPlayed;
}
/**
* @return the numberOfInjuries
*/
public int getNumberOfInjuries() {
return numberOfInjuries;
}
/**
* @param numberOfInjuries the numberOfInjuries to set
*/
public void setNumberOfInjuries(int numberOfInjuries) {
this.numberOfInjuries = numberOfInjuries;
}
}
//creating offensiveplayer class which extends Football player
class OffensivePlayer extends FootballPlayer{
int totalYards;
int numberOfTDs;
public OffensivePlayer(String name, int gamesPlayed, int numberOfInjuries, int totalYards, int numberOfTDs)
{
super(name, gamesPlayed, numberOfInjuries);
}
/**
* @return the totalYards
*/
public int getTotalYards() {
return totalYards;
}
/**
* @param totalYards the totalYards to set
*/
public void setTotalYards(int totalYards) {
this.totalYards = totalYards;
}
/**
* @return the numberOfTDs
*/
public int getNumberOfTDs() {
return numberOfTDs;
}
/**
* @param numberOfTDs the numberOfTDs to set
*/
public void setNumberOfTDs(int numberOfTDs) {
this.numberOfTDs = numberOfTDs;
}
public String toString() {
return "Name of the player:" + this.getName() + ",, "
+ "number of games Played:" + this.getGamesPlayed() + ",, " + "number of injuries he had:"
+ this.getNumberOfInjuries() +",,"+"NumberoFTDs" + this.getNumberOfTDs()+ ",, "
+ "totalYards" + this.getTotalYards();
}
}
class QuarterBack extends OffensivePlayer{
private String name;
private int interceptionsThrown;
private double comspletionPercentage;
public QuarterBack(String name, int gamesPlayed, int numberOfInjuries, int totalYards, int numberOfTDs, int interceptionsThrown, double comspletionPercentage){
super(name, gamesPlayed, numberOfInjuries,totalYards,numberOfTDs);
}
public String toString() {
return "Name of the player:" + this.getName() + ",, "
+ "number of games Played:" + this.getGamesPlayed() + ",, " + "number of injuries he had:"
+ this.getNumberOfInjuries() +",,"+"NumberoFTDs" + this.getNumberOfTDs()+ ",, "
+ "totalYards" + this.getTotalYards()+", ,"+"number of Interceptions thrown"+this.getInterceptionsThrown()+", ,"+"the percentage is "+this.getComspletionPercentage();
}
/**
* @return the name
*/
@Override
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the interceptionsThrown
*/
public int getInterceptionsThrown() {
return interceptionsThrown;
}
/**
* @param interceptionsThrown the interceptionsThrown to set
*/
public void setInterceptionsThrown(int interceptionsThrown) {
this.interceptionsThrown = interceptionsThrown;
}
/**
* @return the comspletionPercentage
*/
public double getComspletionPercentage() {
return comspletionPercentage;
}
/**
* @param comspletionPercentage the comspletionPercentage to set
*/
public void setComspletionPercentage(double comspletionPercentage) {
this.comspletionPercentage = comspletionPercentage;
}
}
/*creating defensive player class which is sub class of football*/
class DefensivePlayer extends FootballPlayer{
private int numberOfInterceptions;
private int numberOfTackles ;
/**
*
* @param name
* @param gamesPlayed
* @param numberOfInjuries
* @param numberOfInterceptions
* @param numberOfTackles
*/
public DefensivePlayer(String name,int gamesPlayed,int numberOfInjuries,int numberOfInterceptions, int numberOfTackles){
super(name, gamesPlayed, numberOfInjuries);
}
/**
* @return the numberOfInterceptions
*/
public int getNumberOfInterceptions() {
return numberOfInterceptions;
}
/**
* @param numberOfInterceptions the numberOfInterceptions to set
*/
public void setNumberOfInterceptions(int numberOfInterceptions) {
this.numberOfInterceptions = numberOfInterceptions;
}
/**
* @return the numberOfTackles
*/
public int getNumberOfTackles() {
return numberOfTackles;
}
/**
* @param numberOfTackles the numberOfTackles to set
*/
public void setNumberOfTackles(int numberOfTackles) {
this.numberOfTackles = numberOfTackles;
}
public String toString() {
return "Name of the player:" + this.getName() + ",, "
+ "number of games Played:" + this.getGamesPlayed() + ",, " + "number of injuries he had:"
+ this.getNumberOfInjuries() +",,"+"number of tackles" + this.getNumberOfTackles()+ ",, "
+ "number of interceptions are" + this.getNumberOfInterceptions();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.