The first class represents student athletes, and is derived from class Student (
ID: 3759669 • Letter: T
Question
The first class represents student athletes, and is derived from class Student (provided on blackboard). An object of class StudentAthlete will have a name (inherited from class Person, also provided on blackboard), and a student number (inherited from class Student), as well as the following data fields: 1. A jersey number (an int value between 0 and 99). 2. A grade point average (a double value between 0.0 and 4.0). Your StudentAthlete class should define constructors for each of the following scenarios: 1. The name, student number, jersey number, and GPA are known. 2. Only the name and student number are known. 3. Only the name is known. Be sure that your constructors initialize unspecified data fields to reasonable values. Add a full complement of accessor and mutator methods to your class. Next, write a writeOutput method that displays the name, student number, jersey number, and GPA to the screen. (Note that this method will call the writeOutput method inherited from Student to display the name and student number.) Finally, add a main method to your StudentAthlete class that creates at least four student athlete objects and thoroughly demonstrates the correct operation of each of the methods of your class, including such inherited methods as “getName” and “equals”. The second class represents hockey players, and is derived from your StudentAthlete class. Your HockeyPlayer class should add two data fields: goals and assists (both of type int). Include constructors that handle each of the following scenarios: 1. The name, student number, jersey number, and GPA are known. 2. Only the name and student number are known. 3. Only the name is known. Note that these constructors should initialize the goals and assists fields to 0, and should utilize the appropriate four-, two-, or one-parameter constructor inherited from StudentAthlete to set the remaining fields. Add accessor and mutator methods to your class, including an accessor that returns the number of points earned by the player. (In hockey, a player earns one point for each goal and each assist.) Write a writeOutput method that displays all six data fields and the point total to the screen. Finally, add a main method to your HockeyPlayer class that demonstrates the correct operation of all of your methods (including inherited methods). Submit Java source code files defining your Person, Student, StudentAthlete, and HockeyPlayer classes.
Explanation / Answer
//Four java source code java files
//Person.java
public class Person
{
//instance name variable
private String name;
//default constructor
public Person()
{
name = "unknown";
}
//parameterized name
public Person(String name)
{
setName(name);
}
//Method to set name
public void setName(String name)
{
this.name=name;
}
//Method returns the name
public String getName()
{
return name;
}
//Method that prints name
public void writeOutput()
{
System.out.println("Name: " + name);
}
}
------------------------------------------------------------------------------------------------------------------------------------
//This version has material on toString added.
public class Student extends Person
{
private int studentid;
//default constructor
public Student()
{
super();
studentid = 0;
}
//parameterized constructor
public Student(String initialName, int studentid)
{
//call super class constructor
super(initialName);
setStudentNumber(studentid);
}
//Returns student number
public int getStudentNumber()
{
return studentid;
}
//Set student number
public void setStudentNumber(int studentid)
{
if(studentid>=0 && studentid<=99)
this.studentid = studentid;
else
studentid=0;
}
//Method writeOutput that prints name and student id number
public void writeOutput()
{
System.out.println("STUDENT");
System.out.println("Name: " + getName());
System.out.println("Student Number : " + studentid);
}
//Override the method equals that returns true otherwise
//returns false
public boolean equals(Student otherStudent)
{
return (getName().equals(otherStudent.getName())
&& studentid == otherStudent.getStudentNumber());
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
//StudentAthletes.java
//The class StudentAthletes that extends the Student class
public class StudentAthletes extends Student
{
//instance variables of StudentAthletes
private double gpa;
private int jersey;
//constructor to set default values
public StudentAthletes()
{
super();
jersey = 0;
gpa = 0.0;
}
//parameterized constructor to set name, id and gpa
public StudentAthletes(String name, int studentid,
double gpa,int jersey)
{
//Student class constructor
super(name, studentid);
//call setGpa method
setGpa(gpa);
setJersey(jersey);
}
//Method to set jersey nummber
private void setJersey(int jersey)
{
this.jersey=jersey;
}
//Check if gpa is in between 0 and 4
private void setGpa(double gpa)
{
//Check if gpa is in between 0 and 4
if(gpa>=0 && gpa<=4)
this.gpa=gpa;
else
gpa=0;
}
//Returns gpa value
public double getGpa()
{
return gpa;
}
//Returns the jersey number
public int getJersey()
{
return jersey;
}
//WriteOutput that prints the student athlete details
public void writeOutput()
{
System.out.println("STUDENT ATHLETE");
System.out.println("Name: " + getName());
System.out.println("Student Number : " +getStudentNumber());
System.out.println("GPA: " + gpa);
System.out.println("Jersey No: " + jersey);
}
public static void main(String[] args)
{
//Create an instance of StudentAthletes class
//set name, number and gpa values
StudentAthletes studentAthlete = new StudentAthletes("Johnson",
99, 4.0,99);
studentAthlete.writeOutput();
}
}
------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
STUDENT ATHLETE
Name: Johnson
Student Number : 99
GPA: 4.0
Jersey No: 99
------------------------------------------------------------------------------------------------------------------------------------------
//HockeyPlayer.java
//The class HockeyPlayer inherited methods from StudentAthletes
public class HockeyPlayer extends StudentAthletes
{
//instance variables of class
private int goals;
private int assists;
//constructor of class
public HockeyPlayer()
{
//call constructor of class
super();
//set default values of class instance variables
goals=0;
assists=0;
}
//parameterized constructor that takes six arguments
public HockeyPlayer(String name, int studentid,
double gpa,int jersey,int goals, int assists)
{
//call super class constructor with name, number ,gpa and jersey number
super(name, studentid, gpa,jersey);
this.goals=goals;
this.assists=assists;
}
//Returns goals
public int getGoals()
{
return goals;
}
//Returns number of assists
public int getAssists()
{
return assists;
}
//print the details of the HockeyPlayer
public void writeOutput()
{
System.out.println("HOCKEY PLAYER");
System.out.println("Name: " + getName());
System.out.println("Student Number : " +getStudentNumber());
System.out.println("GPA: " + getGpa());
System.out.println("Jersey No: " + getJersey());
System.out.println("Goals :"+goals);
System.out.println("Assists :"+assists);
System.out.println("Total points : "+(goals+assists));
}
//main method that creates an instance of HockeyPlayer
public static void main(String[] args)
{
//Set values of constructor name, number, gpa , jersey number,
//goals and assists
HockeyPlayer player1=new HockeyPlayer("johnson",
55, 3.5,100, 800, 2500);
//prints the details of the HockeyPlayer object
player1.writeOutput();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
HOCKEY PLAYER
Name: johnson
Student Number : 55
GPA: 3.5
Jersey No: 100
Goals :800
Assists :2500
Total points : 3300
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.