What Will You Learn Using inheritance to reuse classes. Deliverables person.java
ID: 3750913 • Letter: W
Question
What Will You Learn
Using inheritance to reuse classes.
Deliverables
person.java
student.java
studentAthlete.java
app.java
Students should apply consistent indenting in all submissions. This can be done via the NetBeans Source menu.
Contents
Using inheritance and the classes (below)
The person class has
first name
last name
age
hometown
a method called getInfo() that returns all attributes from person as a String
The student class is a person with
an id and
a major and
a GPA
student has to call super passing the parameters for the super class constructor
a method called getInfo() that returns all attributes from student as a String
this methods has to override the method getInfo() in person
The student-athlete class is a student with
a sports (football, or track, or soccer, volleyball, etc.)and
a ranking (a random number between 0 and 100)
student-athlete has to call super passing the parameters for the super class constructor
a method called getInfo() that returns all attributes from student-athlete as a String
this methods has to override the method getInfo() in student
Create an application (app) that:
Creates one student-athlete object.
Displays all information about the student-athlete object
Does it in the most efficient way possible (avoids unnecessary duplication of attributes and methods)
Uses the classes
app
person
student inheriting from person
student-athlete inheriting from student
What You Will Learn
Layouts: the organization of graphical components inside a panel.
Deliverables
app.java (initial application)
MainFrame.java (external JFrame)
ControlPanel, a Java class for the panel that will contain two other panels, using a layout.
TopPanel, a Java class for the Panel that will display the group’s name and group's average GPA
CenterPanel, a Java class for the Panel that will display names and semester GPAs of the 4 students in a group.
group.java and student.java (a working version from previous labs, might need updates, see important #2 below).
Students should apply consistent indenting in all submissions. This can be done via the NetBeans Source menu.
Contents
You can start with this NetBeans project.
You will create 3 panels and one group object. One panel contains two other panels.
On a top panel you will display the group's name and average GPA.
The center panel will contain 4 buttons displaying the group 4 students name and GPA.
Important - #1 - The single group object
You will create only one group object g1 in this assignment.
There will be only one statement group g1 = new group(...); in the whole application.
Since two panels need to be used, g1 will need to be created somewhere and then pass the object g1 as a parameter to other classes.
Important - #2 - GPA Calculation
Your previous group/student solution might be working this way; otherwise, you will need to updated it.
Because GPA is calculated randomly in student, depending how you calculate it there is a chance that the average group GPA will not match with the displayed sum of each student's GPA.
In order to fix this, you need to:
in group:
semesterGPA is calculated using the GPA attribute in student, not the semesterGPA() method in student
in student:
it needs GPA as an attribute
the attribute GPA is calculated in the constructor, when the student is created, calling the semesterGPA() random method
the semesterGPA() method, whenever it is called, updates the value of the attribute GPA
person First name Last name Age Hometown getlnfo() app Creates 1 student-athlete object 1-Displays the complete information about the student-athlete object student New attributes in addition to person's attributes Ild I Major GPA Overrides the method from the superclass person 1 getlnfol New attributes student's attributes Student-athlete in addition to sports ranking. Overrides the method from the i superclass student getinfol)Explanation / Answer
package inheritance;
import java.util.Random;
// Creates a base class Person
class Person
{
// Instance variable to store person data
String firstName;
String lastName;
int age;
String homeTown;
// Default constructor to initialize instance variables
Person()
{
firstName = lastName = homeTown = "";
age = 0;
}// End of default constructor
// Parameterized constructor to assign parameter value to instance variables
Person(String fn, String ln, int ag, String ht)
{
firstName = fn;
lastName = ln;
homeTown = ht;
age = ag;
}// End of parameterized constructor
// Method to return first name
String getFirstName()
{
return firstName;
}// End of method
// Method to return last name
String getLastName()
{
return lastName;
}// End of method
// Method to return home town
String getHomeTown()
{
return homeTown;
}// End of method
// Method to return age
int getAge()
{
return age;
}// End of method
// Method to return person information in string format
String getInfo()
{
return " Name " + firstName + " " + lastName + " Home Town: " + homeTown + " Age: " + age;
}// End of method
}// End of class Person
// Creates a class Student derived from Person
class Student extends Person
{
// Instance variable to store student data
String ID;
String major;
double gpa;
// Default constructor to initialize instance variables
Student()
{
// Calls the base class default constructor
super();
ID = major = "";
gpa = 0;
}// End of default constructor
// Parameterized constructor to assign parameter value to instance variables
Student(String fn, String ln, int ag, String ht, String id, String ma, double gp)
{
// Calls the base class parameterized constructor
super(fn, ln, ag, ht);
ID = id;
major = ma;
gpa = gp;
}// End of parameterized constructor
// Method to return id
String getID()
{
return ID;
}// End of method
// Method to return major
String getMajor()
{
return major;
}// End of method
// Method to return gpa
double getGPA()
{
return gpa;
}// End of method
// Overrides base class method to return student information in string format
String getInfo()
{
// Concatenates base class getInfo() return string with own class member information
return super.getInfo() + " ID: " + ID + " Major: " + major + " GPA: " + gpa;
}// End of method
}// End of class Student
//Creates a class student_athlete derived from Student
class student_athlete extends Student
{
// Instance variable to store student_athlete data
String sports;
int rank;
// Default constructor to initialize instance variables
student_athlete()
{
// Calls the base class default constructor
super();
sports = "";
rank = 0;
}// End of default constructor
// Parameterized constructor to assign parameter value to instance variables
student_athlete(String fn, String ln, int ag, String ht, String id, String ma, double gp, String sp, int ra)
{
// Calls the base class parameterized constructor
super(fn, ln, ag, ht, id, ma, gp);
sports = sp;
rank = ra;
}// End of parameterized constructor
// Method to return sports
String getSports()
{
return sports;
}// End of parameterized constructor
// Method to return rank
int getRank()
{
return rank;
}// End of method
// Overrides base class method to return student_athlete information in string format
String getInfo()
{
// Concatenates base class getInfo() return string with own class member information
return super.getInfo() + " Sports: " + sports + " Rank: " + rank;
}// End of method
}// End of class student_athlete
// Driver class App definition
public class App
{
// main method definition
public static void main(String[] args)
{
// Creates a random class object
Random rn = new Random();
// Declares an array of objects of class student_athlete of size 3
student_athlete st[] = new student_athlete[3];
// Dynamically creates objects using parameterized constructor
st[0] = new student_athlete("Pyari", "Sahu", 22, "BAM", "111", "Java", 9.6, "Cricket", rn.nextInt(100));
st[1] = new student_athlete("Suresh", "Panda", 42, "BBSR", "222", "C", 3.2, "Football", rn.nextInt(100));
st[2] = new student_athlete("Anita", "Swain", 34, "RKL", "333", "C++", 6.1, "Volleyball", rn.nextInt(100));
// Loops three time to display information by calling getInfo() method
for(int x = 0; x < st.length; x++)
System.out.println(st[x].getInfo());
}// End of main method
}// End of driver class App
Sample Output:
Name Pyari Sahu
Home Town: BAM
Age: 22
ID: 111
Major: Java
GPA: 9.6
Sports: Cricket
Rank: 7
Name Suresh Panda
Home Town: BBSR
Age: 42
ID: 222
Major: C
GPA: 3.2
Sports: Football
Rank: 38
Name Anita Swain
Home Town: RKL
Age: 34
ID: 333
Major: C++
GPA: 6.1
Sports: Volleyball
Rank: 98
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.