COMS S1004

Homework #5

Due June 29


Do the following questions from Schneider & Gersting Third Edition:
Chapter 7: 3, 4, 13

In this assignment, you will implement some of the features found in such social networking websites as MySpace and Facebook. This assignment requires you to think a bit more "algorithmically" than previous ones, so take time to consider your solution before you begin coding.

First, create a class called Person. This should have two attributes: a String called "name" (representing the person's name) and a String array called "friends", representing the names of the person's online friends. You should have accessor methods for both attributes, and also have a constructor that initializes them, too.

Next, create a class called FriendTracker. This class should have one attribute called "people", which is an array of Person objects. It should also have a method called "findPerson", which takes a String as its parameter and returns the Person object (from the "people" array) whose "name" attribute matches the parameter, and returns null if there is no such Person.

The FriendTracker class should also have a method called "loadFile" which takes a String as its parameter, representing the name of the file to read. Each line of the file will start with a person's name, followed by a semicolon, and then a comma-separated list of friends' names, ending with a period, like this:

Charlie: Snoopy, Linus, Lucy.

The loadFile method should read the file one line at a time, and then create a Person object (where the first name represents the "name" attribute, and the rest are the Strings in the "friends" attribute array). It should then add that Person to the "people" array in the FriendTracker class. For simplicity, you can assume that each person has no more than 10 friends, and that there are no more than 100 people in the file. An example file is here. Your program can assume that the file is correctly formatted, and that names will not contain spaces. Be sure to take into account that the String returned by the Scanner's "next" method will also include the punctuation, so you need to remove it before populating the fields in the Person object.

Note: the "findPerson" and "loadFile" methods are crucial to the functionality of the rest of the program so be sure that you get them right! Ask a member of the teaching staff if you need help!

Then, implement the following methods in the FriendTracker class:

  • "mostFriends": returns the name of the person who has the most friends
  • "friendsInCommon": takes two Strings as parameters (representing the names of two people) and returns an int representing how many friends they have in common
  • "suggestFriend": takes a String as a parameter (representing the name of a person) and returns a String representing the name of another person who is suggested as a friend (of course, the two people should not already be friends). You need to describe your algorithm in the README file; this method is considerably more complex than the previous two, so think about it carefully before you implement it

    Last, create a class called FriendProgram that has a "main" method which creates a FriendTracker object, calls its loadFile method (with the name of the file containing the lists of friends), and then calls the mostFriends, friendsInCommon, and suggestFriend methods to demonstrate that they work correctly.

    Hint: all of this functionality can be implemented using arrays, but you may want to look into the ArrayList and HashMap classes from the Java API. You do not have to use those classes, of course, but you are allowed to. You could use an ArrayList instead of a String array in the Person class to represent the names of the person's friends. And you could use a HashMap instead of an array of Person objects in the FriendTracker class to represent the "people" attribute.

    Your submission must also include a README file, in which you explain how your program works and how to use it. You need to discuss the algorithms you used to implement the findPerson, mostFriends, friendsInCommon, and suggestFriend methods.

  • Grading

    The Theory assignment will be worth a total of 15 points. Each question is worth five points.

    The Programming assignment will be worth a total of 85 points, distributed as follows:

  • Your program must compile with no errors (5 points)
  • Your program must implement the classes and the corresponding methods described above (5 points)
  • Your program must correctly implement the "findPerson" (5 points), "loadFile" (10 points), "mostFriends" (10 points), "friendsInCommon" (10 points) and "suggestFriend" (15 points) methods.
  • Your program must use good algorithmic design: no unnecessarily repeated code, no "brute force" solutions, etc. (10 points)
  • Your program should include a README file (10 points), be well-commented, use good variable names, be easy to read, etc. (5 points)
  • Submitting your assignment

    For the Theory assignment, print a paper copy and submit it at the beginning of class. You do not need to submit it electronically.

    For the Programming assignment, submit a paper copy of all your source code and submit it electronically by the due date. Put all your .java files and your README file in a .zip or .tar file, then follow these instructions:

    1. Make sure the zip/tar file name clearly indicates your UNI and the name of the assignment, e.g.
    2. Log onto CourseWorks and go to the page for COMS S1004
    3. Click on CLASS FILES in the left hand side menu
    4. Click on POST FILE in the top menu, and fill the form to submit your zip/tar file
    5. Make sure you choose Homework #5 (under "Shared Files") as the "Post file To" destination (the contents of this folder should only be visible to the teaching staff)
    6. Click the "Submit" button
    7. All done!