Java related Lab/homework questions -


my professor asked me these 2 labs, , finished first 1 not know how second one.

  1. write person class contains following fields , methods: first name, last name, unique id number should assigned starting 1001. methods return information , tostring method returns neatly formatted string describing key attributes of person.

i finished 1 above.

public class person { private string first; private string last;  private static int idnumber = 1001; int id; private string full;  person(){     first = "john";     last = "doe";     int idnumber;     full = first +" "+ last; }  person(string fn,string ln){     first = fn;     last = ln;     id = idnumber++;     full = first +" "+ last; }       static int getidnumber(){     return idnumber; }       void setfirst(string fn) {     first = fn; }       string getfirst(){     return first; }       void setlast(string ln){     last = ln; }       string getlast(){     return last; }         @override public string tostring(){     string p1 = "first name: " +first+ "   last name:" +last+    "\tthe full name is: "+full+"  id#"+idnumber;         return p1;     } }   public class persontester { public static void main(string[] args) {     // todo code application logic here      person p = new person();     system.out.println(p);      person p1 = new person("dennis", "rodman");     system.out.println(p1);      person p2 = new person("michael", "jordan");     system.out.println(p2);      system.out.println("first name: " +p1.getfirst());         } } 
  1. write addressbook class manges collection of person objects. addressbook allow person add, delete, or search person object in address book.
    • the add method should add person object address book. make sure add method not add duplicate person objects address book.
    • the delete method should remove specified person object book.
    • the search method shearches address book specified person , returns list of persons matching specified criteria. search can done either first name, last name, or person id.

in order number 2, have learn? arrays, .length? etc? know basics, please me out.

public class addressbooktester {

/**  * @param args command line arguments  */ public static void main(string[] args) {     // todo code application logic here   }  }   import java.util.arraylist;   /*   * change template, choose tools | templates   * , open template in editor.   */  import java.util.*;   public class addressbook {    private arraylist<person> contactlist;     addressbook(){     contactlist = new arraylist<person>(); }       void add (person p){     if (!contactlist.contains(p)){         contactlist.add(p);     }    }      void add(string fn, string ln)   {         contactlist.add(new person (fn, ln));     }       void remove (person p){        contactlist.remove(p);     }     arraylist<person> search(string name)     {      ( int i=0; i< contactlist.size(); i++){      //i dont know here       }       person search (int id){         person p = null;      //same here     }  }       void printlist() {                ( int i=0; i< contactlist.size(); i++){             person p = contactlist.get(i);             system.out.println(p);             }         }  } 

okay, start with, know need addressbook class. know

class addressbook { // goes here } 

you know have 3 methods (and maybe others)

class addressbook {    public void add(person p){ /* here */ }    public void delete(person p){ /* here */ }    public person searchbyfirstname(string fn){ /* here */ ; return person;} } 

now, think how store persons. moment, take simplest thing possibly work, array

class addressbook {     private person[] p; //.... 

you need create that, somewhere you'll want p = new person[1000]; -- picked 1000 "probably big enough". there more complicated things can do, simplest now.

now, ask "how can add person array p?". fill in. "how can delete person?" fill in. (remember can put null array mark slot no longer used.) , "how can find person in array?"

finally, add class main method

    public static void main(string[] args){ /* test here */ } 

to let test code directly running class.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -