Question
Inheritance - Extending a class In this lab you will start using Inheritance to create a new class from an existing one. Deliverable A zipped
Inheritance - Extending a class
In this lab you will start using Inheritance to create a new class from an existing one.
Deliverable
A zipped NetBeans project with 4 classes
App
Person
Height
Student
Classes
Suggestion:
Use Netbeans to copy your last lab (Lab 05) to a new project called Lab06.
Close Lab05.
Work on the new Lab06 project then.
The Student class
A Student is a Person with 3 extra attributes, major, academic year and gpa.
Attributes
String major
String academicYear
double GPA
Constructorsone constructor with no input parameterssince it doesn't have input parameters, use the default data below
major - IST
academicYear - Sr.
GPA - 3.0
remember to call the constructor of the superclass with no parameters
one constructor with all the parameters
all three parameters from Student, one for each attribute
since this is a subclass, remember to include also the parameters for the superclass
Methods
Get and Set methods (a requirement from encapsulation)
public String toString()
returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
toString() is a special method, you will learn more about it in the next lessons
it needs to be public
it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
important: in the subclass toString, remember to make a call to the superclass toString so that the resulting String includes
data from the subclass Student
data from the superclass Person
The Person Class
Attributes
String firstName
String lastName
String hometown
String state
Height height
Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:
firstName - No
lastName - Name
hometown - N/A
state - N/A
height - use the Height class no parameter constructor
one constructor with three parameters
firstName using the input parameter
lastName using the input parameter
height using the input parameter
use the default values for
hometown - N/A
state - N/A
one constructor with all (five) parameters
one input parameter for each attribute
Methods
Get and Set methods (a requirement from encapsulation)
important:
You should start generating the default get and set methods using NetBeans automatic generator
Then you will change getFirstName and setLastName as specified. getFirstName
returns firstName with the first letter in upper case and the remaining of the String in lower case
getLastName
getHometown
getState
getHeight
setFirstName
setLastName
updates lastName to be all caps (all upper case)
remember to use setLastName in all constructors so the data (the updated lastName) is stored correctly
setHometown
setState
setHeight
public String toString()
returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
toString() is a special method, you will learn more about it in the next lessons
it needs to be public
it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
regarding state, the toString method will have a similar functionality as App had in the first lab.
if the state attribute is "PA", display the object's attribute name plus the message "is from Pennsylvania"
if the state attribute is not "PA", display the object's attribute name plus the message "is from out-of-state"
In short, the toString() method returns all the data from each object as a String
public void initials( )this method
gets firstName and lastName
extract the initials of each one of them
adds a "." period to each of them
and uses "System.out.println" to display them as one String
public void initials( int option)
this method overloads public void initials( ). This means, it has the same name, but a different number of parameters.
if the value of "option" is 1 gets firstName
extract its initials
adds a "." period to to a String
adds the lastName to this String
and uses "System.out.println" to display the String
if the value of "option" is 2
adds firsName to a String
gets lastName
extract its initials
adds a "." period to it
adds it to the String
and uses "System.out.println" to display the String
The Height class
uses encapsulation
private attributes
get and set methods for each attribute
Attributes
int feet
int inches
Constructorsone constructor with no input parameterssince it doesn't receive any input values, you need to use the default values below:
feet - 5
feet - 6
one constructor with two parameters
feet using the input parameter
inches using the input parameter
Methodspublic String toString()
returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
toString() is a special method, you will learn more about it in the next lessons
it needs to be public
it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
The App class
create a Student object called st1 using the all-parameter constructor with the values
major - Cyber
academicYear - Sr.
GPA - 3.5
firstName - jillian (see the different capitalization used to test the get/set methods)
lastName - Jennings
hometown - Montclair
height - 5 7
state - NJ
create a Student object called st2 using the all-parameter constructor with the value
major - IST
academicYear - Jr.
GPA - 3.5
firstName - KEATON (see the different capitalization used to test the get/set methods)
lastName - Ellis
height - 5 11
hometown - State College
state - PA
create a Student object called st3 using the no-parameter constructor
display all the data from each object
Output
The output should be similar to
Person{firstName=Jillian, lastName=JENNINGS, hometown=Montclair, state=out-of-state, height=Height{feet=5, inches=7}} Student{major=Cyber, academicYear=Sr., GPA=3.5} Person{firstName=Keaton, lastName=ELLIS, hometown=State College, state=Pennsylvania, height=Height{feet=5, inches=11}} Student{major=IST, academicYear=Jr., GPA=3.5} Person{firstName=No, lastName=NAME, hometown=N/A, state=N/A, height=Height{feet=5, inches=6}} Student{major=IST, academicYear=Sr., GPA=3.0}
Require code for ALL classes listed in addition to any explanation of concepts. Thank you for your assistance.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started