class logo

CS 122 - Day 14 Exercises

This page lists some problems for today's class. After today, you should be able to solve these problems on your own. Problems with this color are more challenging.

1) Modules

For our day 12 exercises we implemented a number of functions to examine a file of stock price data. Let's move those functions into a module named "stocks". You can download my solution to that exercise here. Use it as a starting point for your work today.

An important consideration when designing a module is to figure out what functionality should be included in the module and what should be left to programs that use the module. A difficult case here is to figure out whether the module should be responsible for loading the data from the files. There is no clear answer to this point, if the module does load the data then it is less portable (ie, it only works with data in a certain format) but it is easier to use. If it doesn't include data loading, then it is a pain to use, but is more reusable.

One feature that clearly shouldn't be in a module is output. Output is almost always application dependent and it should be the responsibility of the application to decide how it wants to display information.

Modify the original program so that it calls functions in a module. Modify the functions so that they return values instead of printing them out. Note: the functions may need to return a tuple of information rather than just a single value.

My solution, My stocks module

2) Another application

Create a second application that asks the user to input two different ticker symbols and compares the longest run of increasing price, reporting which stock had a longer run.

Note that you can implement this program very easily, just re-using the code in the stocks module. The main body of this program shouldn't be very long

My solution

3) Animation

Look at the Bouncing Ball and Image Display examples linked to under today's class. See if you can combine them in such a way that instead of a bouncing ball, you have a bouncing CS 122 logo.

My solution

4) Text Entry

The graphics library has a feature that allows you to get input from the user. The Entry object creates a text field. Look at the Text Field example linked to under today's class. Try to modify the program so that it implements our secret number guessing game.

My solution