Question
Encapsulation In this lab you will start using encapsulation in the Person and in the Height classes. You will also create a special get method
Encapsulation
In this lab you will start using encapsulation in the Person and in the Height classes.
You will also create a special get method and a special set method.
Deliverable
A zipped NetBeans project with 3 classes
App
Person
Height
Classes
Suggestion:
Use Netbeans to copy your last lab (Lab 04) to a new project called Lab05.
Close Lab04.
Work on the new Lab05 project then.
The Person Class
uses encapsulation
private attributes
get and set methods for each attribute
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
Get methods should not change the value of attributes
In this case, it should not change/update the value of firstName
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()
toString( ) overrides the superclass Object toString( ) method
toString( ) returns information about this class attributes as a String
it returns a formatted String with feet and inches
for instance: 5'2"
The App class
create a Person object called p1 using the five-parameter constructor with the values
firstName - jillian (see the different capitalization used to test the get/set methods)
lastName - Jennings
height 5 7
hometown - Montclair
state - NJ
create a Person object called p2 using the three-parameter constructor with the value
firstName - KEATON (see the different capitalization used to test the get/set methods)
lastName - Ellis
height 5 11
create a Person object called p3 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}} Person{firstName=Keaton, lastName=ELLIS, hometown=N/A, state=N/A, height=Height{feet=5, inches=11}} Person{firstName=No, lastName=NAME, hometown=N/A, state=N/A, height=Height{feet=5, inches=6}}
I'd appreciate an answer including the required code, thank you for the 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