Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do not use any similar code to this for plagiarism reasons. public class Height { int feet; int inches; public Height() { this.feet =

Please do not use any similar code to this for plagiarism reasons.

public class Height {

int feet;

int inches;

public Height() {

this.feet = 0;

this.inches = 0;

}

public Height(int feet, int inches) {

this.feet = feet;

this.inches = inches;

}

@Override

public String toString() {

return feet + " " + inches + "";

}

}

public class Person {

String firstName;

String lastName;

Height height;

String hometown;

String state;

public Person() {

firstName = "John";

lastName = "Doe";

height = new Height();

hometown = "Unknown";

state = "Unknown";

}

public Person(String firstName, String lastName, Height height) {

this.firstName = firstName;

this.lastName = lastName;

this.height = height;

hometown = "Unknown";

state = "Unknown";

}

public Person(String firstName, String lastName, Height height, String hometown, String state) {

this.firstName = firstName;

this.lastName = lastName;

this.height = height;

this.hometown = hometown;

this.state = state;

}

@Override

public String toString() {

String stateMessage = "";

if (state.equals("PA")) {

stateMessage = " is from Pennsylvania";

} else {

stateMessage = " is from an unknown state";

}

return firstName + " " + lastName + " from " + hometown + stateMessage + " with height " + height.toString();

}

}

public class App {

public static void main(String[] args) {

Person p1 = new Person("Jane", "Doe", new Height(5, 6), "Philadelphia", "PA");

Person p2 = new Person("John", "Doe", new Height(6, 2));

Person p3 = new Person();

System.out.println(p1.toString());

System.out.println(p2.toString());

System.out.println(p3.toString());

}

}

and do not use similar to this code.

public class Height {

int feet;

int inches;

public Height() {

feet = -5;

inches = 6;

}

public Height(int feet, int inches) {

this.feet = feet;

this.inches = inches;

}

@Override

public String toString() {

return feet + " " + inches + "";

}

}

public class Person {

String firstName;

String lastName;

Height height;

String hometown;

String state;

public Person() {

firstName = "No";

lastName = "Name";

height = new Height();

hometown = "N/A";

state = "N/A";

}

public Person(String firstName, String lastName, Height height) {

this.firstName = firstName;

this.lastName = lastName;

this.height = height;

hometown = "N/A";

state = "N/A";

}

public Person(String firstName, String lastName, Height height, String hometown, String state) {

this.firstName = firstName;

this.lastName = lastName;

this.height = height;

this.hometown = hometown;

this.state = state;

}

@Override

public String toString() {

String stateMessage = "";

if (state.equals("PA")) {

stateMessage = " is from Pennsylvania";

} else {

stateMessage = " is from out-of-state";

}

return firstName + " " + lastName + " from " + hometown + stateMessage + " with height " + height.toString();

}

}

public class App {

public static void main(String[] args) {

Person p1 = new Person("Jillian", "Jennings", new Height(5, 7), "Montclair", "NJ");

Person p2 = new Person("Keaton", "Ellis", new Height(5, 11));

Person p3 = new Person();

System.out.println(p1.toString());

System.out.println(p2.toString());

System.out.println(p3.toString());

}

}

image text in transcribedimage text in transcribed

Objects as Parameters In this lab you will create and use methods (that use Objects as Parameters) in the Person class. Deliverable A zipped NetBeans project with 3 classes - App - Person - Height Classes Suggestion: - Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. - Close Lab03. - Work on the new Lab04 project then. The Person Class 1. Attributes - String firstName - String lastName - Height height - 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 - height - use the Height class no-parameter constructor - hometown - N/A - state-N/A - 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 3. Methods - public String toString() - returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String. - toString0 is a special method, you will leam 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 retums all the data from each object as a String The Height class 1. Attributes 0 int feet - int inches 2. Constructors - one constructor with no input parameters - since it doesn't receive any input values, you need to use the default values below: - feet 5 - inches-6 - one constructor with two parameters - feet using the input parameter - inches using the input parameter 3. Methods - 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 The App class - create a Person object called p1 using the five-parameter constructor with the values - firstName - Jillian. - lastName - Jennings - height 57 - hometown-Montclair - state-NJ 2. create a Person object called p2 using the three-parameter constructor with the value - firstName - Keaton - lastName - Ellis - height 511 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-Jilian, lastWame-Jennings, hometown-Montclair, state-cut-of-5tate, height-Height {5}}} PersonffirstNane-Keaton, lastNane-Ellis, horetown-N/A, state-N/A, hoight-Height {511}} PorsonffirstNane-No, lastNanc-Nanc, honctown-N/A, stste-N/A, height-Height {56}}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

What are the two main problems identified with prototyping?

Answered: 1 week ago