Share on Google+Share on Google+

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









Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads