/* Class to represent a Person consisting of their first and last names, age and whether they are married. If they are married, we store * that Person object as their spouse */ public class Person { private String first, last; // name private boolean married; // if married, also store spouse - note that if we don't know who the spouse is, we use the 0-arg constructor to create an unknown Person as spouse private Person spouse; private int age; // Person's age public Person() // 0-arg constructor, we know nothing of this Person { first = ""; last = ""; married = false; spouse = null; age = 0; } public Person(String f, String l) // 2-arg constructor, we only know their name { first = f; last = l; married = false; spouse = null; age = 0; } public Person(String f, String l, int a) // 3-arg constructor, we will set married & spouse separately via getsMarried { first = f; last = l; married = false; spouse = null; if(a>0) age = a; else age = 0; } public void getsMarried() // mutator to change a Person to being married to an unknown Person { if(!married) // only allowed if not yet married { married = true; spouse = new Person(); // we don't know the spouse so use the 0-arg constructor } } public void getsMarried(Person s) // mutator to change a Person to married where we know the Person { if(!married) // again, only allow marriage when not yet married { married = true; spouse = s; // set this Person's spouse to s s.setMarried(); // now set Person s to being married to this Person s.setSpouse(this); } } public void getsDivorced() // upon a divorce, reset married and spouse (note that this does not adjust { // this Person's spouse's info as the spouse may not have that info available, so the other if(married) // Person must also get a divorce { married = false; spouse = null; } } public void ages(int years) // mutator for age as long as the parameter is legal { if(years>0) age+=years; } public void changeName(String f, String l) // allow a name change { first = f; last = l; } public void setMarried() { married = true; } // mutators for married and spouse public void setSpouse(Person p) { spouse = p; } public String getFirstName() { return first; } // accessors for first name, last name and full name public String getLastName() { return last; } public String getName() { return first + " " + last; } public String getSpouseName() { // if married, accessor to return this Person's spouse's name (used in toString) if(married) return spouse.getName(); else return "unmarried"; } public boolean getMaritalStatus() { return married; } // accessor for married public Person getSpouse() // accessor for spouse { return spouse; // note: returns null if not married } public int getAge() // accessor for age { return age; } public String toString() // returns this Person's first and last name and age, and if married, the spouse's name { if(!married) return first + " " + last + " is " + age + " years old and unmarried"; else return first + " " + last + " is " + age + " years old and married to " + spouse.getName(); } }