PersonPairDirectory help.
Hey there =)
So, I've had this homework assignment for a few weeks now.. it's long past due =( and I still haven't been able to figure it out.
This is the third time I've restarted the program from scratch.
I'm not looking for the answers.. just a hint on how to complete each method inside PersonPairDirectory.java.
Here are the files you need for it:
Code:
//Person.java
public interface Person{
public boolean equalTo (Person other);
public String getName();
public int getAge();
}
Code:
//Student.java
public class Student implements Person {
String id;
String name;
int age;
public Student (String i, String n, int a) {
id = i;
name = n;
age = a;
}
protected int studyHours {
return age/2; }
public String getID() {
return id; }
public String getName() {
return name; }
public int getAge() {
return age; }
public boolean equalTo (person other) {
Student otherStudent = (Student) other;
return (id.equals (otherStudent.getID())); }
public String toString() {
return "Student(ID: " + id + ", Name: " + name + ". Age: " + age + ")";
}
}
Here is a new class that I made (one reason my second attempt failed).
I'm hoping I now have everything I need to make the PersonPairDirectory.java work.
Code:
//Pair.java
public class Pair{
Person person;
Person other;
public Pair(Person person, Person other){
this.person = person;
this.other = other; }
public Person getPerson(){
return Person; }
public Person getOther(){
return other; }
public String toString(){
return getPerson().toString() + " ....;.... " + getOther().toString();}
}
And here's the code that I need to finish the methods in.
Code:
//PersonPairDirectory
public class PersonPairDirectory{
private Pair[] directory;
privant final int MAX = 1000;
private int count;
public PersonPairDirectory(){
directory = new Pair[MAX];
count = 0;
}
public void insert (Person person, Person other){
if( count < MAX){
directory[count] = new Pair(person, other);
count++; }
else{
System.out.println("Directory full."); }
}
public Person findOther (Person person){
return null; }
public void remove (Person person, Person other){
}
}
I'm pretty sure the insert code works... my problem is figuring out what goes in findOther .. and how to remove a person.
Any help you can give me?
Also.. to print the full directory.. I would just use..
Code:
for (int i = 0; i < count; i++){
System.out.println((directory[i].toString()));}
right? and that would print out all of the crap?