Project 3: A very social network

Due Dates

  • Sign up by Sunday, Apr. 12 at 1pm
  • Optional design checks Apr. 13–Apr. 15 (design check document due Apr. 16 at 9pm)
  • Project is due Apr. 21 at 9pm

Summary

In this project, you will implement the logic for a social networking website. Like many social networks, it is based on a graph where each node is a user and edges represent friendships. The graph is undirected and has no parallel edges: either two users are friends or they are not.

We have provided the graph implementation (including methods for adding, removing, and querying nodes and edges) and the frontend of the website. You will implement the logic that connects these two components and builds a social network from a graph!

Learning goals

This project will give you experience:

  • Writing Scala code, using data structures including Set and List
  • Using a graph data structure
  • Understanding and using someone else’s code
  • Experimenting with social network algorithms

Project tasks

  1. Form a project group according to the class’s new guidelines. If you’re having trouble finding a group, please post on Campuswire!
  2. Sign up for a design check.
  3. Read through this document in its entirety!
  4. Download the project stencil from social stencil.
  5. Complete the design check questions.
  6. Implement and test the “short” methods described below.
  7. Implement and test the “longer” methods described below.

IntelliJ setup

If we have configured the project correctly, it should be possible to open the project folder in IntelliJ, wait for IntelliJ to “Import” the project (clicking on “Enable Auto-Import” when prompted), and then interact with the project as normal. If any step of this process doesn’t work, please post on Campuswire.

You can run the project by clicking the “play” button at the top of the WebApp.scala file. Once it is running, you can go to http://localhost:8080 in your web browser and you should see the social networking website.

Project components

WebApp

The WebApp class in WebApp.scala implements the frontend of the website. You can read through this file if you are interested in how it works, but it should not be necessary to understand in detail.

The front page

The front page, at [http://localhost:8080], displays a list of the three “most popular” users on the website (the three users with the most friends). You can click on a user’s name to go to that user’s page.

The signup page

The signup page, at [http://localhost:8080/signup], lets you add a user to the social network, then takes you to that user’s page. The user will start with no friends and no hobbies.

The user page

Each user has a page–for instance, the first user’s page is at [http://localhost:8080/user/0]. This page displays a user’s hobbies, suggested hobbies, friends, and suggested friends. From here you can add or remove hobbies and friends, and can go to the user pages for the user’s friends and suggested friends.

Note that our social network doesn’t have a concept of logging in: whenever you are on a user’s page, you can make decisions for that user!

SocialGraph

The SocialGraph class in SocialGraph.scala implements a graph where nodes are users. You should read through this file and familiarize yourself with the available methods–you’ll be using them to implement your social network!

The nodes of the graph are instances of the User class. Each user has a name, a unique identifier (which is an integer), and a (mutable) set of hobbies. The addNode method creates a new user and assigns them a new unique identifier. We can then look up a user using this identifier.

SocialNetwork

The SocialNetwork class in SocialNetwork.scala is the heart of the social network, and is the class whose methods (described below) you’ll be implementing.

Project implementation

You will implement your social network by filling in the methods of the SocialNetwork class in SocialNetwork.scala. Some of these methods should be one-line methods that mostly just call the relevant graph method or directly modify a user; these are the “short” methods. Three of the methods require more work; these are the “longer” methods.

In addition to the “short” and “longer” methods described below, you may fill in the initNetwork method. This method is just used to add users, hobbies and friendships to your social network when it runs so that you don’t have to manually add them every time.

Short methods

These methods should have very short bodies: each should either call a SocialGraph method or change a user directly.

getUser

This method gets a user in the network by their identifier, then returns the user.

WebApp calls this method to display user information on user pages.

addUser

This method adds a user to the network.

WebApp calls this method when a user signs up for the social network.

addFriendship

This method records a friendship between two users.

WebApp calls this method when a user adds a friend on the user page.

removeFriendship

This method deletes a friendship between two users.

WebApp calls this method when a user deletes a friend on the user page.

addHobby

This method adds a hobby (represented as a String) to a user’s set of hobbies.

WebApp calls this method when a user adds a hobby on a user page.

removeHobby

This method deletes a hobby from a user’s set of hobbies.

WebApp calls this method when a user removes a hobby on a user page.

friends

This method returns all of a user’s friends.

WebApp calls this method to display the friends list on a user page.

Longer methods

We have listed these methods in approximate order of complexity (simplest first). We suggest this as a potential implementation order.

mostPopularUsers

This method should return the three most popular users in the network (i.e, the three users with the most friends). If there are three or fewer total users, it should return all of the users in the graph.

WebApp calls this method to get the list of popular users for the front page

suggestHobbies

This method takes a user and returns a list of suggested hobbies. The mthod should suggest hobbies that the user does not currently have but that at least two of the user’s friends have.

Implementation hint: there are multiple possible implementation strategies, but one is to build a mutable set of hobbies. You can convert a mutable set into a list with set.toList.

WebApp calls this method to get the list of suggested hobbies on each user page.

suggestFriends

This method takes a user and returns a list of suggested friends (other users). The list should include:

  • Friends of the user’s friends
  • Friends of those users (i.e., friends of friends of friends) who have at least one hobby in common with the user)
  • If the user currently has no friends, a random user in the network

The list should never include the user, or anyone the user is already friends with. This means that if the random suggested friend you get from the graph is the same as the user, you should suggest no friends.

Implementation hint: there are multiple possible implementation strategies, but one is to build a mutable set of friends. You can convert a mutable set into a list with set.toList.

Testing

You should write good tests for all of your methods in SocialNetworkSuite.scala. Be sure to test edge cases, including cases that should throw exceptions. You can get full credit for testing even if your implementation of a given method is incorrect, incomplete, or missing.

Design check

For the design check, you should submit a README.txt file with answers to the following questions:

  1. Look at the graph class. How would it need to be modified to support directed instead of undirected edges?
  2. Pick two of the three “longer” methods. For each method:
    • break it down into subproblems (as discussed in lecture)
    • For each subproblem, make a brief note about how you plan to solve it (e.g., the name of a built-in method, “a for loop”, “a variable”, etc.)

Handin

You may submit as many times as you want. Only your latest submission will be graded. This means that if you submit after the deadline, you will be using a late day – so do NOT submit after the deadline unless you plan on using late days.

The README template can be found here.

Please follow the design and clarity guide–part of your grade will be for code style and clarity. After completing the homework, you will submit:

  • README.txt
  • SocialNetwork.scala
  • SocialNetworkSuite.scala

Because all we need are the files, you do not need to submit the whole project folder. As long as the file you create has the same name as what needs to be submitted, you’re good to go!

If you are using late days, make sure to make a note of that in your README. Remember, you may only use a maximum of 3 late days per assignment. If the assignment is late (and you do NOT have anymore late days) no credit will be given.

Please don’t put your name anywhere in any of the handin files–we grade assigments anonymously!

You can follow this step-by-step guide for submitting assignments through gradescope here.