Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STEP 1 – Create Superclass and Subclasses Create an Eclipse project named JohnDo

ID: 3725549 • Letter: S

Question

STEP 1 – Create Superclass and Subclasses

Create an Eclipse project named JohnDoeHw7, and use the default package, i.e., no package name should be provided. Then inside this project, create the parent class (superclass) Player as specified in the table below. Save it in a file Player.java.

Also create the three child classes (subclasses) shown below. Save each class in a separate file – BaseballPlayer.java, FootballPlayer.java, and BasketballPlayer.java

this template file includes the appropriate program header and comments in the class body.

Class names

Player

(superclass)

BaseballPlayer
(subclass of Player)

FootballPlayer  

(subclass of Player)

BasketballPlayer
(subclass of Player)

private data

id

number of hits

number of yards

number of shots made

player name

number of times at bat

number of rushes

number of shots attempted

team name

position

salary

commission rate

public methods

constructors

constructors

constructors

constructors

getters

getters

getters

getters

setters

setters

setters

setters

calculate comission

calculate statistics

calculate statistics

calculate statistics

determine status

determine status

determine status

Detail specifications:

In class Player:

id is of int type; player name, team name, position is of String type; salary, commission rate is of double type.

There should be two constructors, one is the default constructor with no parameter, and the other has all six parameters to initialize the six private data fields, respectively.

There should be a public getter and setter for each of the six private data fields.

There is one effector method to calculate the commission by multiplying the salary with the commission rate. This method return a double type.

In class BaseballPlayer:

The two private data fields are int type.

There should be two constructors, one is the default constructor with no parameter, and the other has all eight parameters to initialize the six inherited data fields plus the two private data fields, respectively.

There should be a public getter and setter for each of the two private data fields.

There are two effector methods:

calculate statistics : calculate the player’s batting average by dividing “the number of hits” by “the number of times at bats”, it should return a double type

determine status : boolean return type. If the batting average is more than 0.25, return true; o/w return false

In class FootballPlayer:

The two private data fields are int type.

There should be two constructors, one is the default constructor with no parameter, and the other has all eight parameters to initialize the six inherited data fields plus the two private data fields, respectively.

There should be a public getter and setter for each of the two private data fields.

There are two effector methods:

calculate statistics : Calculate the player’s rushing average by dividing “the number of yards” by “the number of rushes” , it should return a double type

determine status : boolean return type. If the rushing average is more than 3.5, return true; o/w return false

In class BasketballPlayer:

The two private data fields are int type.

There should be two constructors, one is the default constructor with no parameter, and the other has all eight parameters to initialize the six inherited data fields plus the two private data fields, respectively.

There should be a public getter and setter for each of the two private data fields.

There are two effector methods:

calculate statistics : Calculate the player’s shot percentage by dividing “the number of shots made” by “the number of shots attempted” , it should return a double type

determine status : boolean return type. If the shot percentage is more than 0.32, return true; o/w return false

The three constant values mentioned in the determine status method as thresholds should be defined as public static final variable in the three subclasses, respectively.

In the same Eclipse project, create a new class file named JohnDoeHw7.java.

This application file has the main method, and inside the main method, it will create two instances of BaseballPlayer, two instances of FootballPlayer, and two instances of BasketballPlayer.

For each instance, you should use the constructor with 8 parameters, and then plug in all 8 parameters directly in the constructor. You can refer to source code “TestFruit.java” in file “inheritanceExample.zip”, and see how object myOrange is constructed.

You can use arbitrary values for the 8 parameters in each instance, as long as they are reasonable, for example, in basketball player, number of shots made should be less than number of shots attempted.

Then for each player instance, you need to output the six inherited data fields from Player class, and its commissions, then output the two private date fields, and then the statistics of this player, and the “keeping status” of this player: true of false. An example output for a football player is as below:

player id: 20

name: Barry Sanders

team: OSU

position: running back

salary: 1000000.00

commission rate: 0.02

commission: 20000.00

number of yards: 2850

number of rushes: 373

statistics: 7.6

