Question
Do not use this code need a different one please // Person class public class Person { // Attributes private String firstName; private String lastName;
Do not use this code need a different one please
// Person class
public class Person {
// Attributes
private String firstName;
private String lastName;
private String hometown;
private String state;
// Constructors
public Person() {
firstName = "No";
lastName = "Name";
hometown = "N/A";
state = "N/A";
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
hometown = "N/A";
state = "N/A";
}
public Person(String firstName, String lastName, String hometown, String state) {
this.firstName = firstName;
this.lastName = lastName;
this.hometown = hometown;
this.state = state;
}
// Methods
@Override
public String toString() {
String stateString;
if (state.equals("N/A")) {
stateString = "N/A";
} else if (state.equals("PA")) {
stateString = "is from Pennsylvania";
} else {
stateString = "is from out-of-state";
}
return "Person{firstName=" + firstName + ", lastName=" + lastName + ", hometown=" + hometown + ", state=" + stateString + "}";
}
public void initials() {
String firstInitial = firstName.substring(0,1) + ".";
String lastInitial = lastName.substring(0,1) + ".";
System.out.println(firstInitial + lastInitial);
}
public void initials(int option) {
String fullInitials;
if (option == 1) {
fullInitials = firstName.substring(0,1) + "." + lastName;
} else {
fullInitials = firstName + lastName.substring(0,1) + ".";
}
System.out.println(fullInitials);
}
}
// App class
public class App {
public static void main(String[] args) {
Person p1 = new Person("Jillian", "Jennings", "Montclair", "NJ");
Person p2 = new Person("Keaton", "Ellis");
Person p3 = new Person();
System.out.println(p1.toString());
System.out.println(p2.toString());
System.out.println(p3.toString());
p1.initials();
p2.initials(1);
p3.initials(2);
}
}
In this lab you will create and use methods in the Person class, including this special method, the constructor. Deliverable A zipped NetBeans project with 2 classes - App - Person Classes \begin{tabular}{|l|l|} \hline \multicolumn{1}{|c|}{ App } \\ \hline - Creates a Person object p1 using the four-parameter constructor. \\ - Creates a Person object p2 using the two-parameter constructor. \\ - Creates a Person object p3 using the no-parameter constructor. \\ - Uses the method toString() to display the data from the 3 objects \end{tabular} Suggestion: - Use Netbeans to copy your last lab (Lab 02) to a new project called Lab03. - Close Lab02. - Work on the new Lab03 project then. The Person Class 1. Attributes - String firstName - String lastName - String hometown - String state 2. Constructors - one constructor with no input parameters - since 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 - one constructor with two parameters - firstName using the input parameter - lastName using the input parameter - use the default values for - hometown - N/A - state - N/A - one constructor with all (four) parameters - one input parameter for each attribute 3. Methods - public String toString0) - 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 but with an update to account for the new possible value "N/A. - if the state attribute is "N/A", display the value as it is "N/A". - 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 firstName to a String - gets lastName - extract its initials - adds a ". period to it - adds it to the String - and uses "System.outprintln" to display the String The App class - create a Person object called p1 using the four-parameter constructor with the values - firstName - Jillian - lastName - Jennings - hometown-Montclair - state - NJ 2. create a Person object called p2 using the two-parameter constructor with the value - firstName - Keaton - lastName - Ellis 3. create a Person object called p3 using the no-parameter constructor 4. display all the data from each object Output The output should be similar to Person\{firstNane= Jillan, 1astNane=Jennings, hometown=Montclair, state=out-of-state\} Person\{firstNane=Keaton, 1 astName=E11ls, hometown=N/A, state=N/A\} Person\{firstNane-No, las tName=Nane, honetown=N/A, state=N/A
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