Results 1 to 7 of 7
Thread: PersonPairDirectory help.
- 09-19-2008, 04:26 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
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:
Java Code://Person.java public interface Person{ public boolean equalTo (Person other); public String getName(); public int getAge(); }Java 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.
Java 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.
I'm pretty sure the insert code works... my problem is figuring out what goes in findOther .. and how to remove a person.Java 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){ } }
Any help you can give me?
Also.. to print the full directory.. I would just use..
right? and that would print out all of the crap?Java Code:for (int i = 0; i < count; i++){ System.out.println((directory[i].toString()));}
- 09-19-2008, 05:21 AM #2
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
I think I'm getting somewhere with the finding of a person..
public person findOther (person person, person find)
FOR int <- i TO i < count
if( ((pair[i].getPerson()).equalTo(person)){
find = (pair[i].getPerson());
return find; }
else {
find = ((pa[i]).getPerson());}
}
- 09-19-2008, 05:42 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Haha, not trying to spam... just updating as my progress goes along.. incase anyone is trying to help.
Here is where I'm at now with my findOther method.
What I saw right before I was posting it, that the loop will only run 1000 times, and on the 1001th try, it will fail.. so the last else won't happen, and find won't be set to null. I just need to take that ouside the loop I guess.Java Code:public Person findOther (Person person, Person find){ for( int i = 0; i < count; i++ ){ if ( ((directory[i]).getPerson()).equalTo(person)){ find = (directory[i].getOther(); } else if ( ((directory[i]).getOther()).equalTo(person)){ find = (directory[i].getPerson(); } else ( i == 1001 ){ find = null; } return find; }
- 09-19-2008, 05:52 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Another update.. I seem to be making good progress =P
It compiles and everything.Java Code:public class PersonPairDirectory{ private Pair[] directory; private 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, Person find, int fail){ for( int i = 0; i < count; i++ ){ if ( ((directory[i]).getPerson()).equalTo(person)){ find = (directory[i].getOther()); } else if ( ((directory[i]).getOther()).equalTo(person)){ find = (directory[i].getPerson()); } else{ fail = 1000;} } if ( fail == 1000 ){ find = null; } return find; } public void remove (Person person, Person other){ } }
Hopefully someone responds soon, because I still haven't a clue of where to go with removing a person.
- 09-19-2008, 06:27 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Some updates.. fixed stupid stuff.
I think I understand how to remove a pair.. wish me luck. =PJava Code:public class PersonPairDirectory{ private Pair[] directory; private 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){ Person find; for( int i = 0; i < count; i++ ){ if ( ((directory[i]).getPerson()).equalTo(person)){ find = (directory[i].getOther()); } else if ( ((directory[i]).getOther()).equalTo(person)){ find = (directory[i].getPerson()); } else{ find = null;} } return find; } public void remove (Person person, Person other){ } public int getCount(){ return count;} }
- 09-19-2008, 08:14 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Owned!
I finally got it.
I'm not sure if the findOther method works completely.. I got too tired and crammed with time I just had to move on.
Anywho.. here it is, incase anyone else happens to have this problem.
Java Code:public interface Person{ public boolean equalTo (Person other); public String getName(); public int getAge(); }
Java Code: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; } 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 + ")"; } }
Java Code: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();} }
Java Code:public class PersonPairDirectory{ private Pair[] directory; private 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){ Person find = null; for( int i = 0; i < count; i++ ){ if ( ((directory[i]).getPerson()).equalTo(person)){ find = (directory[i].getOther()); } else if ( ((directory[i]).getOther()).equalTo(person)){ find = (directory[i].getPerson()); } else{ find = null; System.out.println("Person's partner not found");} } return find; } public void remove (Person person, Person other){ int var = -1; for( int i = 0; i < count; i++){ if( (((directory[i]).getPerson()).equalTo(person)) && (((directory[i]).getOther()).equalTo(other)) ){ var = i;} } if( var == -1 ){ System.out.println("Cannot remove... persons were not found");} else if ( var != -1){ for( int i = var; i < count - 1; i++ ){ directory[i] = directory[i+1];} directory[count - 1] = null; count--; } } public int getCount(){ return count;} public void print(){ for( int i = 0; i < count; i++ ){ System.out.println((directory[i].toString()) + "\n");}} public static void main(String[] args){ PersonPairDirectory pp = new PersonPairDirectory(); Student one = new Student("A", "Bobby", 12); Student two = new Student("B", "Susan", 15); Student three = new Student("C", "Eddie", 19); Student four = new Student("D", "Keaton", 18); pp.insert(one, two); pp.insert(three, four); pp.insert(one, three); pp.print(); System.out.println("\n"); pp.remove(one, two); pp.print(); System.out.println("\n"); //pp.findOther(one); //System.out.println(find); //System.out.println("\n"); //System.out.println(count); } }
- 09-19-2008, 10:24 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What are you doing in this thread. If you have any technical question better to ask it clearly. This is not the place to describe your project for others. If you keep going this further, without any doubts to clarify I have to close the thread.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks