CPSC 115L: Introduction to Computing Fall 2010

Homework 7

CPSC 115-01: due Monday, November 8
CPSC 115-02: due Tuesday, November 9

1. The class Time

First, download the file Time.py (for this, you must be on this Homework 7 webpage and right-click the link provided). This file contains a Python class named Time that represents the time of day in the 24-hour notation. Save the file Time.py in a new folder named hw7 inside your cpsc115 folder. To load the class Time from the file Time.py into DrPython's interactive session, type
>>> import os
>>> os.chdir('cpsc115/hw7')
>>> from Time import Time
1.1. Open the file Time.py and study the class Time very carefully. There are eight methods in this class. For the first two methods __init__() and __str__(), docstrings concisely explain what they do. What do the remaining six methods do? For each of these methods, insert a docstring to explain what it does.

1.2. In the class Time, implement a method named to_seconds(self) that returns the time in seconds (i.e., the total number of seconds elapsed since midnight). Your method should behave as follows. Run your method with five test cases (including the one below) and save the snapshots of your test runs in a text file named Time.out.
>>> time = Time(9, 23, 56)
>>> time.to_seconds()
33836
1.3. In the class Time, implement a method named from_seconds(self, seconds) that resets the time in the 24-hour format with the value of seconds (which represents the total number of seconds elapsed since midnight). Your method should behave as follows. Run your method with five test cases (including the one below) and save the snapshots of your test runs in the file Time.out.
>>> time = Time()
>>> time.from_seconds(33836)
>>> print time
09:23:56
1.4. In the class Time, implement a method named s_increment(self, seconds) that increments the time by the value of seconds in seconds. Your method should behave as follows. Run your method with five test cases (including the one below) and save the snapshots of your test runs in the file Time.out.
>>> time = Time(9, 23, 56)
>>> time.increment(100)
>>> print time
09:25:36
1.5. In the class Time, implement a method named difference(self, another_time) that returns a Time object that represents the time difference between the time and another_time given as a Time object. Your method should behave as follows. Run your method with five test cases (including the one below) and save the snapshots of your test runs in the file Time.out.
>>> start_time = Time(9, 23, 56)
>>> finish_time = Time(10, 54, 2)
>>> time_difference = start_time.difference(finish_time)
>>> print time_difference
01:30:06

2. What time will I get home?

Recall Exercise 4 (3) in Chapter 2 of Downey, which was assigned in Homework 2. Now, using the class Time, in the same file Time.py, implement a Python script to solve the following generalization of this exercise: Given a start time and the number of miles I will run at tempo, 7 minutes and 12 seconds per mile, determine the time I will get home. As before, assume that I will run the first and last miles at an easy pace of 8 minutes and 15 seconds per mile. Your scrpit should behave as follows.
Enter the hour of the start time: 6
Enter the minute of the start time: 52
Enter the number of miles I will run at tempo: 2
If I start at 06:52:00 and run 2 miles at tempo, then I will be home at 07:22:54.
As usual, run your script with five test cases, and save the snapshots of your test runs in the file Time.out.

What to hand in

Submit the following in paper. Be sure to put a file header at the top of each file.

Plagiarism and academic dishonesty

Please remember our course policy on plagiarism and academic dishonesty: You are encouraged to consult with one another when you work on homework assignments, but in the end everyone must do one's own work to hand in. In particular, discussion of homework assignments should be limited to brainstorming and verbally going through strategies, but it must not involve one student sharing written solutions with another student. In the end everyone must write up solutions independently. If you have discussed with classmates or used any outside source, you must clearly indicate so on your solutions and provide all references. Turning in another person's work under your name is plagiarism and qualifies as academic dishonesty. Academic dishonesty is a serious intellectual violation, and the consequences can be severe. For more details, read the Student Handbook 2010–2011, pp. 21–29.


* CPSC 115L home page
Valid HTML 4.01!