Why this is not working...?

Why this is not working...?

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 Tutorials/Questions & Answers:
Why spring?
Why NSMutableDictionary
Advertisements
Why request.getParameter(
why this can't
Why this is not working...?
why jre in jdk
why the php is open source?
Why it called Struts?
why using static keyword
Why is super.super.method(); not allowed in Java?
why not float main?
why not float main?
Why Ajax for web?
Why "<![CDATA[ ]]>" CDATA tag.
Java is an opensource program or not? why???
ModuleNotFoundError: No module named 'why'
why get method is used?
why php and mysql - PHP
why crieterias are importent in hibernate?
Why Abstract Class?
Why Abstract Class?
why java is securable ?
Why Generics was introduced in Java?
Why Python is useful for AI?
Why is data science in demand?
Why is Big Data bad?
Why is Python so hard?
Why is Python popular?
Why is everyone Machine Learning?
Why data science?
Why NSString Objective C
why servlet as controller - Struts
Why is this code working
Why bufferedreader is used in java?
Why application server is important?
why it is throwing classCastException.
why cant i close this ??
Why hibernate is used?
Why XML?, Why XML is used for?
Why are my variables not dividing properly?
Why is it a bad practice to call System.gc?
why the program is showing error?
Multi Threading is not working in that why...?
why to use hibernet as a dataacces layer
why php is open source - PHP
ModuleNotFoundError: No module named 'why_nester'
ModuleNotFoundError: No module named 'why_python_v1'
ModuleNotFoundError: No module named 'why_nester'
ModuleNotFoundError: No module named 'why_python_v1'
Why PHP ?

Ads