Modify the Polymorphism codeas shown below. Include an Abstract Class and an Int
ID: 3915816 • Letter: M
Question
Modify the Polymorphism codeas shown below.
Include an Abstract Class and an Interface (add at least one abstract method)
Modify your code to include Polymorphic Code (variables, methods, calls)
In comments, clearly label all polymorphic variable.
In comments, clearly label all polymorphic methods.
In comments, clearly label all polymorphic method calls.
Remove the reference variables and replace them with an array called [100] people.
The array people will be of type person.
You are to load the array with the data below. Feel free to add extra people.
Clean up the two string methods throughout the project. Make them neat and make them fit together.
A name is comprised of the following: firstName, middleInitial, lastName.
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 football player is an athlete and a person.
A hockey player is an athlete and a person.
A golfer is an athlete and a person.
Baseball players have a position.
Football players may play defense or offense or special teams. A player can play multiple positions.
A hockey player is a skater or a goalie (not both)
A golfer has a sponsor
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. The doThis() method must be name doThis() in all classes that have the method.
create the class Person, Athlete, Baseball
create the class Football, Hockey, Golfer
Create an equals methods for each Class
Create the doThis() method in each class
in main create and load the people array with the following:
Hank H. Aaron: who is a baseball player (make up the rest...)
Terry F. Bradshaw: a football player (make up the rest...)
Mario Lemieux: a hockey player (make up the rest...)
Tiger T. Woods: a golfer (make up the rest...)
Barry B. Bonds: a baseball player (make up the rest...)
Payton A. Manning: a football player (make up the rest...)
Wayne Gretzky: a golfer (make up the rest...)
Phil A. Mickleson: a golfer (make up the rest...)
add at least 5 or 10 more athletes
Create equals methods for each Class
By Sport, list the players that are equal to each other
Call each sports doThis() method (one at a time) passing each player.
call the toString methods for each player object.
Add a menu system that makes this all work.
import java.util.*;
class Person{
String name;
int age;
public void doThis(){
;
}
}
class Athlete extends Person {
String team;
String position;
public void doThis(){
;
}
}
class Baseball extends Athlete{
String batting_positon;
public void doThis(){
System.out.println("I hit something");
}
}
class Football extends Athlete {
String speciality;
public void doThis(){
System.out.println("I tackle something");
}
}
class Hockey extends Athlete{
String stick_brand;
public void doThis(){
System.out.println("I sit in a penalty box");
}
}
class Golfer extends Athlete {
String main_sponsor;
public void doThis(){
System.out.println("I putt it in the hole");
}
}
class MainClass{
public static void main(String[] args) {
Baseball hank = new Baseball();
Baseball barry = new Baseball();
Football terry = new Football();
Football payton = new Football();
Hockey mario = new Hockey();
Hockey wayne = new Hockey();
Golfer tiger = new Golfer();
Golfer phil = new Golfer();
//Call each sports doThis Method passing each
//player
call_do_this(hank);
call_do_this(barry);
call_do_this(terry);
call_do_this(payton);
call_do_this(mario);
call_do_this(wayne);
call_do_this(tiger);
call_do_this(phil);
}
public static void call_do_this(Person p){
p.doThis();
System.out.println(p.toString());
}
}
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.