Create a package called family and execute the following instructions. Create a
ID: 3685186 • Letter: C
Question
Create a package called family and execute the following instructions. Create a class called Person. Person should have (instance variables should be private): private String name; private int age; 2 Constructors: Default constructor (no parameters) Constructor which takes 2 parameters for name and age Methods: toString equals Appropriate set and get methods for setting and accessing the Person data. ---------------------------------------------------------- Create a class called Student which extends Person. Student has the private data: private String major; private double gpa; Similar to the Person class, add get and set methods. Add 2 constructors: default constructor Constructor which sets all necessary data in the Person and Student class (name, age, major and gpa) Methods: toString equals ------------------------------------------------------------ Create a Family class with the following: Constructor: Family (int size_of_family) Family contains an array of Person that is the size of "size_of_family". Family has a method called: void addPerson(Person p); The addPerson will call equals to make sure that the Person p hasn't already been added. If a duplicate is found, print out a message and continue without adding the duplicate person. void printOutFamily(); This routine calls all of the toString methods in the family. -------------------------------------------------------------- Run your Family with the following main routine: public static void main(String[] args) { Family f = new Family(8); Person fred= new Person("Fred Flintstone", 50); System.out.println("created " + fred); f.addPerson(fred); f.addPerson(fred); Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1); System.out.println("created "+ fredStudent); f.addPerson(fredStudent); Person wilma = new Person("Wilma Flintstone", 48); f.addPerson(wilma); Student george= new Student("George", 21, "Politics", 3.1); System.out.println("created " + george); f.addPerson(george); george.setName("Georgie"); f.addPerson(new Student("George", 21, "Politics", 3.1)); f.addPerson(new Student("John", 18, "Geology", 2.9)); f.addPerson(new Student("Jane", 21, "Music", 3.2)); f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0)); f.addPerson(new Student("Jim", 21, "Physics", 2.5)); f.addPerson(new Person("Robert", 18)); f.addPerson(new Person("Clemente", 32)); System.out.println("****** family listing: "); f.printOutFamily(); }
Explanation / Answer
family.java
public class family {
Person[] personInFamily;
int currPersonInFamily =0;
family ()
{
this (10);
}
family (int sizeOfFamily)
{
personInFamily = new Person [sizeOfFamily];
}
void printOutFamily()
{
for (int i=0; i<currPersonInFamily; i++)
{
System.out.println (personInFamily[i].toString());
}
}
void addPerson (Person pers)
{
if (currPersonInFamily >= personInFamily.length)
{
System.out.println ("Person not added due to size limitations " + pers.toString());
}
else
{
for (int i=0; i<currPersonInFamily; i++)
{
if (pers.equals(personInFamily[i]))
{
System.out.println ("Duplicate not added " + pers.toString());
return;
}
}
personInFamily [currPersonInFamily] = pers;
currPersonInFamily ++;
}
}
Person.java
public class Person
{
private String name;
private int age;
public Person ()
{
name = "";
age = 0;
}
public Person (String theName, int theAge)
{
if (theName ==null || theAge <=0 || theAge >= 200 )
{
System.out.println ("Fatal error creating the person.");
System.exit(0);
}
name= theName;
age = theAge;
}
public Person (Person originalObject)
{
name = originalObject.name;
age = originalObject.age;
}
public String getName ()
{
return name;
}
public int getAge ()
{
return age;
}
public void setName (String newName)
{
if (newName != null && newName != "")
name = newName;
else
{
System.out.println ("Error. Improper name value.");
}
}
public void setAge (int newAge)
{
if (newAge >0 && newAge <200)
age= newAge;
else
{
System.out.println ("Error: Improper age value. ");
System.exit(0);
}
}
public String toString ()
{
return (name + " " + age);
}
public boolean equals (Person p)
{
return (name.equals(p.name) && age ==p.age);
}
}
Student.java
public class Student extends Person {
private String major;
private double gpa;
public Student ()
{
super();
major = "";
gpa = 0;
}
public Student (String theName, int theAge, String theMajor, double theGpa)
{
super (theName, theAge);
if (theMajor == null || theGpa < 0 || theGpa >4.0)
{
System.out.println ("Fatal error creating student.");
System.exit (0);
}
else
{
major = theMajor;
gpa = theGpa;
}
}
public Student (Student originalObject)
{
super(originalObject);
major= originalObject.major;
gpa = originalObject.gpa;
}
public String getMajor ()
{
return major;
}
public double getGpa ()
{
return gpa;
}
public void setMajor (String m)
{
if (m != null)
major = m;
else
{
System.out.println ("Please enter the right major.");
System.exit (0);
}
}
public void setGpa (double g)
{
if (g>=0 && g <=4.0)
gpa =g;
else
{
System.out.println ("Please enter the right GPA.");
System.exit (0);
}
}
public String toString ()
{
return (getName() + " " + getAge() + " " + major + " " + gpa );
}
public boolean equals (Student s)
{
return major.equals(s.getMajor()) && gpa == s.gpa && super.equals(s);
}
}
main.java
public class main{
public static void main(String []args){
family f = new family(8);
Person fred= new Person("Fred Flintstone", 50);
System.out.println("created " + fred);
f.addPerson(fred);
f.addPerson(fred);
Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);
System.out.println("created "+ fredStudent);
f.addPerson(fredStudent);
Person wilma = new Person("Wilma Flintstone", 48);
f.addPerson(wilma);
Student george= new Student("George", 21, "Politics", 3.1);
System.out.println("created " + george);
f.addPerson(george);
george.setName("Georgie");
f.addPerson(new Student("George", 21, "Politics", 3.1));
f.addPerson(new Student("John", 18, "Geology", 2.9));
f.addPerson(new Student("Jane", 21, "Music", 3.2));
f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0));
f.addPerson(new Student("Jim", 21, "Physics", 2.5));
f.addPerson(new Person("Robert", 18));
f.addPerson(new Person("Clemente", 32));
System.out.println("****** family listing: ");
f.printOutFamily();
}
}
sample output
created Fred Flintstone 50
Duplicate not added Fred Flintstone 50
created Fred Flintstone 50 Math 3.1
Duplicate not added Fred Flintstone 50 Math 3.1
created George 21 Politics 3.1
Person not added due to size limitations Robert 18
Person not added due to size limitations Clemente 32
****** family listing:
Fred Flintstone 50
Wilma Flintstone 48
Georgie 21 Politics 3.1
George 21 Politics 3.1
John 18 Geology 2.9
Jane 21 Music 3.2
Tarzan 22 Gymnastics 4.0
Jim 21 Physics 2.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.