开发者

Java related Lab/homework questions

开发者 https://www.devze.com 2023-02-12 20:42 出处:网络
My professor asked me to do these 2 labs, and I finished the first one but I do not know how to do the second one.

My professor asked me to do these 2 labs, and I finished the first one but I do not know how to do the second one.

  1. Write a person class that contains the following fields and methods: First name, Last name, A unique id number that should be assigned starting from 1001. Methods to return the information and a ToString method that returns a neatly formatted string describing the key attributes of the person.

I finished the one 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 an addressbook class that manges a collection of Person objects. An addressBook will allow a person to add, d开发者_StackOverflow社区elete, or search for a Person object in the address book.
    • the add method should add a person object to the address book. Make sure the add method does not add duplicate person objects to the address book.
    • the delete method should remove the specified person object from the book.
    • the search method that shearches the address book for a specified person and returns the list of persons matching the specified criteria. The search can be done either by first name, last name, or person id.

In order to do number 2, what do I have to learn? Arrays, .length? etc? I only know the very basics, so please help me out.

public class AddressBookTester {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

 }
 }

 import java.util.ArrayList;
  /*
  * To change this template, choose Tools | Templates
  * and open the template in the 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)
    {
     for ( int i=0; i< contactList.size(); i++){
     //i dont know what to do here


    }


    Person Search (int id){
        Person p = null;
     //same here
    }

}


    void printlist() {   
            for ( int i=0; i< contactList.size(); i++){
            Person P = contactList.get(i);
            System.out.println(P);
            }
        }

}


Okay, to start with, you know you need an AddressBook class. So you know

class AddressBook { // something goes here
}

You know it will have those three methods (and maybe others)

class AddressBook {
   public void add(Person p){ /* do something here */ }
   public void delete(Person p){ /* do something here */ }
   public Person searchByFirstName(String fn){ /* do something here */ ; return person;}
}

Now, think about how to store your Persons. For the moment, take the simplest thing that could possibly work, which is probably an array

class AddressBook {
    private Person[] p; //....

You need to create that, so somewhere you'll want p = new Person[1000]; -- where I picked 1000 as "probably big enough". There are more complicated things you can do, but this is simplest for now.

Now, ask yourself "how can I add a person to my array p?". Fill that in. "How can I delete a person?" Fill that in. (Remember that you can always put a null into the array to mark that slot as no longer used.) And "How can I find a Person in my array?"

Finally, add to the class a main method

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

to let you test the code directly by simply running the class.


I would add Comparable to your people and then use an OrderSet or ArrayList to hold them in your address book. The nice thing is that using Comparable and the Collections is that Java will sort them for you and then you can just do a binary search for your target.


A really simple implementation would be using a java.util.Set<Person>, as this takes care of about everything for you. So, have a look at the collection framework in java.util.

If you want to do this from ground, yes, have a look at arrays, how to define them, how to access elements and so on.


I would use a class that wraps java.util.Map<Integer, Person> as your address book. Since the id field uniquely identifies the Person, a map is the correct data structure. Stay away from arrays since they require too much manual work. Lists and Sets don't have keys, which a Person does have: so a Map is a natural choice here.


Your professor may have told you if you should use Sets or LinkedLists to do this.

Did he mention either of those two concepts? If so I can follow this up with walking you through those again.

Arrays could also be used, but you would have to set a maximum size of the address book for right now, like Charlie Martin has set to 1000 in his p = new Person[1000];

0

精彩评论

暂无评论...
验证码 换一张
取 消