FacePamphlet - Section Handout - Programming Methodology- 42, Exercises of Programming Methodologies

Introduction to computer science. Section Handout of Programming Methodology. FacePamphlet. Prof. Sahami. Stanford University.

Typology: Exercises

2010/2011

Uploaded on 10/10/2011

hollyb
hollyb 🇺🇸

4.8

(44)

431 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mehran Sahami Handout #42
CS 106A November 28, 2007
Assignment #7FacePamphlet
Due: 3:15pm on Friday, December 7th
Note: No late days (free or otherwise) may be used on Assignment #7
For many years, computer have been used as a ubiquitous platform for communication.
While email is perhaps still the most common medium for computer-based interaction,
social networking applications (such as Facebook, Orkut, and MySpace1) have gained
immense popularity in recent years. In this vein, your job for this assignment is to create
an application that keeps track of a simple social network.
What is a Social Network?
For those of you not already familiar with social networks, a social network, in the
simplest sense, is a means of keeping track of a set of people (each of whom have a
"profile" in the social network) and the relationships (usually involving friendship)
between them. For example, let's consider a simple social network that contains four
people's profiles: Alice, Bob, and Cathy, and Don. Say now that Alice is friends with
both Bob and Don (in which case, we consider Bob and Don to automatically be friends
of Alice, reciprocally). And Cathy is also a friend of Don. Graphically, we could draw
this "network" as:
Here, each profile in the network is represented by a circle containing the name of the
profile (more formally, such circles would be called "nodes") and a friendship
relationship between two people (which, for our purposes, is always reciprocal) is shown
as a line connecting two profiles of people who are considered friends.
The Assignment
For this assignment, you will create an application that keeps tracks of the information in
such a simple social network. More specifically, your application will allow for user
profiles to be added to, deleted from, or looked-up in the social network. Moreover, for
each profile, you will keep track of the person's name associated with that profile, an
optional image that the person may wish to display with his/her profile, an optional
"current status" for the profile (which is basically just a String indicating what activity
the owner of that profile is currently engaged in), and a list of friends for each profile.
1 Facebook, Orkut, and MySpace are trademarks of those respective social networking sites. They are
referred to here only for eduational expository reasons.
Alice
Bob
Don
Cathy
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download FacePamphlet - Section Handout - Programming Methodology- 42 and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout

CS 106A November 28, 2007

Assignment #7—FacePamphlet

Due: 3:15pm on Friday, December 7th

Note: No late days (free or otherwise) may be used on Assignment

For many years, computer have been used as a ubiquitous platform for communication.

While email is perhaps still the most common medium for computer-based interaction,

social networking applications (such as Facebook, Orkut, and MySpace^1 ) have gained

immense popularity in recent years. In this vein, your job for this assignment is to create

an application that keeps track of a simple social network.

What is a Social Network?

For those of you not already familiar with social networks, a social network, in the

simplest sense, is a means of keeping track of a set of people (each of whom have a

"profile" in the social network) and the relationships (usually involving friendship)

between them. For example, let's consider a simple social network that contains four

people's profiles: Alice, Bob, and Cathy, and Don. Say now that Alice is friends with

both Bob and Don (in which case, we consider Bob and Don to automatically be friends

of Alice, reciprocally). And Cathy is also a friend of Don. Graphically, we could draw

this "network" as:

Here, each profile in the network is represented by a circle containing the name of the

profile (more formally, such circles would be called "nodes") and a friendship

relationship between two people (which, for our purposes, is always reciprocal) is shown

as a line connecting two profiles of people who are considered friends.

The Assignment

For this assignment, you will create an application that keeps tracks of the information in

such a simple social network. More specifically, your application will allow for user

profiles to be added to, deleted from, or looked-up in the social network. Moreover, for

each profile, you will keep track of the person's name associated with that profile, an

optional image that the person may wish to display with his/her profile, an optional

"current status" for the profile (which is basically just a String indicating what activity

the owner of that profile is currently engaged in), and a list of friends for each profile.

(^1) Facebook, Orkut, and MySpace are trademarks of those respective social networking sites. They are referred to here only for eduational expository reasons.

