Polymor. Code (Java) Program Specifications: You are to design the following pro
ID: 3906346 • Letter: P
Question
Polymor. Code (Java)
Program Specifications:
You are to design the following project:
A person has a name, and age.
An athlete is a person. An athlete has a team and position.
A baseball player is an athlete and a person.
A baseball player has a battingPosition.
Baseball players either bat lefthanded, righthanded or both.
A football player is an athlete and a person.
A football player has a specialty.
A football player's speciality is either Offense, Defense, or Special Teams.
A hockey player is an athlete and a person.
A hockey player has a stickBrand.
A golfer is an athlete and a person.
A golfer has a mainSponser.
A soccer player is not an athlete or a person...therefore
soccer does not exist.
All sport type objects have a method named doThis();
Baseball -> doThis() displays I hit something.
Football -> doThis() displays I tackle something.
Hockey -> doThis() displays I sit in a penalty box.
Golfer -> doThis() displays I putt it in the hole.
...all sports have a doThis() method that displays something.
Main()
create the class Person, Athlete, Baseball
create the class Football, Hockey, Golfer
Create the doThis() method in each class
In main create the follow reference variables
Baseball Hank (create this reference variable)
Football Terry (create this reference variable)
Hockey Mario (create this reference variable)
Golfer Tiger (create this reference variable)
Baseball Barry (create this reference variable)
Football Payton (create this reference variable)
Hockey Wayne (create this reference variable)
Golfer Phil (create this reference variable)
Call each sports doThis method (one at a time) passing each player.
call the toString methods for each player object.
YOU CANNOT:
Use global variables, the word goto, the break command outside a case statement
Explanation / Answer
Note: If you have any doubt before giving negative feedback do let me know in the comment section below and if you really liked the solution then dont forget to give a big thumbs up.
Solution:
Person.java
//Person Class
public class Person {
// Person has name and age
// Declaring the fields for the same
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
Athlete.java
//Athlete is a person hence extends Person class
public class Athlete extends Person {
//Athlete has team and position
String team;
int position;
public Athlete(String name, int age, String team, int position) {
super(name, age);
this.team = team;
this.position = position;
}
}
BaseballPlayer.java
//Baseball player is an athlete and a person too hence extends both
//Athlete extends person hence Baseball player is both
public class BaseballPlayer extends Athlete{
// Baseball Player has batting position
// Baseball players either bat lefthanded, righthanded or both.
String battingPosition;
public BaseballPlayer(String name, int age, String team, int position, String battingPosition) {
super(name, age, team, position);
this.battingPosition = battingPosition;
}
// do this method to display message
public void doThis() {
System.out.println("I hit something.");
}
// Overriding toString method to get proper information about the object
@Override
public String toString() {
return "Baseball Player:"
+ " Name: "+ this.name
+ " Age: "+this.age
+" Batting Position:" + battingPosition;
}
}
FootballPlayer.java
//Football player is an athlete and a person too hence extends both
//Athlete extends person hence Football player is both
public class FootballPlayer extends Athlete{
//Football player has speciality
// A football player's speciality is either Offense, Defense, or Special Teams.
String speciality;
public FootballPlayer(String name, int age, String team, int position, String speciality) {
super(name, age, team, position);
this.speciality = speciality;
}
// do this method to display message
public void doThis() {
System.out.println("I tackle something.");
}
// Overriding toString method to get proper information about the object
@Override
public String toString() {
return "Football Player:"
+ " Name: "+ this.name
+ " Age: "+this.age
+" Speciality:" + speciality;
}
}
HockeyPlayer.java
//Hockey player is an athlete and a person too hence extends both
//Athlete extends person hence Hockey player is both
public class HockeyPlayer extends Athlete{
//Hockey player has stickBand
String stickBand;
public HockeyPlayer(String name, int age, String team, int position, String stickBand) {
super(name, age, team, position);
this.stickBand = stickBand;
}
// do this method to display message
public void doThis() {
System.out.println("I sit in a penalty box.");
}
// Overriding toString method to get proper information about the object
@Override
public String toString() {
return "Hockey Player:"
+ " Name: "+ this.name
+ " Age: "+this.age
+" Stick Band:" + stickBand;
}
}
Golfer.java
//Golfer player is an athlete and a person too hence extends both
//Athlete extends person hence Golfer player is both
public class Golfer extends Athlete{
//Golfer has a mainSPonser
String mainSponser;
public Golfer(String name, int age, String team, int position, String mainSponser) {
super(name, age, team, position);
this.mainSponser = mainSponser;
}
// do this method to display message
public void doThis() {
System.out.println("I put it in the hole.");
}
// Overriding toString method to get proper information about the object
@Override
public String toString() {
return "Golfer:"
+ " Name: "+ this.name
+ " Age: "+this.age
+" Main Sponser:" + mainSponser;
}
}
Polymor.java
//Driver class
public class Polymor {
public static void main(String[] args) {
// Creating objects
BaseballPlayer Hank = new BaseballPlayer("Hank", 26, "Chicago Cubs", 3,"lefthanded");
FootballPlayer Terry = new FootballPlayer("Terry", 30, "Argentina", 6, "Defense");
HockeyPlayer Mario = new HockeyPlayer("Mario", 24, "Montreak Canadiens", 8, "ABC");
Golfer Tiger = new Golfer("Tiger", 27, "ASD", 5, "Unknwon");
BaseballPlayer Barry = new BaseballPlayer("Barry", 36, "New York Yankees", 3,"righthanded");
FootballPlayer Payton = new FootballPlayer("Terry", 30, "Argentina", 6, "Attack");
HockeyPlayer Wayne = new HockeyPlayer("Wayne", 34, "Montreak Washington Capitals", 10, "SABC");
Golfer Phil = new Golfer("Phil", 21, "ASQD", 9, "known");
// Calling each doThis method
Hank.doThis();
Terry.doThis();
Mario.doThis();
Tiger.doThis();
Barry.doThis();
Payton.doThis();
Wayne.doThis();
Phil.doThis();
// Calling to string method by printing the objects
System.out.println(Hank);
System.out.println(Terry);
System.out.println(Mario);
System.out.println(Tiger);
System.out.println(Barry);
System.out.println(Payton);
System.out.println(Wayne);
System.out.println(Phil);
}
}
Sample Run:
I hit something.
I tackle something.
I sit in a penalty box.
I put it in the hole.
I hit something.
I tackle something.
I sit in a penalty box.
I put it in the hole.
Baseball Player:
Name: Hank
Age: 26
Batting Position:lefthanded
Football Player:
Name: Terry
Age: 30
Speciality:Defense
Hockey Player:
Name: Mario
Age: 24
Stick Band:ABC
Golfer:
Name: Tiger
Age: 27
Main Sponser:Unknwon
Baseball Player:
Name: Barry
Age: 36
Batting Position:righthanded
Football Player:
Name: Terry
Age: 30
Speciality:Attack
Hockey Player:
Name: Wayne
Age: 34
Stick Band:SABC
Golfer:
Name: Phil
Age: 21
Main Sponser:known