keeping status: true

The output precision requirement: for monetary items, two digits after the decimal point; for statistics, one digit after the decimal point.

Generate a simialr output as above for all six players. When you plus in arbitrary parameters to initialize six playes, you can carefully design the parameters so that in each sport category, one player has “keeping status” as true, and the other player has it as false.

NOTE – ADDITIONAL SPECIFICATIONS!!

¨You do NOT need to use any array in this homework.

¨The names of the classes must be exactly as stated in the table in page 1 of this document.

¨You can not CHANGE, ADD or DELETE any instance variable or method to the Player class and its subclasses. However, inside each method, you have the freedom to use and name any local variable that you need. Also in the main method of the application class, you have the freedom to name and use any local variable.

¨For class Player and its subclasses, you must provide a constructor without arguments and a constructor with a full list of arguments to initialize all inherited data fields (if applicable), and all private data fields.

¨In main method, you must use constructors with actual parameters to create the required six objects, by hardcoding the arguments in the contructors. NO USER INPUT is needed in this homework!

¨For the determine statistics method in each subclass, the division should happen between two int type variables, and the result should be double. How do you achieve that? Hint: in the division statement, type converting the dividend from int to double will yiled a double result, such as   (double) dividend / divisor

¨Except for the three threshold values in the three subclasses, all class data must be private, and you must provide public getters and setters for each private instance data.

¨The methods to calculate statistics must have the exactly SAME name in all classes.

¨The methods to determine the player’s status must have exactly the SAME name in all classes and must all return a boolean.

¨You do NOT need to use any abstract class or abstract method in this homework.

¨Put each class in a separate file –Player.java, BasketballPlayer.java, FootballPlayer.java,.java, BaseballPlayer.java, and JohnDoeHw7.java(for application program that has the main method).

Class names

Player

(superclass)

BaseballPlayer
(subclass of Player)

FootballPlayer  

(subclass of Player)

BasketballPlayer
(subclass of Player)

private data

id

number of hits

number of yards

number of shots made

player name

number of times at bat

number of rushes

number of shots attempted

team name

position

salary

commission rate

public methods

constructors

constructors

constructors

constructors

getters

getters

getters

getters

setters

setters

setters

setters

calculate comission

calculate statistics

calculate statistics

calculate statistics

determine status

determine status

determine status

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

1. Defined the super class Player.java with all required attributes and methods

2. Defined a class BaseballPlayer extended from Player with all required attributes and methods

3. Defined a class FootballPlayer extended from Player with all required attributes and methods

4. Defined a class BasketballPlayer extended from Player with all required attributes and methods

5. Defined JohnDoeHw7.java, defined 2 objects of BaseballPlayer, BasketballPlayer and FootballPlayer each, and

displayed their details on the screen

6. Comments are included, If you have any doubts, feel free to ask, Thanks

EDIT: Getting error while trying to paste without losing the format. Says “Answer cannot be longer than 65000 characters”

, so I’m pasting the answer as plain text, forgive me if the code indentation is messed up. (copy the code into Eclipse,

and press Ctrl+Shift+F to format it easily)

// Player.java

public class Player {

//attributes

private int id;

private String playerName;

private String teamName;

private String position;

private double salary;

private double commisionRate;

//def constructor

public Player() {

id=0;

playerName="";

teamName="";

position="";

salary=0;

commisionRate=0;

}

//parameterized constructor

public Player(int id, String playerName, String teamName, String position,

double salary, double commisionRate) {

this.id = id;

this.playerName = playerName;

this.teamName = teamName;

this.position = position;

this.salary = salary;

this.commisionRate = commisionRate;

}

//getters and setters

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getPlayerName() {

return playerName;

}

public void setPlayerName(String playerName) {

this.playerName = playerName;

}

public String getTeamName() {

return teamName;

}

public void setTeamName(String teamName) {

this.teamName = teamName;

}

public String getPosition() {

return position;

}

public void setPosition(String position) {

this.position = position;

}

public double getSalary() {

return salary;

}

public void setSalary(double salary) {

this.salary = salary;

}

public double getCommisionRate() {

return commisionRate;

}

public void setCommisionRate(double commisionRate) {

this.commisionRate = commisionRate;

}

/**

* calculates and return the commision

*/

public double calculateCommision(){

return salary*commisionRate;

}

}

