Assignment 6: NameSurfer - Stanford University
CS106A Winter 2011-2012
Handout #27 February 29, 2011
Assignment 6: NameSurfer
_________________________________________________________________________________________________________ Assignment by Nick Parlante, with revisions by Patrick Young and Eric Roberts
At this point, you now possess all the tools necessary to build an impressive piece of software to explore interesting data sets. You've seen file I/O. You've seen arrays, ArrayLists, and HashMaps. You've seen interactors. It's now time to pull your talents together to build a program to visualize important trends in American society: The NameSurfer, a program that displays the popularity of baby names over the course of the past century. In doing so, you'll hone your programming skills, learn more about object-oriented design, and will end up with software that can take data and make it come to life.
Due Wednesday, March 7 at 3:15PM
Overview of the NameSurfer project
Against all bureaucratic stereotypes, the Social Security Administration provides a neat web site
showing the distribution of names chosen for children over the last 100 years in the United States
(). The Social Security Administration provides data that
shows the 1000 most popular boy and girl names for children at 10 year intervals. The data can be
boiled down to a single text file that looks something like this:
NamesData.txt
. . .
Sam 58 69 99 131 168 236 278 380 467 408 466 997
Samantha 0 0 0 0 0 0 272 107 26 5 7 63
Samara 0 0 0 0 0 0 0 0 0 0 886 0
Samir 0 0 0 0 0 0 0 0 920 0 798 0
Sammie 537 545 351 325 333 396 565 772 930 0 0 0
Sammy 0 887 544 299 202 262 321 395 575 639 755 0
Samson 0 0 0 0 0 0 0 0 0 0 915 0
Samuel 31 41 46 60 61 71 83 61 52 35 28 32
Sandi 0 0 0 0 704 864 621 695 0 0 0 0
Sandra 0 942 606 50 6 12 11 39 94 168 257 962
. . .
Each line of the file begins with the name, followed by the rank of that name in each of the 11 decades since 1900, counting the current one: 1900, 1910, 1920, and so on up to 2010. A rank of 1 indicates the most popular name that year, while a rank of 997 indicates a name that is not very popular. A 0 entry means the name did not appear in the top 1000 names for that year and therefore indicates a name that is even less popular. The elements on each line are separated from each other by a single space. The lines happen to be in alphabetical order, but nothing in the assignment depends on that fact.
As you can see from the small excerpt from the file, the name Sam was #58 in the first decade of the 1900s and has since dropped to the bottom of the Top 1000 list. Samantha popped on the scene in the 1960s (possibly because the show Bewitched, which had a main character named Samantha, ran on television during those years) and was extremely popular ten years ago, but is now just very popular. Samir didn't even appear on the Top 1000 list until the 1980's. The database counts children born in the United States, so trends in particular names tend to reflect the evolution of ethnic communities over the years.
- 1 -
Figure 1. Sample run of the NameSurfer program (with names "Sam" and "Samantha")
The goal of this assignment is to create a program that graphs these names over time, as shown in the sample run in Figure 1. In this diagram, the user has just typed Samantha into the box marked "Name" and then clicked on the "Graph" button, having earlier done exactly the same thing for the name Sam. Whenever the user enters a name, the NameSurfer program creates a new plot line showing how that name has fared over the decades. Clicking on the "Clear" button removes all the plot lines from the graph so that the user can enter more names without all the old names cluttering up the display. Note that the program is not case sensitive to the names that the user types into the "Name" textbox. So, the user could enter "Samantha", "SAMANTHA", "samantha" or even "sAmAnThA" and the program would display the graph for the name "Samantha". To give you more experience working with classes that interact with one another, the NameSurfer application as a whole is broken down into several class files, as follows:
? NameSurfer--This is the main program class that ties together the application. It is responsible for creating the other objects and for responding to the buttons at the bottom of the window, but only to the point of redirecting those events to the objects represented by the other classes.
? NameSurferConstants--This interface is provided for you and defines a set of constants that you can use in the rest of the program. Any class that implements the NameSurferConstants interface will automatically have access to these constants, as you can see from the provided starter files. The NameSurferConstants interface therefore has the same role as the YahtzeeConstants interface that you saw in in Assignment #5.
? NameSurferEntry--This class ties together all the information for a particular name. Given a NameSurferEntry object, you can find out what name it corresponds to and what its popularity rank was in each decade.
? NameSurferDataBase--This class keeps track of all the information stored in the data files, but is completely separate from the user interface. It is responsible for reading in the data and for locating the data associated with a particular name.
- 2 -
? NameSurferGraph--This class is a subclass of GCanvas that displays the graph of the various names by arranging the appropriate GLine and GLabel objects on the screen, just as with the various graphical programs you've written this quarter.
Even though the class structure sounds complicated, the NameSurfer application code is about the same size as Yahtzee. Even if the scale of the project is comparable to the last assignment, the wise course is to start on the assignment soon and keep up with the milestones described in this handout.
Figure 2. Structure of the NameSurfer Assignment
NameSurferDataBase Loads and manages NameSurferEntrys
Asks for NameSurferEntrys
NameSurferEntry
NameSurferEntry NameSurferEntry
Returns NameSurferEntrys
NameSurfer
Receives user input, reads from the
database, and tells the graph what to display.
Hands over NameSurferEntrys that need to be displayed.
NameSurferGraph
Graphs NameSurferEntrys and keeps track of which entries need
to be display.
Milestone 1: Assemble the GUI interactors
If you look at the bottom of Figure 1, you will see that the region along the NORTH edge of the window contains several interactors: a JLabel, a JTextField, and two JButtons. Since putting up interactors is something you haven't done in previous assginments, you probably want to work on this step before it becomes complicated with all the other parts of the assignment. Thus, your first milestone is simply to add the interactors to the 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 field.
The simplest strategy to check whether your program is working is to change the definition of the NameSurfer 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, Figure 3 shows a possible transcript of the commands used to generate the output from Figure 1, in which the user has just completed the following actions:
1. Entered the name Sam in the text field and clicked the Graph button.
2. Entered the name Samantha in the text field and then typed the ENTER key.
3. Clicked the Clear button.
The hard part about reaching this milestone is understanding how interactors work. Once you do, writing the code is quite straightforward ? it's only 10 to 15 lines of code.
- 3 -
Figure 3. Illustration of Milestone 1
Milestone 2: Implement the NameSurferEntry class The starter file for the NameSurferEntry class appears in full as Figure 4 on the following page. As with the other files supplied with this assignment, the starter file includes definitions for all of the public methods we expect you to define. The method definitions in the starter files, however, do nothing useful, although they occasionally include a return statement that gives back a default value of the required type. In Figure 4, for example, the getRank method always returns 0 to satisfy the requirement that the method returns an int as defined in its header line. Methods that will eventually become part of the program structure but that are temporarily unimplemented are called stubs. Stubs play a very important role in program development because they allow you to set out the structure of a program even before you write most of the code. As you implement the program, you can go through the code and replace stubs with real code as you need it. The NameSurferEntry class encapsulates the information pertaining to one name in the database. That information consists of two parts:
1. The name itself, such as "Sam" or "Samantha" 2. A list of 12 values indicating the rank of that name in each of the decades from 1900 to 2010,
inclusive. The class definition begins with a constructor that creates an entry from the line of data that appears in the NamesData.txt file. For example, the entry for Sam looks like this:
Sam 58 69 99 131 168 236 278 380 467 408 466 997
The idea behind the design of this constructor is that it should be possible to read a line of data from the file and then create a new entry for it using code that looks like this:
String line = rd.readLine(); NameSurferEntry entry = new NameSurferEntry(line);
The implementation of the constructor has to divide up the line at the spaces, convert the digit strings to integers (using Integer.parseInt), and then store all of this information as the private state of the object in such a way that it is easy for the getName and getRank methods to return the appropriate values. To split the line apart at the spaces, you may want to look into the String.split method, which can be used to split the initial string into a String[] of smaller pieces. You may also want to look at the StringTokenizer class, which can also be used to split the string apart.
- 4 -
Figure 4. Starter file for the NameSurferEntry class
/* * File: NameSurferEntry.java * -------------------------* This class represents a single entry in the database. Each * NameSurferEntry contains a name and a list giving the popularity * of that name for each decade stretching back to 1900. */
import acm.util.*; import java.util.*;
public class NameSurferEntry implements NameSurferConstants {
/* Constructor: NameSurferEntry(line) */ /**
* Creates a new NameSurferEntry from a data line as it appears * in the data file. Each line begins with the name, which is * followed by integers giving the rank of that name for each * decade. */ public NameSurferEntry(String line) {
// You fill this in // }
/* Method: getName() */ /**
* Returns the name associated with this entry. */ public String getName() {
// You need to turn this stub into a real implementation // return null; }
/* Method: getRank(decade) */ /**
* Returns the rank associated with an entry for a particular * decade. The decade value is an integer indicating how many * decades have passed since the first year in the database, * which is given by the constant START_DECADE. If a name does * not appear in a decade, the rank value is 0. */ public int getRank(int decade) {
// You need to turn this stub into a real implementation // return 0; }
/* Method: toString() */ /**
* Returns a string that makes it easy to see the value of a * NameSurferEntry. */ public String toString() {
// You need to turn this stub into a real implementation // return ""; } }
- 5 -
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- since 2011 charlotte has taken the lead in girls names
- this is jeopardy university of california berkeley
- african congolese names or zairian names
- baby names
- assignment 6 namesurfer stanford university
- name ideas for your older adult amazon s3
- names nombres by julia alvarez
- the book of african names duke university libraries
Related searches
- stanford university philosophy department
- stanford university plato
- stanford university encyclopedia of philosophy
- stanford university philosophy encyclopedia
- stanford university philosophy
- stanford university ein number
- stanford university master computer science
- stanford university graduate programs
- stanford university computer science ms
- stanford university phd programs
- stanford university phd in education
- stanford university online doctoral programs