Alice

Bob

Don

Cathy

The Program

To see how the program works, we give an example of using the program to create a

small social network. Initially, the social network starts out empty (i.e., it contains no

profiles). Below we illustrate what the application initially looks like when it is run:

Along the NORTH border of the application, is a text field entitled Name, along with three

buttons: Add, Delete, and Lookup. To create a new profile, the user would enter a name

in the Name text field and click the Add button. For example, say we entered Mehran

Sahami in the text field and clicked Add. Since there is not already a profile with the

name "Mehran Sahami" in the network, the resulting screen would look as follows:

In the screen above we see that the status text associated with the current profile has been

changed to the text "Mehran Sahami is coding like a fiend". Moreover, the Application

Message at the bottom of the display canvas has also been changed to reflect the last

action taken, namely "Status updated to coding like a fiend".

Changing Picture

We can now update the image associated with the current profile by entering the name of

a valid image file (in this case, MehranS.jpg ) in the text field associated with the Change

Picture button and pressing the Enter key (or clicking the Change Picture button).

The display updates as follows:

The image area in the current profile now displays (a scaled version of) the image from

the file MehranS.jpg and the Application Message at the bottom of the display canvas has

once again been changed to reflect the last action taken, namely "Picture updated".

Adding Friends

Now, let's add another profile for Julie Zelenski (another intrepid faculty member in the

Computer Science department) to the social network so that we can show an example of

adding a friend to a profile. In the Name text field at the top of the screen, we enter the

text Julie Zelenski and click Add. The display now shows the newly created profile

(shown on the next page). Note that although a new profile was created for Julie (which

has no image associated with it and no current status), the previous values we entered in

the text fields for status ( coding like a fiend ) and image ( MehranS.jpg ) are still there

simply because the text fields were never cleared. It's important to remember that the

values in the text fields do not reflect what is in the current profile we are looking at –

rather these fields are simply interactors that allow us to update the values in a profile,

and old values entered in these text fields need not be cleared in the program (although

this would be a simple extension to add to the program, if you were so inclined).

Since Julie likes to maintain her privacy, she may choose to neither update her image nor

her status. But, being the friendly person that she is, she chooses to add Mehran as a

friend. This is done by entering the profile name Mehran Sahami in the text field

immediately above the Add Friend button and then either clicking the button or pressing

the Enter key. After this is done, the display is updated as follows:

In the picture above, we see that Mehran Sahami has been added to the list of friends that

Julie has, and the Application Message reads "Mehran Sahami added as a friend."

Not only has Julie's profile been removed from the social network, but the profile of all

members of the social network that had Julie as a friend must also be updated to remove

Julie from their list of friends (since it is not possible to be friends with a non-existent

profile). So, if we lookup Mehran's profile again by entering Mehran Sahami in the text

field entitled Name (in the NORTH border region) and click the Lookup button, the display

will look as follows:

Note that Julie is no longer listed as one of Mehran's friends in the display above. She

was removed from Mehran's list of friends when her profile was deleted.

To verify that Julie's profile been removed from the social network, we could try to look

it up. To do this, we enter Julie Zelenski in the text field entitled Name and click the

Lookup button. The display appears as follows:

Note that when we try to lookup Julie's no-longer existent profile, the current profile that

was previously displayed is cleared and we are prompted in the Application Message that

"A profile with the name Julie Zelenski does not exist". It's important to note that when

there is no current profile being displayed (as is the case above), then the interactors in

the WEST border region have no profile to update. Thus, if we were to try to, say, change

status by entering the text sleeping in the text field and clicking the Change Status

button, the display would update as follows:

Milestone 1: Assemble the GUI interactors

As seen in the initial start-up screen of the application (shown below), there are a number

of interactors (JLabels, JTextFields, and JButtons) in both the NORTH and WEST border

regions of the application.

Similar to the NameSurfer assignment, your first milestone is simply to add the

interactors to the application window and create an implementation for the

actionPerformed method that allows you to check whether you can detect button clicks

and read what’s in the text fields. Since you've already had experience doing that in the

previous assignment, this milestone hopefully won't present many new challenges.