// BaseballPlayer.java

public class BaseballPlayer extends Player {

// constant for storing the limit for determining status

public static final double STATUS_THRESHOLD = 0.25;

//extra attributes

private int numberOfHits;

private int numberOfTimesAtBat;

//def constructor

public BaseballPlayer() {

super();

numberOfHits = 0;

numberOfTimesAtBat = 0;

}

public BaseballPlayer(int id, String playerName, String teamName,

String position, double salary, double commisionRate,

int numberOfHits, int numberOfTimesAtBat) {

/**

* passing values to super class

*/

super(id, playerName, teamName, position, salary, commisionRate);

this.numberOfHits = numberOfHits;

this.numberOfTimesAtBat = numberOfTimesAtBat;

}

/*getters and setters*/

public int getNumberOfHits() {

return numberOfHits;

}

public void setNumberOfHits(int numberOfHits) {

this.numberOfHits = numberOfHits;

}

public int getNumberOfTimesAtBat() {

return numberOfTimesAtBat;

}

public void setNumberOfTimesAtBat(int numberOfTimesAtBat) {

this.numberOfTimesAtBat = numberOfTimesAtBat;

}

/**

* calculates and return the stats

*/

public double calculateStatistics() {

if (numberOfTimesAtBat == 0) {

/**

* in case if number of times batted is 0, returning 0, or else it

* will cause an ArithmeticException when divided by zero

*/

return 0;

}

return (double) numberOfHits / numberOfTimesAtBat;

}

/**

* returns true of stats > limit , else false

*/

public boolean determineStatus() {

if (calculateStatistics() > STATUS_THRESHOLD) {

return true;

} else {

return false;

}

}

}

// BasketballPlayer.java

public class BasketballPlayer extends Player {

// constant for storing the limit for determining status

public static final double STATUS_THRESHOLD=0.32;

//extra attributes

private int numberOfShotsMade;

private int numberOfShotsAttempted;

public BasketballPlayer() {

super();

numberOfShotsMade = 0;

numberOfShotsAttempted = 0;

}

public BasketballPlayer(int id, String playerName, String teamName,

String position, double salary, double commisionRate,

int numberOfShotsMade, int numberOfShotsAttempted) {

/**

* passing data to super class

*/

super(id, playerName, teamName, position, salary, commisionRate);

this.numberOfShotsAttempted = numberOfShotsAttempted;

this.numberOfShotsMade = numberOfShotsMade;

}

public int getNumberOfShotsMade() {

return numberOfShotsMade;

}

public void setNumberOfShotsMade(int numberOfShotsMade) {

this.numberOfShotsMade = numberOfShotsMade;

}

public int getNumberOfShotsAttempted() {

return numberOfShotsAttempted;

}

public void setNumberOfShotsAttempted(int numberOfShotsAttempted) {

this.numberOfShotsAttempted = numberOfShotsAttempted;

}

/**

* calculates and return the stats

*/

public double calculateStatistics() {

if (numberOfShotsAttempted == 0) {

/**

* in case if number of shots attempted is 0, returning 0, or else it

* will cause an ArithmeticException when divided by zero

*/

return 0;

}

return (double) numberOfShotsMade / numberOfShotsAttempted;

}

/**

* returns true of stats > limit , else false

*/

public boolean determineStatus() {

if (calculateStatistics() > STATUS_THRESHOLD) {

return true;

} else {

return false;

}

}

}

// FootballPlayer.java

