Below is the syntax highlighted version of

 Reference solution for Spring 2013 COS 126 Programming Exam 2: Person

 Author:       COS 126 Staff
 Netid:        cos126
 Precepts:     lots of them
 Dependencies: Stack, Queue, StdOut

 Description:  Models a person, a list of messages that they can
               read, and a list of their friends, so that when you
               post a message, all your friends can read it too.


public class Person { 

    private String name;
    private Queue<Person> friends; // Stack or Queue, doesn't matter
    private Stack<String> wall;    // Stack, so newest is iterated first

    // Create a new Person with this name.
    public Person(String name) { = name;
        this.friends = new Queue<Person>();
        this.wall = new Stack<String>();

    // Make these two people become friends with each other.
    // Throw an exception if you try to meet yourself.
    // We are allowed to assume we didn't meet this person yet.
    public void meet(Person otherPerson) {
        if (otherPerson == this)
            throw new RuntimeException("You can't meet yourself");

        friends.enqueue(otherPerson);      // remember this friend
        otherPerson.friends.enqueue(this); // reciprocate

    // Are these two people friends?
    // Throw an exception if you ask about knowing yourself.
    public boolean knows(Person otherPerson) {
        if (otherPerson == this)
            throw new RuntimeException("You can't call knows() on yourself");

        // Search through all my friends
        for (Person p : friends) {
            if (p == otherPerson)
                return true;

        // If otherPerson was not found, I don't know them
        return false;

    // Post a message to my list and the lists of all my friends
    public void post(String message) {
        wall.push(message);       // add to my own list

        for (Person p : friends)
            p.wall.push(message); // and to each friend's

    // Print a header, then all messages this Person can read, newest first
    public void listMessages() {
        // header
        StdOut.println("== The wall of " + name + " ==");

        // Iterate through all messages and print them
        for (String s : wall)