The two linked lists are linked object lists type Person and type Friend. So we have a friends package consisting of three files (Friend.java Person.java PersonList.java) and a driver.
I'm pretty sure the problem lies in one of the first two methods of PersonList. Here is what I have for that one
package friends;
public class PersonList {
Person firstPerson;
private Person Person;
public PersonList( ) {
firstPerson = Person;
}
public Person lookup(String name){
Person ptr;
for (ptr = firstPerson; ptr.nextPerson == null; ptr = ptr.nextPerson) {
if (ptr.name == null) {
ptr.name = name;
return ptr;
}
else if (ptr.name == name) {
return ptr;
}
}
ptr.nextPerson.name = name;
return ptr;
}
void addFriend(Person who, String friendName){
if (firstPerson == null)
return;
Person ptr = firstPerson;
while (ptr != who) {
ptr = ptr.nextPerson;
}
if (ptr == who)
{
if (ptr.firstFriend.who == null)
ptr.firstFriend.who.name = friendName;
Friend ptr2 = ptr.firstFriend;
while (ptr2.nextFriend != null) {
ptr2 = ptr2.nextFriend;
}
ptr2.nextFriend.who.name = friendName;
}
}
}
Person.java:
package friends;
public class Person {
String name;
Friend firstFriend;
Person nextPerson;
public Person(String name, Person nextPerson) {
this.name = name;
this.nextPerson = nextPerson;
this.firstFriend = null;
}
public String friendString(){
String friends = "Friends: ";
while (this.firstFriend != null) {
friends += this.firstFriend;
}
return friends;
}
public void addFriend(Person friend){
// fill in here
}
public void removeFriend(Person friend){
// fill in here
}
}
Friend.java
package friends;
public class Friend {
Friend nextFriend;
Person who;
Friend(Person who, Friend nextFriend) {
this.who = who;
this.nextFriend = nextFriend;
}
void addFriend(Person newWho){
// fill in here
}
Friend findBefore(Person who){
return null;
}
void removeFriend(Person who){
// fill in here
}
}
Here is the driver which I'm pretty sure we cannot modify:
package driver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import friends.PersonList;
import friends.Person;
public class DriveFriends {
public static void main(String [] args)
throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char cmd=' ';
PersonList people = new PersonList();
while (cmd != 'q'){
System.out.println("type: for:");
System.out.println(" q quit");
System.out.println(" a add friend");
System.out.println(" d delete friend");
System.out.println(" p print friend list");
Person who;
String line = br.readLine();
if (line.length() == 0){
cmd = ' ';
} else {
cmd = line.charAt(0);
}
switch (cmd){
case 'a': who = readPerson("person", people, br);
who.addFriend(readPerson("friend", people, br));
break;
case 'd': who = readPerson("person", people, br);
who.removeFriend(readPerson("friend", people, br));
break;
case 'p': who = readPerson("person", people, br);
System.out.println(who.friendString());
break;
case 'q': System.out.println("Bye.");
break;
default: System.out.println("Bad option.");
}
}
}
static Person readPerson(String kind, PersonList people, BufferedReader br)
throws IOException {
System.out.println("type "+ kind + "'s name: ");
Person who = people.lookup(br.readLine());
return who;
}
}
Any ideas?

New Topic/Question
Reply




MultiQuote








|