handout #19
CSE142—Computer Programming I
Programming Assignment #6
due: Tuesday,
optional checkpoint
due: Sunday,
Thanks to
Nick Parlante of
of this “nifty” assignment
This assignment will give you
practice with file processing in conjunction with all of the other constructs
we have learned this quarter (loops, if/else, methods, and graphics). We will be processing a file with data
obtained from the Social Security Administration. They provide a web site showing the
distribution of names chosen for children over the last 100 years in the
Every 10 years, the data gives the
1000 most popular boy and girl names for kids born in the
...
Sam 58 69 99 131 168 236 278 380 467 408 466
Samantha 0 0 0 0 0 0 272 107 26 5 7
Samara 0 0 0 0 0 0 0 0 0 0 886
Samir 0 0
0 0 0
0 0 0
920 0 798
Sammie 537 545 351 325 333 396 565 772 930 0 0
Sammy 0 887 544 299 202 262 321 395 575 639 755
Samson 0 0 0 0 0 0 0 0 0 0 915
Samuel 31 41 46 60 61 71 83 61 52 35 28
Sandi 0 0 0 0 704 864 621 695 0 0 0
Sandra 0 942 606 50 6 12 11 39 94 168 257
...
We see that “Sam” was #58 in 1900
and is slowly moving down. “Samantha” popped on the scene in 1960 and is moving
up strong to #7. “Samir” barely appears in 1980, but
by 2000 is up to #798. The database is for children born in the
Your program is to give an introduction and then prompt the user for a name to display. Then it will read through the data file searching for that name. If it finds it, it should graph the data for that name. If not, it should generate a short message indicating that the name is not found. Look at the sample log of execution at the end of this write-up. You are to exactly reproduce this format. Notice that we print an error message if the name is not found, but when the name is found, we don’t print anything. Instead we graph the data.
If the name is found, you are to construct a DrawingPanel to graph the data. Don’t construct this object until you verify that the name is in the file. If the name is not found, you shouldn’t construct a DrawingPanel at all. For the cases when the name is found, you should construct the panel and then give a series of drawing commands that produce output like the following. You are to exactly reproduce this output.
The background color is the standard white (in other words, you don’t have to set the background color). There are a series of horizontal and vertical lines drawn in black. The overall height of the panel should always be 550 pixels. The horizontal line at the top should be 25 pixels below the top and the horizontal line at the bottom should be 25 pixels above the bottom. The panel is divided into 11 sections of equal width to represent the 11 different decades for which we have data (starting with 1900 through 2000). These particular sections have a horizontal width of 70 pixels each. You should introduce class constants for the number of decades (11), the starting year (1900) and the horizontal width per decade (70). It should be possible to change these values and have your program adapt appropriately (more on this later). Notice that you must draw vertical lines to separate the 11 sections.
Notice that at the bottom of the panel you need to label each decade. To do so, you will need a new drawing command. The Graphics class includes a drawString method that takes 3 parameters: a String, an x-coordinate and a y-coordinate. The (x, y) coordinate is the position of the lower-left corner of the text. For example, the text “1900” above has coordinates (0, 550) while the text “1910” has the coordinates (70, 550). You will obviously want to write a loop to draw these various labels, especially since your program has to adapt properly if the constants are changed. At some point you will be faced with the problem of turning an int into a String (e.g., how do you turn an int value like 1900 into the String “1900”?). There are two ways to do this. You can call the method String.valueOf passing it the int and it will return a corresponding String or you can concatenate the int with an empty string as in:
"" + 1900
Then you’ll need to plot the actual data for the individual name. As noted earlier, the panel will always be 550 pixels high with the upper and lower 25 pixels not part of the plot area. That leaves exactly 500 pixels for plotting these values. The numbers range from 1 to 1000, so each pixel will represent two different rankings. Thus, a rank of 1 or 2 should be drawn at a y-coordinate of 25. A rank of 3 or 4 should be drawn at a y-coordinate of 26. A rank of 5 or 6 should be drawn at a y-coordinate of 27. And so on up to ranks of 999 and 1000 which should be drawn at a y-coordinate of 524. A rank of 0 (which means the name didn’t appear in the top 1000 at all) should be drawn at the very bottom of the plot range, at a y-coordinate of 525.
You are to draw lines connecting the different decades. In addition, just to the right of the line you are to include a String that has the name followed by a space followed by the rank. Notice, for example, that the String “Sam 58” appears to the right of the line for 1900. That is because Sam had rank 58 in 1900. The text is to appear just to the right and just above the point you are plotting. In other words, you can use the same coordinates for drawString that you use for drawLine. The lines and text for the actual plot should be drawn in red.
You should ignore case when comparing the name typed by the user with the names in the input file. The Strings that you display on the drawing panel should use the name from the input file, even if the user types the name in a different case. For example, if the user asks you to search for “SAM”, you should find it even though the input file has it as “Sam” and the drawing panel should use the input file’s “Sam” rather than what the user typed when it displays the name/rank information in the plot.
You will be using several different Scanner objects for this program. You will have one Scanner that you use to read information from the console. You will use a different Scanner to read from the input file. And because the input file is line-based, you should construct a different Scanner object for each line of the input file, as in handout #18 and as explained in section 6.3 of the book. You should write your code in such a way that you stop consuming lines of input once you find one that has the name you’re searching for.
In terms of style points, we will be grading on your appropriate use of control structures like loops and if/else statements, your ability to avoid redundancy and your ability to break down the overall problem into methods that each solve part of the overall problem. No one method should be overly long. You should be able to come up with at least four different methods other than main that each perform some nontrivial part of the problem.
You will need to have DrawingPanel.java in the same directory as your program for it to compile properly. In addition, you will need to have the file names.txt in the same directory as your program for Java to find it.
Nick suggests the following questions to explore
and recommends the original article that gave him the idea (http://www.farfilm.com/peggy/articles/wherehaveallthelisas.htm):
·
Why
is Rock popular in 1950 and Trinity in 2000?
·
Type
in your grandparents names. Names like Ethel and Mildred and Clarence sound old
fashioned – and they are! But wait long enough and the come back – Emma!
Hannah!
·
Michael
is very popular. To see the growing Spanish speaking influence in the
·
Apparently
Biblical old-testament names came back in the 1970's. A reaction to the 1960's
maybe? Try Rachel and Rebecca. The pattern seems to generalize – Sarah,
Abraham, Adam. Eve
and Moses are out of luck for some reason though.
·
Try
historical names like Sigmund or Adolf. I was
thinking Adolf would vanish in the mid 30's but it
seems to vanish 10 years before that. In any case, Adolf
is tricky, since there are a bunch of variant names like Adolph.
As noted earlier, your program has to have three constants for the number of decades, the start year and the horizontal width to use for each decade. You can make sure your program works properly by changing the number of decades to 9, the start year to 1920 and the horizontal width to 90 and changing the file name to names2.txt (which has just 9 decades worth of data). You must also store names2.txt in the same directory as your program. Below is a sample of the output for “Ethel”.
You can introduce other class constants if you
want to in addition to the three required constants.
Your program should be stored in a file called Names.java. All of
the files that you will need are stored in a file called ass6.zip on the class
web page. These files should be put in
the same folder as your program.
Checkpoint (optional)
To
encourage you to start early on the assignment, we are specifying an optional
checkpoint program. If you complete a
working checkpoint program by the checkpoint deadline, you will earn an extra
free late day that you can use on this or any future assignment. The checkpoint involves doing the file
processing but not the graphics part of the program. Your program must prompt for a name to search
for and if it finds it in the file, it should print the line of the input file with
the data for that name. It should also
print a series of lines of output, one for each decade, showing the rank for
that name in that decade. Look at the
log of execution for an example. You
must exactly reproduce this format.
To get credit for the checkpoint, your program
must ignore case when comparing names and must print the “name not found”
message if the name does not appear in the input file. Your program should be called NamesCheckpoint.java and there will be a separate turnin for it. Even though the checkpoint program will
produce extra output, be sure that your solution to Names.java
does not produce this output (it will use a DrawingPanel
instead to display the data).
Log of
execution for Checkpoint when name found (user input underlined)
name? sam
Sam 58 69 99 131 168 236 278 380 467 408 466
1900: 58
1910: 69
1920: 99
1930: 131
1940: 168
1950: 236
1960: 278
1970: 380
1980: 467
1990: 408
2000: 466
Log of
execution for Checkpoint when name not found (user input underlined)
name? butters
name not found
Log of
execution for Names when name found (user input underlined)
This program allows you to search through the
data from the Social Security Administration
to see how popular a particular name has been
since 1920.
name? ethel
Log of execution for Names when name not found (user
input underlined)
This program allows you to search through the
data from the Social Security Administration
to see how popular a particular name has been
since 1920.
name? kumar
name not found