CS 331 Spring 2013  >  Assignment 5

# CS 331 Spring 2013 Assignment 5

Assignment 5 is due at 5 p.m. Thursday, March 28. It is worth 20 points.

## Procedures

E-mail answers to the exercises below to ggchappell@alaska.edu, using the subjectPA5”.

• Your answers should consist of one file: pa5.py, from Exercise A. This file (or an archive file containing it) should be attached to your e-mail message.
• I may not read your homework e-mail immediately. If you wish to discuss the assignment (or anything else) with me, send me a separate message with a different subject line.

## Exercises (20 pts total)

### Exercise A — Basic Python

#### Purpose

In this exercise, you will write some simple Python functions and a class.

#### Instructions

Write Python 3.x code meeting the requirements outlined below. Place all of this in the file pa5.py.

The file should define the following.

• Class Time. Objects of this class represent a time of day, in hours, minutes, and seconds past midnight. The following operations should be available.
• A constructor taking 3 parameters, representing numbers of hours, minutes, and seconds. The constructed object represents a time equal to the given number of hours, minutes, and seconds past midnight.
• String conversion. Name this according to Python standards. It should return an 8-character string in the form hh:mm:ss, with the three numbers padded on the left with zeroes, as necessary. Times are represented using 24-hour time, so that hh will be from 00 to 23, and mm and ss will be from 00 to 59.

Other requirements:

• Out-of-range values should be handled properly. In particular, 24 hours past midmight is midnight on the next day, which is really 0 hours past midnight. For example, Time(30,70,-2) should return an object that prints as “07:09:58”.
• Generator function squares6. This function should be a generator that yields, one at a time, in increasing order, the sequence of all squares whose last digit, in base ten, is 6: 16, 36, 196, 256, 576, 676, 1156, 1296, .... It should take no parameters, and it should produce its values in a reasonable amount of time.
• Function foo_maybe. This function takes one parameter, x. It attempts to call x.foo(). If this is successful, it returns the return value of x.foo(). If x.foo() cannot be done, then it returns None (this is a special Python no-value value).
• Function get_331_char. This function takes 1 parameter, an integer. It downloads the source for the web page for this class, and returns the character with the given index. It should return a string (not a byte). The first character has index 0. Thus, with the page in its current form, get_331_char(0) should return “<”.

#### General Requirements

• Coding standards are as in Assignment 2.
• You may use any function, object, or type in the Python standard library. If you use something that is not a built-in, then you may import whatever portion of the library you require.
• In this assignment, there are no restrictions on the creation of additional public functions, members, etc. (This is, in part, because in Python, nothing is really “private”.)

#### Notes

• Your functions do not need to do any type checking (with the possible exception of foo_maybe, whose purpose might be considered a form of type checking).
• I have given you examples of Python functions, classes, and generators, along with code to read a web page, and code that handles exceptions. See the sample code on the web page.
• String conversion for a class is a member function named “__str__”. Once you write that, you should be able to use the built-in print capability. Like so:

[Python 3.x]

x = Time(1, 2, 3)
print(x)
• Remember that every member function gets an extra first parameter (“self”), representing the object it is called on.

#### Test Program

A test program is available: pa5_test.py. This will test whether your package works properly.

To do the testing, place the test program and your file (pa5.py) in the same directory, and run the test program (e.g., at a Unix prompt, type “python3 pa5_test.py”).

Do not turn in pa5_test.py.

CS 331 Spring 2013: Assignment 5 / Updated: 27 Mar 2013 / Glenn G. Chappell / ggchappell@alaska.edu