A few specific issues to note in the implementation of these interactors are the following:

  • All text fields are TEXT_FIELD_SIZE characters wide. TEXT_FIELD_SIZE is just a

constant set in FacePamphletConstants.

  • The Name text field in the NORTH region does not have any actionCommand

associated with it. In other words, pressing the Enter key in that text field should

have no effect, so you don't need to worry about that case.

  • The three text fields in the WEST region do have actionCommands associated with

them. The actionCommand associated with each respective text field should be the

same as its corresponding button. For example, pressing the Enter key in the text

field next to the Change Status button should have the same effect as pressing the

Change Status button.

  • If a text field is empty when its corresponding button is pressed, then nothing

should happen. For example, if the Name text field in the NORTH region has nothing

in it when the Add (or Delete, or Lookup) button is clicked (i.e., the text field's

value is the empty string ("")), then we should simply not do anything as a result of

the button click. This idea applies to all text fields in the application, and helps

prevent situations such as trying to add a profile with an empty name, or trying to

change the status of a profile to the empty string.

One issue to note is that in laying out the interactors in the WEST border region, you'll

notice that there are spaces between the various text field/button pairs (for example, there

is space between the Change Status button and the text field associated with Change

Picture). These spaces should be produced by adding a JLabel with the label text

EMPTY_LABEL_TEXT (this is just a constant defined in FacePamphletConstants) at the

appropriate points when adding interactors to the WEST border region. So, your interactor

layout code will likely include two lines at various points that look something like this:

add(new JLabel(EMPTY_LABEL_TEXT), WEST);

As you did on the previous assignment, you can take the strategy of changing the

definition of the FacePamphlet class so that it extends ConsoleProgram instead of

Program, at least for the moment. You can always change it back later. Once you have

made that change, you can then use the console to record what’s happening in terms of

the interactors to make sure that you’ve got them right. For example, we provide below a

transcript of the commands used to generate the output in Figure 1, in which the user has

just completed the following actions:

1. Entered the name Mehran in the Name text field and clicked the Add button.

2. Entered the name Julie in the Name text field and clicked the Delete button.

3. Entered the name Eric in the Name text field and clicked the Lookup button.

4. Entered the text sleeping in the Change Status text field and clicked the Change

Status button.

5. Entered the text eating in the Change Status text field and pressed the Enter key.

6. Entered the text StanfordLogo.jpg in the Change Picture text field and clicked

the Change Picture button.

7. Entered the text MehranS.jpg in the Change Picture text field and pressed the

Enter key.

8. Entered the text Julie in the Add Friend text field and clicked the Add Friend

button.

9. Entered the text Eric in the Add Friend text field and pressed the Enter key.

Figure 1. Illustration of Milestone 1

Figure 2. Starter file for the FacePamphletProfile class

/*

  • File: FacePamphletProfile.java

  • This class keeps track of all the information for one profile
  • in the FacePamphlet social network. Each profile contains a
  • name, an image (which may not always be set), a status (what
  • the person is currently doing, which may not always be set),
  • and a list of friends. */

import acm.graphics.; import java.util.;

