Java - how to call an add() method in a different class -


this homework, , becoming little frustrated how can't figure out simple.

to simplify code, have 3 files right now: 1 class add() method created among other things, 1 file tests (made prof), , 1 creates object (which won't post, b/c working). here's add() function.

edit 2: i'm going add method prints array, maybe that's problem?

    public class population {       private person[] pop = new person[15];       private int numpop = 0;            public void add(person c){ // object created in class, works fine         for(int = 0; < pop.length; i++){           if(pop[i] == null) {             pop[i] = c;             numpop++;           } else {}         }   public string listpeople(){       system.out.println("population "+numpeople+" people follows:");       int = 0;       while (i<numpeople){        system.out.println("a "+pop[i].getage()+"year old person named "+pop[i].getname());         i++; //fyi methods working fine , in file.  }        return("");       } 

then, run program in test file ensure works, provided us. here's part isn't working

public class poptestprogram{ // fyi prof created this, can't change   public static void main(string[] args){      population pop = new population(15);      pop.add(new person(4, "bob"));     pop.add(new person(25, "kim"));     // adds 8 more people different ages , names     // prints people 

it compiles, when run it, puts 10 of last person array, crashes saying there problem "pop[i] = c;" line. cannot figure out need change here.

i haven't received email prof directly, thought i'd ask here.

edit: here's shows after printing out last person 10 times. showing problems other methods haven't completed yet though...

java.lang.arrayindexoutofboundsexception: -1     @ population.removeperson(population.java:49)     @ poptestprogram.main(poptestprogram.java:31)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)     @ java.lang.reflect.method.invoke(unknown source)     @ edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:272) 

in add(person), not stopping when add item, first item added put in cells in array, rest won't go in @ all, since there no more null cells in array. break loop when find empty location.

public void add(person c) {     for(int = 0; < pop.length; i++){       if(pop[i] == null) {         pop[i] = c;         numpop++;         break;       }       else {         //.....       }    } } 

could use numpop next location in list, like:

public void add(person c) {     if (numpop < pop.length) {        pop[numpop++] = c;     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -