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
}
}