public class FacePamphletProfile implements FacePamphletConstants {

/**

  • Constructor
  • This method takes care of any initialization needed for
  • the profile. */ public FacePamphletProfile(String name) { // You fill this in }

/** This method returns the name associated with the profile. / public String getName() { // You fill this in. Currently always returns the empty string. return ""; } /* This method returns the image associated with the profile.

  • If there is no image associated with the profile, the method
  • returns null. */ public GImage getImage() { // You fill this in. Currently always returns null. return null; }

/** This method sets the image associated with the profile. */ public void setImage(GImage image) { // You fill this in }

/** This method returns the status associated with the profile.

  • If there is no status associated with the profile, the method
  • returns the empty string (""). */ public String getStatus() { // You fill this in. Currently always returns the empty string. return ""; }

/** This method sets the status associated with the profile. */ public void setStatus(String status) { // You fill this in }

/** This method adds the named friend to this profile's list of

  • friends. It returns true if the friend's name was not already
  • in the list of friends for this profile (and the name is added
  • to the list). The method returns false if the given friend name
  • was already in the list of friends for this profile (in which
  • case, the given friend name is not added to the list of friends
  • a second time.) */ public boolean addFriend(String friend) { // You fill this in. Currently always returns true. return true; }

/** This method removes the named friend from this profile's list

  • of friends. It returns true if the friend's name was in the
  • list of friends for this profile (and the name was removed from
  • the list). The method returns false if the given friend name
  • was not in the list of friends for this profile (in which case,
  • the given friend name could not be removed.) */ public boolean removeFriend(String friend) { // You fill this in. Currently always returns false. return false; }

/** This method returns an iterator over the list of friends

  • associated with the profile. */ public Iterator getFriends() { // You fill this in. Currently always returns null. return null; }

/** This method returns a string representation of the profile.

  • This string is of the form: "name (status): list of friends",
  • where name and status are set accordingly and the list of
  • friends is a comma separated list of the names of all of the
  • friends in this profile.
  • For example, in a profile with name "Alice" whose status is
  • "coding" and who has friends Don, Chelsea, and Bob, this method
  • would return the string: "Alice (coding): Don, Chelsea, Bob" */ public String toString() { // You fill this in. Currently always returns the empty string. return ""; }

}

Figure 3. Starter file for the FacePamphletDatabase class

/*

  • File: FacePamphletDatabase.java

  • This class keeps track of the profiles of all users in the
  • FacePamphlet application. Note that profile names are case
  • sensitive, so that "ALICE" and "alice" are NOT the same name. */

import java.util.*;

public class FacePamphletDatabase implements FacePamphletConstants {

/** Constructor

  • This method takes care of any initialization needed for
  • the database. */ public FacePamphletDatabase() { // You fill this in }

/** This method adds the given profile to the database. If the

  • name associated with the profile is the same as an existing
  • name in the database, the existing profile is replaced by
  • the new profile passed in. */ public void addProfile(FacePamphletProfile profile) { // You fill this in }

/** This method returns the profile associated with the given name

  • in the database. If there is no profile in the database with
  • the given name, the method returns null. */ public FacePamphletProfile getProfile(String name) { // You fill this in. Currently always returns null. return null; }

/** This method removes the profile associated with the given name

  • from the database. It also updates the list of friends of all
  • other profiles in the database to make sure that this name is
  • removed from the list of friends of any other profile.
  • If there is no profile in the database with the given name, then
  • the database is unchanged after calling this method. */ public void deleteProfile(String name) { // You fill this in }

/**

  • This method returns true if there is a profile in the database
  • that has the given name. It returns false otherwise. */ public boolean containsProfile(String name) { // You fill this in. Currently always returns false. return false; } }

To test this part of the program, you can add code to the FacePamphlet program so that it

creates the FacePamphletDatabase and then change the code for the Add, Delete, and

Lookup button handlers as follows:

  • Entering a name in the Name text field and clicking the Add button looks up the

current name in the database to see if a profile with that name already exists. If the

name does not exist, then it adds a new profile to the database and prints out "Add:

new profile: " followed by the string version of the profile (using the toString

method of the FacePamphletProfile). If the profile name already exists in the

database, then it prints out the fact that the profile with that name already exists

followed by the string representation of the profile.

  • Entering a name in the Name text field and clicking the Delete button looks up the

current name in the database to see if it exists. If the name does exist, then it deletes

the profile with that name from the database and prints out that the profile was

deleted. If the profile name does not exist in the database, then it simply prints out

that a profile with the given name does not exist.

  • Entering a name in the Name text field and clicking the Lookup button looks up the

current name in the database to see if it exists. If the name does exist, then prints out

"Lookup: " followed by the string version of the profile. If the name does not exist,

then it prints out that a profile with the given name does not exist.

A sample run of this milestone is shown in Figure 4 (on the next page), where the user

has just completed the sequence of actions given below. (Note that your text messages

need not correspond exactly to those shown in the sample run here, as long as you can

still verify that your program is working properly.)

1. Entered the name Mehran in the Name text field and clicked the Add button.

2. Entered the name Julie in the Name text field and clicked the Add button.

3. Again, entered the name Mehran in the Name text field and clicked the Add button.

4. Entered the name Julie in the Name text field and clicked the Delete button.

5. With the name Julie still in the Name text field, clicked the Delete button again.

6. Entered the name Mehran in the Name text field and clicked the Lookup button.

7. Entered the name Julie in the Name text field and clicked the Lookup button.

Implementing Change Status

If the user enters some text in the text field associated with the Change Status button

and either presses the Change Status button or hits Enter, the application should update

as follows:

  • If there is a current profile, then the status for that profile should be updated to the

text entered, and you can just print out a message to that effect.

  • If there is no current profile, then you should simply prompt the user to select a

profile to change the status of (and there should be no changes to any of the profiles

in the database).

Implementing Change Picture

If the user enters some text in the text field associated with the Change Picture button

and either presses the Change Picture button or hits Enter, the application should

update as follows:

  • If there is a current profile, then we need to see if the we can create a GImage with

the filename of the text entered in the text field. Checking to see if a valid image

file exists can be accomplished using the code fragment below (where filename is

a String containing the name of the image file we are trying to open):

GImage image = null; try { image = new GImage(filename); } catch (ErrorException ex) { // Code that is executed if the filename cannot be opened. }

Note in the code fragment above that the variable image will still have the value

null if we were unable to open the image file with the given filename. Otherwise,

the value of the variable image will be a valid GImage object (whose value will not

be null).

If we obtained a valid GImage, then the image for the current profile should be

updated to this image, and you can print out a message to that effect (although you

won't be able to display the actual image for now).

  • If there is no current profile, then you should simply prompt the user to select a

profile to change the image of (and there should be no changes to any of the profiles

in the database).

In the starter bundle for this assignment we have provided you with an images folder that

contains a number of images (of your beloved course staff as well as a couple Stanford

logos) that you can use for this assignment. Of course, you can feel free to use your own

image files as well (as long as they are in GIF or JPG format).

Implementing Add Friend

If the user enters some text in the text field associated with the Add Friend button and

either presses the Add Friend button or hits Enter, the application should update as

follows:

  • If there is a current profile, then we need to see if the name entered in the text field

is the name of a valid profile in the database. If it is, then we try to add the named

friend to the list of friends for the current profile. If the named friend already exists

in the list of friends for the current profile, then we simply write out a message that

such a friend already exists. If that named friend does not previously exist in the list

of friends (i.e., it was successfully added to the list of friends for the current

profile), then (recalling that friendships are reciprocal) we also need to update the

profile of the named friend to add the name of the current profile to its list of

friends. For example, if the current profile was "Mehran" and we tried to add as a

friend "Julie" (which, say, is the name of valid profile in the database, which is not

already a friend of Mehran), then we should add Julie as a friend of Mehran and also

add Mehran as a friend of Julie.

  • If the name entered in the Add Friend text field is not a valid profile in the system,

we should just print out a message to that effect.

  • If there is no current profile, then you should simply prompt the user to select a

profile to add a friend to (and there should be no changes to any of the profiles in

the database).

To show one possible example of the interactions at this milestone, we show a sample run

in Figure 5 on the next page, where the user has just completed the sequence of actions

given below. (Note that your text messages don't need to correspond exactly to those

shown here, but you should be able to get the idea of what profile, if any, is the current

profile at any given time, as well as the updates that are made to it.)

1. Entered the name Mehran in the Name text field and clicked the Add button. Note

that at this point the current profile is set to Mehran's profile.

2. Entered the text sleeping in the Change Status text field and clicked the Change

Status button.

3. Entered the text MehranS.jpg in the Change Picture text field and clicked the

Change Picture button.

4. Entered the name Julie in the Name text field and clicked the Add button. Note

that at this point the current profile is set to Julie's profile.

5. Entered the name Mehran in the Add Friend text field and clicked the Add Friend

button. Note that Julie's current profile now shows Mehran as a friend.

6. Entered the name Mehran in the Name text field and clicked the Lookup button.

Note that at this point the current profile is Mehran's profile and it now shows Julie

as friend.