Lab 9: Dictionaries, Object-Oriented Programming, and Creating Graphs
Goals
After the lab, you should be proficient at
- creating and testing your own classes from a specification
- creating your own class
- developing a larger program using a class to solve a problem
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
For the most recent version of the help client, run
java -jar /csdept/local/courses/cs111-01/shared/labhelp.jar
Or, run
runHelpClient.sh &
- Copy
/csdept/local/courses/cs111-01/handouts/lab9
and all of its contents (which means what command-line option should you use?) into yourcs111
directory.
Objective: Programming in Python
Your programs will be graded on correctness, style, efficiency, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
- (10) Using a dictionary object, create a program that maps a
letter to an example word that starts with that letter. You must
have at least three entries in your dictionary. Then, print out
the dictionary so that it looks similar to a children's book, and
the keys are printed in alphabetical order. Example output looks
like:
f is for fiddle g is for goose z is for zoo
- (20) The most common last names for people in the United States
are Smith, Johnson, and Williams. The most common first names for
females are Mary, Patricia, and Linda and for males are James,
John, and Robert.
(Source: Name
Statistics) In this program, you are going to determine the
most common names for W & L students.
Your program will read in a text file of names (one name per line), count how many times each name occurs in the text file, and print out the names (in alphabetical order) and the number of times each name occurs.
There are five data files in the
data
directory for you to process. Filetest.txt
is provided as an easier first file to test. The remaining four files represent W&L undergraduate's last names, first names, female first names, and male first names.Implement your code on one file. (The
lastnames.txt
file will be the easiest "real file" to check that your work is correct because the file is in alphabetical order.)You can add the following code to your code to enable some "spot checks" for correctness. Note: the code may need to be modified slightly, depending on how you named variables.
while True: name = input("What name do you want to check? ") if name in nameToCount: print(name, "occurs", nameToCount[name}, "times.") else: print(name, "was not in the data.")
Hit Control-C to exit the
while
loop.Next, to make later development a little simpler, refactor your code so that you have a function that takes the name of the file (a string) as a parameter. The function should process the file and return the generated dictionary. The
main
function should then display the (alphabetized) contents of the returned dictionary.Next, modify your
main
to call the function four times, once for each of the data files.You don't need to save the output from this program. Just look at it yourself and verify that it makes sense.
While this is useful output, we can't easily determine the name that occurs most frequently, and we can't see trends in names' frequencies.
- (25) The reason we can't get the output we want from the previous program
is because we can't tie the name and the number of occurrences together. We
want to sort by number of occurrences, but, given the number of occurrences,
we can't look up the name that has that number of occurrences. When we
want to package and encapsulate data together, that calls for a new data
type.
We will tie this class into the last program in problem 4. For now, just focus on implementing this class.
To address this issue, create a
FrequencyObject
class. The filefreqobj.py
) was provided for you when you copied thelab9
directory at the beginning of lab. The following specifies the class's attributes and methods.Data:
- a string representing the
FrequencyObject
's key - a count representing the number of times that
the
FrequencyObject
occurred
Functionality:
- constructor - doesn't return anything; initializes the
object's data, namely the key and its value
What is the method name associated with the constructor? - string representation - returns a string that has the format:
key count
What is the method name associated with this method? getKey()
- returns the FrequencyObject's keygetCount()
- returns the FrequencyObject's countupdateCount()
- increments the FrequencyObject's count by 1
Reminder of Development Process:
You may want to review the
Card
andDeck
classes.- Identify the need for a class. (Done!)
- Define the class
- Define the constructor, which intializes the attributes/data the object needs.
- Define the
__str__
method - Test the methods you implemented so far.
- Define and test the remainder of the methods (in order of above)
Testing: Write a function that tests your class's methods, similar to the function that tested the Card class's functionality. You don't need to write something like the
testRummyValue
because this class is relatively simple and doesn't require that level of testing. Your test function (likelymain
should just exercise all the methods and you can manually verify that they are correct. - a string representing the
- (25) Putting it all together.
This exercise illustrates how you can use Python to generate graphs. You will use your
FrequencyObject
class to print out the name frequency results that can be used by another Python program to generate graphs.Copy the second program for this problem. Modify the program so that the dictionary maps the key (the name) to the value (the
FrequencyObject
). When the program sees a name again, update theFrequencyObject
's count.After you're done processing the file:
- Get the
values
from the dictionary, which you should make into alist
ofFrequencyObject
s. - Sort the list by the
FrequencObject
's count, following the example in the slides. - Reverse the list so that the objects are in the order of greatest to least
- Write the list to a file, one at a time, in the following
format:
<name> <count>
For example, an output file for male first names could look like
James 12 John 9 Robert 7 ...
The data above is not the correct values for the W&L data.
- Save your output files for each input file in the
data
directory. You should name the summary files appropriately, such asfemale_fnames_freq.dat
. (You can name the files either in your program or using Linux commands.) With proper use of functions, you should be able to easily modify the program to handle all four input files at once.
- Get the
Generating Graphs
Now, we're going to use a Python program to create bar graphs of
the data you generated from your programs
using matplotlib
, which is useful for generating lots of
different kinds of graphs.
Run generateFreqGraphs.py
or
modify graphing_example.py
to generate the graphs for
each of your data files.
Save the graphs in your data
or public_html
directory so that the images
do not affect your printing later.
Example generated graph:
Objective: Creating a New Web Page
- Go into your
public_html
directory. - Copy your
lab3.html
orindex.html
file to a file calledlab9.html
(in thepublic_html
directory).
Review the copy command if necessary. - Copy the graphs you created in the last part into your
public_html
directory if they are not already there. - Modify the Lab 9 web page (using jedit) to have an appropriate, title, heading, and information.
- Modify your Lab 9 web page to display the graphs you created.
To change the size of the images you can use the
width=
attribute to make the graphs be a certain width in pixels, e.g.,width=400
- Add text labeling each graph separately.
- Note: Do not display the "old" images from the original index.html or lab5.html pages. Your page should only contain content about this week's lab.
- Modify your index.html page to link to your Lab 9 web page.
Extra Credit (up to 10 pts)
Now that we have the Card
and Deck
classes, you can
write a better game of War or a different card game. You could also update the
Deck
class to represent more than one deck of cards.
More complete and difficult games will be awarded more points.
Finishing up: What to turn in for this lab
- IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
- Copy
your
lab9
directory into yourturnin
directory. - Remove any .pyc files and any graph files (*.png) files from the directory; otherwise, you'll get an error when creating the output file. include("printing.html"); ?>
Labs are due at the beginning of Friday's class. You should
hand in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 2:20 p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 80 pts; see above for breakdown
- Graphs: 10 pts
- Web pages: 10 pts (both your index.html page and the lab9.html page)