public class FootballPlayer extends Player {

// constant for storing the limit for determining status

public static final double STATUS_THRESHOLD=3.5;

//extra attributes

private int numberOfYards;

private int numberOfRushes;

public FootballPlayer() {

super();

numberOfYards = 0;

numberOfRushes = 0;

}

public FootballPlayer(int id, String playerName, String teamName,

String position, double salary, double commisionRate,

int numberOfYards, int numberOfRushes) {

/**

* passing data to super class

*/

super(id, playerName, teamName, position, salary, commisionRate);

this.numberOfRushes = numberOfRushes;

this.numberOfYards = numberOfYards;

}

public int getNumberOfYards() {

return numberOfYards;

}

public void setNumberOfYards(int numberOfYards) {

this.numberOfYards = numberOfYards;

}

public int getNumberOfRushes() {

return numberOfRushes;

}

public void setNumberOfRushes(int numberOfRushes) {

this.numberOfRushes = numberOfRushes;

}

/**

* calculates and return the stats

*/

public double calculateStatistics() {

if (numberOfRushes == 0) {

/**

* in case if number of rushes is 0, returning 0, or else it

* will cause an ArithmeticException when divided by zero

*/

return 0;

}

return (double) numberOfYards / numberOfRushes;

}

/**

* returns true of stats > limit , else false

*/

public boolean determineStatus() {

if (calculateStatistics() > STATUS_THRESHOLD) {

return true;

} else {

return false;

}

}

}

// JohnDoeHw7.java

public class JohnDoeHw7 {

public static void main(String[] args) {

/**

* Defining two baseball players

*/

BaseballPlayer player1 = new BaseballPlayer(1234, "Daenarys",

"Targeryens", "Catcher", 2500.55, 0.5, 7, 10);

BaseballPlayer player2 = new BaseballPlayer(1235, "Aegon",

"Targeryens", "Shortstop", 3000, 0.2, 8, 11);

/**

* Defining two football players

*/

FootballPlayer player3 = new FootballPlayer(2200, "Arya", "Starks",

"Lineman", 1000, 0.02, 300, 20);

FootballPlayer player4 = new FootballPlayer(2211, "Jon", "Snows",

"Tight end", 1234, 1.9, 200, 26);

/**

* Defining two basketball players

*/

BasketballPlayer player5 = new BasketballPlayer(8966, "Cersei",

"Lannisters", "Point guard", 3005, 0.05, 15, 20);

BasketballPlayer player6 = new BasketballPlayer(8966, "Brienne",

"Taarth", "Swing man", 772, 0.7, 12, 38);

/**

* Printing details of each players

*/

System.out.println("Player id: " + player1.getId());

System.out.println("name: " + player1.getPlayerName());

System.out.println("team: " + player1.getTeamName());

System.out.println("position: " + player1.getPosition());

System.out.printf("Salary: %.2f ", player1.getSalary());

System.out.printf("Commision rate: %.2f ", player1.getCommisionRate());

System.out.printf("Commision: %.2f ", player1.calculateCommision());

System.out.println("number of hits: " + player1.getNumberOfHits());

System.out.println("number of times at bat: "

+ player1.getNumberOfTimesAtBat());

System.out.printf("statistics: %.1f ", player1.calculateStatistics());

System.out.println("keeping status: " + player1.determineStatus());

System.out.println();

System.out.println("Player id: " + player2.getId());

System.out.println("name: " + player2.getPlayerName());

System.out.println("team: " + player2.getTeamName());

System.out.println("position: " + player2.getPosition());

System.out.printf("Salary: %.2f ", player2.getSalary());

System.out.printf("Commision rate: %.2f ", player2.getCommisionRate());

System.out.printf("Commision: %.2f ", player2.calculateCommision());

System.out.println("number of hits: " + player2.getNumberOfHits());

System.out.println("number of times at bat: "

+ player2.getNumberOfTimesAtBat());

System.out.printf("statistics: %.1f ", player2.calculateStatistics());

System.out.println("keeping status: " + player2.determineStatus());

System.out.println();

System.out.println("Player id: " + player3.getId());

System.out.println("name: " + player3.getPlayerName());

System.out.println("team: " + player3.getTeamName());

System.out.println("position: " + player3.getPosition());

System.out.printf("Salary: %.2f ", player3.getSalary());

System.out.printf("Commision rate: %.2f ", player3.getCommisionRate());

System.out.printf("Commision: %.2f ", player3.calculateCommision());

System.out.println("number of yards: " + player3.getNumberOfYards());

System.out.println("number of rushes: "

+ player3.getNumberOfRushes());

System.out.printf("statistics: %.1f ", player3.calculateStatistics());

System.out.println("keeping status: " + player3.determineStatus());

System.out.println();

System.out.println("Player id: " + player4.getId());

System.out.println("name: " + player4.getPlayerName());

System.out.println("team: " + player4.getTeamName());

System.out.println("position: " + player4.getPosition());

System.out.printf("Salary: %.2f ", player4.getSalary());

System.out.printf("Commision rate: %.2f ", player4.getCommisionRate());

System.out.printf("Commision: %.2f ", player4.calculateCommision());

System.out.println("number of yards: " + player4.getNumberOfYards());

System.out.println("number of rushes: "

+ player4.getNumberOfRushes());

System.out.printf("statistics: %.1f ", player4.calculateStatistics());

System.out.println("keeping status: " + player4.determineStatus());

System.out.println();

System.out.println("Player id: " + player5.getId());

System.out.println("name: " + player5.getPlayerName());

System.out.println("team: " + player5.getTeamName());

System.out.println("position: " + player5.getPosition());

System.out.printf("Salary: %.2f ", player5.getSalary());

System.out.printf("Commision rate: %.2f ", player5.getCommisionRate());

System.out.printf("Commision: %.2f ", player5.calculateCommision());

System.out.println("number of shots made: " + player5.getNumberOfShotsMade());

System.out.println("number of shots attempted: "

+ player5.getNumberOfShotsAttempted());

System.out.printf("statistics: %.1f ", player5.calculateStatistics());

System.out.println("keeping status: " + player5.determineStatus());

System.out.println();

System.out.println("Player id: " + player6.getId());

System.out.println("name: " + player6.getPlayerName());

System.out.println("team: " + player6.getTeamName());

System.out.println("position: " + player6.getPosition());

System.out.printf("Salary: %.2f ", player6.getSalary());

System.out.printf("Commision rate: %.2f ", player6.getCommisionRate());

System.out.printf("Commision: %.2f ", player6.calculateCommision());

System.out.println("number of shots made: " + player6.getNumberOfShotsMade());

System.out.println("number of shots attempted: "

+ player6.getNumberOfShotsAttempted());

System.out.printf("statistics: %.1f ", player6.calculateStatistics());

System.out.println("keeping status: " + player6.determineStatus());

System.out.println();

}

}

