class logo

CS 122 - Day 16 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) Button Object

Create a button class for use with our graphics programs. It should be able to tell when a mouse is clicked in it and have a method for setting the background color. The initializer should create the class. When your class is completed, you should be able to create a new button like this:

b = Button(centerPoint, label, color)

Then, when you get a mouse click and store the point where the click happened in a variable "clickPoint", you should be able to check whether that point is in the button by doing something like this:

if b.containsClickPoint(clickPoint): # do something here

If you want to change the background color of the button, you should be able to do so like this:

b.setBackgroundColor("blue")

My solution. Note that I added my Button class directly to the program rather than creating a separate module as we did in class.

2) Menu

This assignment is a bit of a challenge, but now that you know how to create a button class, create a menu class that will display a drop-down menu of items to click on. There are many ways to structure such a class, so I don't want to limit you by suggesting how I would do it. It will just respond to mouse clicks (and your main program will need to pass those mouse click points to the menu) so it won't be a perfect match for a menu like we use in the Mac OS X operating system, but see if you can get something approximating a menu.

3) Stock Quote class

Revisit the stocks module we worked on in the exercises from day 14. How could we simplify this module using objects? One area where the module is difficult to use is that each function returns a tuple of information. Think about how you could replace this tuple with an instance of a class. Update the module and applications to use this class definition.

My solution, My new stocks2.py module

Using objects makes the module easier to write because there is a little less bookkeeping in some of the functions (in the max functions we just need to keep track of a single quote rather than three different data elements). The main program is also a little cleaner because the functions it calls in the module return just a single thing (a StockQuote object) rather than a tuple where the programmer is required to remember -- or lookup -- the order of the elements every time.

4) Stock class

When you get the class above working, look at the stocks module for another place that the interface could be simplified. One area is where we load the stock data. Right now the main program needs to have a variable that stores the list of tuples (or stock quotes). This is somewhat inelegant, update the module so that it just stores a reference to a Stock object. The Stock instance can load its own data and store it internally.

My solution, My new stocks3.py module

This enhancement makes the main program even easier to use. When a new Stock object is created, its data is loaded. The programmer just needs to use that single stock object rather than worrying about keeping track of a list of data elements.