Why this is not working...?

Ads

 
 
 

Share on Google+Share on Google+

Jallal
Why this is not working...?
5 Answer(s)      5 years 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();

    }

}

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

Ads









Tutorials   
Java Spring Hibernate Struts Training java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile?

Ads

 
Advertisement null

Ads