Difference between revisions of "CSC111 Homework 11 2015"
Line 250: | Line 250: | ||
<br /> | <br /> | ||
===SuperTruck Class=== | ===SuperTruck Class=== | ||
+ | [[Image: SuperTruckDerivedFromTruck.png|right|150px]] | ||
<br /> | <br /> | ||
* The SuperTruck must be derived from the Truck class. | * The SuperTruck must be derived from the Truck class. | ||
− | * Creating a SuperTruck object, drawing it, and moving it a few pixels to the left is illustrated below. The corresponding image is shown to the right. | + | * Creating a SuperTruck object, drawing it, and moving it a few pixels to the left is illustrated below. The corresponding image is shown to the right. A SuperTruck object is constructed by giving it a reference point, which is the top-left point of the rectangle forming the body of the truck, a width, and a height for the body of the truck, and the color of the body and top of the super truck. |
− | + | The top of the SuperTruck extends from the windshield to the end of the body. | |
<br /> | <br /> | ||
::<source lang="python"> | ::<source lang="python"> |
Revision as of 21:37, 13 April 2015
--D. Thiebaut (talk) 17:20, 12 April 2015 (EDT)
<showafterdate after="20150415 12:00" before="20150601 00:00">
This Homework is due on Tuesday evening, 4/21/15, at 11:55 p.m.
Problem #1: Temperatures in the UK
Assignment
- Your assignment is to write a program that reads weather data from a text file, then processes the data, and finally outputs the answers to several questions.
- Call your program hw11_1.py
The Text Files and Their Format
- The British government has been keeping track of temperatures in several cities of the U.K. The official Web site where the data is available is http://www.metoffice.gov.uk/climate/uk/stationdata/. The records of temperatures for 37 towns/cities of the UK for the past few years, going back in some cases to 1853, are kept on this site.
- The data recorded consists of these quantities
- Mean daily maximum temperature (tmax)
- Mean daily minimum temperature (tmin)
- Days of air frost (af)
- Total rainfall (rain)
- Total sunshine duration (sun)
- (More information can be found here.)
- The format for the data is CSV.
- The data from the UK Web site have been mirrored on a Smith server: http://cs.smith.edu/~dthiebaut/UKTemperatures/. You will need to download a few files from that site to develop and test your program.
- The name of a file is simply the name of the U.K. town, all lowercase, suffixed by "data.txt."
Format of the data
- Download one of the files from http://cs.smith.edu/~dthiebaut/UKTemperatures/, say armaghdata.txt, and take a look at it. Use Notepad, TextEdit or your favorite text editor.
- You will notice that when measurements are missing, they are replaced by "---". Your program should not skip measurements that are missing.
- Sometimes, especially at the end of the file, measurements are suffixed with an asterisk (*). Your program should discard the asterisk, and treat the data as valid.
- Some lines have the word "Provisional" at the end. Your program should treat these lines the same as regular line, and should not skip them.
Input
- Your program should prompt the user for a file name. The user will supply the name of one of the text files that will have been previously downloaded from the Smith Web site listed above. In other words, your program simply needs to read a text file and does not need to access the Web.
- If the user provides an invalid file name, your program will keep on prompting the user for a new name. Your program will only exit once it has been given a valid file name, and processed its data and output the answers.
Processing
- You must use the method illustrated in the first 2 problems of Lab 11 to process the data.
- All temperatures should be reported in degrees Celsius which is the system used in the U.K.
Questions
- Question 1
- In what year or years was the coldest temperature recorded? If the coldest temperature appears several times, list all the years and month in which it will have been reported. The format of the output is illustrated in a later section.
- Question 2
- In what year or years was the warmest temperature recorded? If the warmest temperature appears several times, list all the years and month in which it will have been reported. The format of the output is illustrated in a later section.
- Question 3
- What are the 5 sunniest months and years for the given city? The format of the output is illustrated below.
Output Format
- Here is the expected output for armaghdata.txt:
> armagh.txt Invalid file name, please re-enter > armaghdata.txt 1, -4.2, 1878, 12, 1895, 2 2, 23.8, 1995, 8, 1989, 7 3, 256.0, 1940, 6, 252.9, 1949, 6, 251.6, 1935, 5, 244.1, 1957, 6, 243.8, 1989, 7
- and here is the expected output for ballpatrickdata.txt:
> ballypatrickdata.txt 1, -1.8, 1979, 1 2, 20.0, 1995, 8 3, 279.3, 1975, 5, 272.6, 1976, 8, 253.7, 1977, 5, 247.2, 1984, 5, 245.6, 1974, 4
- Note that each line is prefixed with a number, identifying the question for which the line is the answer.
- The order in which the years of min or max temperature are listed is unimportant.
- The sunniest months are listed in order of decreasing exposure.
- Note, also, that the output is in CSV form. A coma separates all the values. No extra spaces should appear in front of comas.
Moodle Submission
- Submit your program in the Moodle HW11 PB1 section.
Problem 2: Class Inheritance
Preparation
- Create a file called car.py, containing the following code:
# car.py # CSC111 # This file contains the definition of a Wheel and a Car # The car is a convertible, with a rectangular body, # a windshield, and two wheels. from graphics import * #---------------------------------------------------------------- class Wheel: """A class with two concentric circles""" def __init__( self, center, r1, r2 ): self.circ1 = Circle( center, r1 ) self.circ2 = Circle( center, r2 ) r1, r2 = min( r1, r2 ), max( r1, r2 ) self.radius1 = r1 self.radius2 = r2 def draw( self, win ): self.circ2.draw( win ) self.circ1.draw( win ) def setFill( self, color1, color2 ): self.circ1.setFill( color1 ) self.circ2.setFill( color2 ) def getRadius1( self ): return self.radius1 def getRadius2( self ): return self.radius2 def move( self, dx, dy ): self.circ1.move( dx, dy ) self.circ2.move( dx, dy ) #---------------------------------------------------------------- class Car: """a class containing a rectangle and 2 wheels""" def __init__( self, P1, P2 ): """constructs the car. The top-left and bottom-right points defining the body of the car are given.""" #self.P1 = P1 #self.P2 = P2 self.width = abs( P1.getX()-P2.getX() ) self.height= abs( P1.getY()-P2.getY() ) #--- define rectangle--- self.body = Rectangle( P1, P2 ) #--- add a triangle for windshield --- P3 = Point( P1.getX()+self.width//5, P1.getY() ) P4 = Point( P1.getX()+self.width//3, P1.getY() ) P5 = Point( P4.getX(), P1.getY()-self.height//2 ) self.windshield = Polygon( P3, P4, P5 ) self.windshield.setFill( "lightblue" ) #--- and the two wheels --- center1 = Point( P1.getX()+self.width/8, P2.getY() ) center2 = Point( P1.getX()+self.width*7/8, P2.getY() ) radius2 = self.height/3 radius1 = radius2/2 self.wheel1 = Wheel( center1, radius1, radius2 ) self.wheel2 = Wheel( center2, radius1, radius2 ) def draw( self, win ): """draw rectangle 2 wheels on window""" self.body.draw( win ) self.windshield.draw( win ) self.wheel1.draw( win ) self.wheel2.draw( win ) def setFill( self, color1, color2, color3 ): """defines the color of the car. First is body, then inside wheel, then tire color""" self.body.setFill( color1 ) self.wheel1.setFill( color2, color3 ) self.wheel2.setFill( color2, color3 ) def move( self, dx, dy ): """defines direction of movement for all 3 elements of car""" self.body.move( dx, dy ) self.windshield.move( dx, dy ) self.wheel1.move( dx, dy ) self.wheel2.move( dx, dy )
- Create a new program called hw11_2.py with the code below:
# hw11_2.py # your name here # # This program draws a car on the graphics window # and moves it a few pixels. from graphics import * # import all the graphic elements (Rectangles, etc.) from car import * # import the Wheel and Car classes from car.py WIDTH = 600 HEIGHT= 600 def main(): global W, H win = GraphWin( "Put your name here", WIDTH, HEIGHT ) # create a car car = Car( Point( 100,100 ), Point( 250, 170 ) ) # set the color of its different parts and draw it car.setFill( "red", "yellow", "black" ) car.draw( win ) # move it for i in range( 100 ): car.move( -2, 0 ) win.getMouse() if __name__=="__main__": main()
Assignment
Your assignment is to create two new classes inside the hw11_2.py program. The first class will be called Truck, and is derived from Car. The second class is called SuperTruck, and is derived from Truck.
Truck Class
- The Truck must be derived from the Car class.
- The constructor for the Truck is given two points and three colors. Creating a truck, drawing it, and moving it a few pixels is illustrated below. The corresponding truck is shown on the right hand side.
truck = Truck( Point( 300, 200 ), Point( 400, 250 ), "red", "grey", "blue" ) truck.draw( win ) for i in range( 20 ): truck.move( -2, 0 )
SuperTruck Class
- The SuperTruck must be derived from the Truck class.
- Creating a SuperTruck object, drawing it, and moving it a few pixels to the left is illustrated below. The corresponding image is shown to the right. A SuperTruck object is constructed by giving it a reference point, which is the top-left point of the rectangle forming the body of the truck, a width, and a height for the body of the truck, and the color of the body and top of the super truck.
The top of the SuperTruck extends from the windshield to the end of the body.
sTruck = SuperTruck( Point( 450, 300 ), 100, 75, "yellow" ) sTruck.draw( win ) for i in range( 20 ): sTruck.move( -2, 0 )
Submission
- Submit a copy of the graphics window showing a car, a truck and a super-truck. Look for the HW11 Image section on Moodle. Assuming that we use the code above to create them, the image created by your program should be as close to the one below as possible (except for the text in grey, of course):
- Submit a copy of your program hw11_2.py in the HW11 PB2 section on Moodle.
</showafterdate>