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

Lab #1 Objectives for this lab: Deriving classes from parent classes Overriding

ID: 3880000 • Letter: L

Question

Lab #1

Objectives for this lab:

Deriving classes from parent classes

Overriding methods

Visibility when dealing with inheritance

Object oriented design with abstract classes

Creating an All Star Roster

For this lab you will be showing what you’ve learned about inheritance. You will need to complete the following steps:

Create a parent class called ProPlayerProPlayer should have the following specs:

A protected string variable to take in a player’s name

A private base salary variable set to a specified amount of your choosing.

A constructor that takes in the player’s name

A public method that returns the base salary you have defined

A method that prints the players name

An abstract method that for computing pay

Create a class called HockeyPlayer that is a child class of ProPlayerHockeyPlayer should have the following specs:

Variables to hold the team name of the player and the bonus pay of the player

A constructor that takes in and stores the player’s name(think super here), team name, and bonus pay

A method that returns the calculated pay of the player using the base pay plus the bonus pay.( the base pay needs to come from the parent class.

A method that overrides the parent’s method that prints the name but also includes the additional player info. (HINT: you should add to the parent’s print method by calling it inside the child method)

Create a main program class called AllStarRosterAllStarRoster should have the following specs:

A main method that creates and array of HockeyPlayer objects of size 6

Populate the 6 indexes with hockey player objects

Create a for each loop that will print all the player info you entered.

A sample output could look like this:

Sidney Crosby plays for the Penguins and makes $1250000 a year.

Jaromir Jagr plays for the Penguins and makes $1250000 a year.

Wayne Gretzky plays for the Kings and makes $2270300 a year.

Bobby Orr plays for the Bruins and makes $3776120 a year.

Doug Glatt plays for the Flyers and makes $515896 a year.

Mario Lemieux plays for the Penguins and makes $350000 a year.

Deliverables:

AllStarRoster.java

HockeyPlayer.java

ProPlayer.java

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

ProPlayer.java
===============
public abstract class ProPlayer {
protected String name;
private int baseSalary = 1000000;
public ProPlayer(String name1)
{
this.name = name1;
}
public int getBaseSalary()
{
return baseSalary;
}
public void print()
{
System.out.print(name);
}
public abstract int computePay();
}



HockeyPlayer.java
=================
public class HockeyPlayer extends ProPlayer {
private String teamName;
private int bonus;
public HockeyPlayer(String pname, String tname, int bonus1 )
{
super(pname);
teamName = tname;
bonus = bonus1;
}
@Override
public int computePay() {
return getBaseSalary() + bonus;
}
@Override
public void print()
{
super.print();
System.out.print(" plays for the " + teamName + " and makes " + computePay() + " a year. ");
}
}


AllStarRoaster.java
====================
public class AllStarRoaster {
public static void main(String[] args) {
HockeyPlayer[] players = new HockeyPlayer[6];
players[0] = new HockeyPlayer("Sidney Crosby", "Penguins", 250000);
players[1] = new HockeyPlayer("Jaromir Jagr", "Penguins", 250000);
players[2] = new HockeyPlayer("Wayne Gretzky", "Kings", 1270300);
players[3] = new HockeyPlayer("Bobby Orr", "Bruins", 2776120);
players[4] = new HockeyPlayer("Doug Glatt", "Flyers", 4158960);
players[5] = new HockeyPlayer("Mario Lemieux", "Penguins", 2500000);
for(int i = 0; i < 6; i++)
players[i].print();
}
}

output
======
Sidney Crosby plays for the Penguins and makes 1250000 a year.
Jaromir Jagr plays for the Penguins and makes 1250000 a year.
Wayne Gretzky plays for the Kings and makes 2270300 a year.
Bobby Orr plays for the Bruins and makes 3776120 a year.
Doug Glatt plays for the Flyers and makes 5158960 a year.
Mario Lemieux plays for the Penguins and makes 3500000 a year.