import java.util.*; public class Family {
int size_of_family=0;
public Person[] members=new Person[size_of_family];
Person d = new Person();
public Family (int size_of_family){
members = new Person[size_of_family];
for ( int i = 0; i < size_of_family; ++i )
{
members[i] = new Person();
System.out.println(members[i]);
}
}
public void printOutFamily(){
for (int i = 0; i < size_of_family; ++i)
System.out.println(members[i]);
}
public void addPerson(Person p){
d = new Person();
for( int i=0; i <members.length; i++){
if(!members[i-1].equals(d)){
members[i-1]=d;
}
else{
System.out.println("This person already exist");
}
}
}
public static void main(String[] args)
{
Person fred= new Person("Fred Flintstone", 50);
System.out.println("created " + fred);
Person wilma = new Person("Wilma Flintstone", 48);
student george= new student("George Flintstone", 21, "Politics", 3.1);
System.out.println("created " + george);
student sue= new student("Sue Flintstone", 24, "Nursing", 3.3);
student anotherGeorge= new student("George Flintstone", 21, "Math", 3.4);
Person yetAnotherGeorge= new Person("George Flintstone", 50);
Family f = new Family(10);
f.addPerson(fred);
f.addPerson(wilma);
f.addPerson(george);
f.addPerson(sue);
f.addPerson(anotherGeorge);
f.addPerson(yetAnotherGeorge);
anotherGeorge.setName("Georgie Flintstone");
f.addPerson(anotherGeorge);
f.printOutFamily();
}
}
Post the code of Student.java and Person.java files.
public class Person {
private String name;
private int age;
public Person(){
name="No name";
age=0;
}
public Person(String theName,int theAge){
this.name=theName;
this.age=theAge;
}
public String getName(){
return name;
}
public void setName(String theName){
name=theName;
}
public int getAge(){
return age;
}
public void setAge(int theAge){
age=theAge;
}
public String toString(){
return( getName().toString()+" " + age);
}
public boolean equals(Person otherPerson){
return (name.equals(otherPerson.name) && age==(otherPerson.age));
}
}
public class student extends Person { private String major; private double gpa;
public student(){
super();
major="Undeclared";
gpa=0;
}
public student(String theName,int theAge,String theMajor,double theGPA){
super(theName,theAge);
this.major=theMajor;
this.gpa=theGPA;
}
public String getMajor(){
return major;
}
public void getMajor(String theMajor){
major=theMajor;
}
public double getgpa(){ return gpa; } public void setgpa(double theGPA){ gpa=theGPA; }
public String toString(){
return(getName()+" "+ getAge()+" " + major+" "+ gpa );
}
public boolean equals(student other){
return(getName().equals(other.getName()) &&getAge()==(other.getAge())&& major==(other.major) && gpa==(other.gpa));
}
}
Object class is the root of every class you create.Every class implicitly extends Object class.
for example:
class cat {
String colour;
}
the same class can also be written as ....
class cat extends Object {
String colour
}
This is the output after i run the program!!!!!!
created Fred Flintstone 50
created George Flintstone 21 Politics 3.1
No name 0
No name 0
No name 0
No name 0
No name 0
No name 0
No name 0
No name 0
No name 0
No name 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Family.addPerson(Family.java:31)
at Family.main(Family.java:57)