Home Answers Viewqa Java-Beginners Why this is not working...?

 
 


Jallal
Why this is not working...?
5 Answer(s)      a year and a month ago
Posted in : Java Beginners

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();

    }

}

View Answers

April 4, 2012 at 4:59 PM


Post the code of Student.java and Person.java files.


April 4, 2012 at 5:31 PM


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));

}

}


April 4, 2012 at 5:31 PM


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));


}

}


April 4, 2012 at 6:27 PM


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
    }

April 4, 2012 at 9:55 PM


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)









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.