/*OUTPUT*/

Player id: 1234

name: Daenarys

team: Targeryens

position: Catcher

Salary: 2500.55

Commision rate: 0.50

Commision: 1250.28

number of hits: 7

number of times at bat: 10

statistics: 0.7

keeping status: true

Player id: 1235

name: Aegon

team: Targeryens

position: Shortstop

Salary: 3000.00

Commision rate: 0.20

Commision: 600.00

number of hits: 8

number of times at bat: 11

statistics: 0.7

keeping status: true

Player id: 2200

name: Arya

team: Starks

position: Lineman

Salary: 1000.00

Commision rate: 0.02

Commision: 20.00

number of yards: 300

number of rushes: 20

statistics: 15.0

keeping status: true

Player id: 2211

name: Jon

team: Snows

position: Tight end

Salary: 1234.00

Commision rate: 1.90

Commision: 2344.60

number of yards: 200

number of rushes: 26

statistics: 7.7

keeping status: true

Player id: 8966

name: Cersei

team: Lannisters

position: Point guard

Salary: 3005.00

Commision rate: 0.05

Commision: 150.25

number of shots made: 15

number of shots attempted: 20

statistics: 0.8

keeping status: true

Player id: 8966

name: Brienne

team: Taarth

position: Swing man

Salary: 772.00

Commision rate: 0.70

Commision: 540.40

number of shots made: 12

number of shots attempted: 38

statistics: 0.3

keeping status: false