Description what the JAVA should execute: Create a new project called Family. Add a person class with name, birthDate, and sex private instance variables. Create the public getter and setter methods for each. Create an overloaded constructor that initializes all of the instance variables. Create the default constructor. Override the toString() method to show the Person's info. Create the Person in the main method, and display that person's info. Now add a new instance variable to the Person class of type Person with the identifier spouse. Spouse should be private with setter and getter methods. If you try to instantiate spouse in the contructor, you will get a stack overflow error. Each Person, creates a Person, creates a Person, and so on forever. Try that to see the error and then remove the code from the constructor.   Error melt: One or more project compiled with errors JAVA 1: package Family; public class Family { private static String steveStudent; private static Person oliver; private static Person samantha; private static Person philip; private int famArray = 0; private final Person[] family; public Family(int size_of_family) { famArray = size_of_family; family = new Person[famArray]; } public void addPerson(Person p) { boolean isPresent = false; int i; for (i = 0; i < family.length; i++) { if (family[i] != null && family[i].equals(p)) { isPresent = true; System.out.println(p.getName() + " is already present in the family"); } } if (isPresent == false) { family[i] = p; } } public void printOutFamily() { for (Person family1 : family) { System.out.println(family1.toString()); } } public static void main(String[] args) { Family f = new Family(4); Person steve = new Person("Steve White", 42); System.out.println("created " + steve); f.addPerson(steve); f.addPerson(samantha); f.addPerson(oliver); f.addPerson(philip); Student steveStudent = new Student("Steve White", 42, "Math", 3.1); System.out.println("created " + steveStudent); f.addPerson(steveStudent); Person samantha = new Person("Samantha White", 39); f.addPerson(samantha); Student oliver = new Student("Oliver", 20, "Engineering", 3.1); System.out.println("created " + oliver); f.addPerson(oliver); oliver.setName("Oliver"); f.addPerson(new Student("Oliver", 20, "Engineering", 3.1)); f.addPerson(new Student("Philip", 18, "Chemistry", 2.9)); System.out.println("****** family listing: "); f.printOutFamily(); } private void addPerson(Student oliver) { } }   JAVA 2; package Family; public class Person { private String name; private String birthDate; private String sex; private Person spouse; // Default constructor will initialize all fields to empty public Person() { this.name = ""; this.birthDate = ""; this.sex = ""; } // Overloaded constructor public Person(String name, String birthDate, String sex) { this.name = name; this.birthDate = birthDate; this.sex = sex; } // All Setters and Getters for all instance variables Person(String steve_White, int i) { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthDate() { return birthDate; } public void setBirthDate(String birthDate) { this.birthDate = birthDate; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Person getSpouse() { return spouse; } public void setSpouse(Person spouse) { this.spouse = spouse; } // toString method that will show the info of the Person @Override public String toString() { return "Name: " + name + "\n" + "Birth Date: " + birthDate + "\n" + "Sex: " + sex; } public String showSpouse() { // -- If spouse is null, return "Not Married" if (this.spouse == null) { return "Not Married"; } // -- other wise, call toString method else { return spouse.toString(); } } //  A method getMarried which is the only place to instantiate spouse //  Taking name, birthDate and sex as parameters public void getMarried(String name, String birthDate, String sex) { this.spouse = new Person(); this.spouse.setName(name); this.spouse.setBirthDate(birthDate); this.spouse.setSex(sex); } public static void main(String[] args) { // Inside main, creating a Person and display his info Person p1 = new Person("Thomas", "1999-06-17", "Male"); System.out.println("\n----- Person's info -----"); System.out.println(p1.toString()); //  As demonstrated, calling showSpouse to show that person is not married System.out.println("\n_____ Spouse's info _____"); System.out.println(p1.showSpouse()); //  Now calling the getMarried method to get marry and then display Spouse info System.out.println("\n----- After getting married ----"); System.out.println("\n_____ Spouse's info _____"); p1.getMarried("Jane", "2004-06-24", "Female"); System.out.println(p1.showSpouse()); // -- Now lets call showSpouse function } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Description what the JAVA should execute:

Create a new project called Family. Add a person class with name, birthDate, and sex private instance variables. Create the public getter and setter methods for each. Create an overloaded constructor that initializes all of the instance variables. Create the default constructor. Override the toString() method to show the Person's info. Create the Person in the main method, and display that person's info.

Now add a new instance variable to the Person class of type Person with the identifier spouse. Spouse should be private with setter and getter methods. If you try to instantiate spouse in the contructor, you will get a stack overflow error. Each Person, creates a Person, creates a Person, and so on forever. Try that to see the error and then remove the code from the constructor.

 

Error melt: One or more project compiled with errors

JAVA 1:

package Family;


public class Family {

private static String steveStudent;
private static Person oliver;
private static Person samantha;
private static Person philip;

private int famArray = 0;
private final Person[] family;

public Family(int size_of_family) {
famArray = size_of_family;
family = new Person[famArray];
}

public void addPerson(Person p) {
boolean isPresent = false;

int i;
for (i = 0; i < family.length; i++) {

if (family[i] != null && family[i].equals(p)) {
isPresent = true;
System.out.println(p.getName()
+ " is already present in the family");
}
}
if (isPresent == false) {
family[i] = p;
}
}

public void printOutFamily() {
for (Person family1 : family) {
System.out.println(family1.toString());
}
}

public static void main(String[] args) {
Family f = new Family(4);
Person steve = new Person("Steve White", 42);
System.out.println("created " + steve);
f.addPerson(steve);
f.addPerson(samantha);
f.addPerson(oliver);
f.addPerson(philip);

Student steveStudent = new Student("Steve White", 42, "Math", 3.1);
System.out.println("created " + steveStudent);
f.addPerson(steveStudent);

Person samantha = new Person("Samantha White", 39);
f.addPerson(samantha);

Student oliver = new Student("Oliver", 20, "Engineering", 3.1);
System.out.println("created " + oliver);
f.addPerson(oliver);

oliver.setName("Oliver");
f.addPerson(new Student("Oliver", 20, "Engineering", 3.1));

f.addPerson(new Student("Philip", 18, "Chemistry", 2.9));

System.out.println("****** family listing: ");
f.printOutFamily();

}

private void addPerson(Student oliver) {

}

}

 

JAVA 2;

package Family;


public class Person {

private String name;
private String birthDate;
private String sex;

private Person spouse;

// Default constructor will initialize all fields to empty
public Person() {

this.name = "";
this.birthDate = "";
this.sex = "";
}


// Overloaded constructor
public Person(String name, String birthDate, String sex) {
this.name = name;
this.birthDate = birthDate;
this.sex = sex;
}

// All Setters and Getters for all instance variables
Person(String steve_White, int i) {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getBirthDate() {
return birthDate;
}

public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public Person getSpouse() {
return spouse;
}

public void setSpouse(Person spouse) {
this.spouse = spouse;
}

// toString method that will show the info of the Person
@Override
public String toString() {
return "Name: " + name + "\n"
+ "Birth Date: " + birthDate + "\n"
+ "Sex: " + sex;
}


public String showSpouse() {
// -- If spouse is null, return "Not Married"
if (this.spouse == null) {
return "Not Married";
} // -- other wise, call toString method
else {
return spouse.toString();
}
}

//  A method getMarried which is the only place to instantiate spouse
//  Taking name, birthDate and sex as parameters
public void getMarried(String name, String birthDate, String sex) {
this.spouse = new Person();
this.spouse.setName(name);
this.spouse.setBirthDate(birthDate);
this.spouse.setSex(sex);
}

public static void main(String[] args) {

// Inside main, creating a Person and display his info
Person p1 = new Person("Thomas", "1999-06-17", "Male");
System.out.println("\n----- Person's info -----");
System.out.println(p1.toString());

//  As demonstrated, calling showSpouse to show that person is not married
System.out.println("\n_____ Spouse's info _____");
System.out.println(p1.showSpouse());

//  Now calling the getMarried method to get marry and then display Spouse info
System.out.println("\n----- After getting married ----");
System.out.println("\n_____ Spouse's info _____");
p1.getMarried("Jane", "2004-06-24", "Female");
System.out.println(p1.showSpouse()); // -- Now lets call showSpouse function
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 5 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY