Difference between revisions of "CSC111 Homework 11 2011"

From dftwiki3
Jump to: navigation, search
(Submission)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 17:50, 6 December 2011 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] 17:50, 6 December 2011 (EST)
 
----
 
----
 +
{|
 +
|
 +
__TOC__
 +
|-
 +
|
 
<bluebox>
 
<bluebox>
 
This is a make-up homework.  You can use it to bring back the current lowest grade on Hw 1 to 7, and 9 to 10.  Homework 8 can only be brought up by doing Make-up Homework 8.
 
This is a make-up homework.  You can use it to bring back the current lowest grade on Hw 1 to 7, and 9 to 10.  Homework 8 can only be brought up by doing Make-up Homework 8.
Line 10: Line 15:
 
This homework is due on Tuesday 12/13/11, at midnight.
 
This homework is due on Tuesday 12/13/11, at midnight.
 
<br />
 
<br />
As the syllabus says, once all the homework assignments have been graded, the lowest one will be dropped automatically.  In other words, only the top 9 grades for homework assignments will count toward the final grade.
+
As specified in the syllabus, once all the homework assignments have been graded, the lowest one will be dropped automatically.  In other words, only the top 9 grades for homework assignments will count toward the final grade.
 
</bluebox>
 
</bluebox>
 
+
|}
=Traffic Light Problem: Part 1=
+
=Traffic Light: Part 1=
 
[[Image:TrafficLight.jpg|150px|right]]
 
[[Image:TrafficLight.jpg|150px|right]]
 
   
 
   
* Create a class for a traffic light that can be drawn on the graphics window, with the bus/car and the trees.
+
* Create a class for a traffic light that can be drawn on the graphics window, along with the bus/car and the trees.
* The traffic light is simply a tall long rectangle with three circles inside, one which is either black or yellow, one that is either black or green, and one that is either black or red.
+
* The traffic light is simply a tall long rectangle with three circles inside, one that is either black or yellow, one that is either black or green, and one that is either black or red.
* Your class should contain methods that should allow objects instantiated from it to
+
* Your class should contain methods that should allow one  to:
** Turn the green light on.  This will set the other ones off (i.e. black)
+
** Turn the green light on.  This will automatically set the other ones off (i.e. black)
** Turn the yellow light on.  This will set the other ones off (i.e. black)
+
** Turn the yellow light on.  This will automatically  set the other ones off (i.e. black)
** Turn the red light on.  This will set the other ones off (i.e. black)
+
** Turn the red light on.  This will automatically  set the other ones off (i.e. black)
* The default mode is for TrafficLight objects to be created with the green light '''on'''.
+
* Add any other methods you feel are necessary for your program to work well.
 +
* By  default TrafficLight objects are created with the green light '''on'''.
  
 
* Test your program and make sure you can draw a traffic light and control it in some ways.  Below is an example of how you could test this:
 
* Test your program and make sure you can draw a traffic light and control it in some ways.  Below is an example of how you could test this:
Line 54: Line 60:
 
=Part 2=
 
=Part 2=
  
* Same as Part 1, but the '''TrafficLight''' object will now switch on its own from green to yellow, from yellow to red, and from red back to green.
+
* This is the continuation of Part 1: now the '''TrafficLight''' object will switch on its own from green to yellow, from yellow to red, and from red back to green.
  
* The way to do that is for the traffic light class to contain a counter, and every time it is called to update itself, it increments a counter.  If the counter passes a particular threshold, say 10, it decide to turn on anther light.
+
* The way to implement this is for the traffic light class to contain a counter, and every time the traffic light is called to update itself, it increments a counter.  If the counter passes a particular ''threshold'', say 10, it decides to turn on another light.
* There should be a car (or bus) going around the window, wrapping around the window as it leaves one edge and reappearing on the other side.
+
* '''The main program should not be the place where the traffic light is told to change light!'''  This should be done inside the TrafficLight class, in a method that is called every time through the animation loop.
* There should be trees around as well.
+
* There should be a car (or bus) going around the window, wrapping around the window as it leaves one edge and reappears on the other side.
* The car should stop if it is within 50 pixels in front of the traffic light and the light is red or yellow.  Otherwise the car moves on...
+
* There should be trees around as well.  The trees do not move.
 +
* The car should stop if it is within 50 pixels in front of the traffic light and the light is red or yellow.  Otherwise the car moves through the light.
 
* Here is an example below of what your main program could look like:
 
* Here is an example below of what your main program could look like:
  
Line 89: Line 96:
  
 
=Submission=
 
=Submission=
* Submit your class as follows:
+
* Submit your program files as follows:
  
 
   rsubmit hw11 hw11.py
 
   rsubmit hw11 hw11.py
Line 95: Line 102:
  
 
* or use the submit form at http://cs.smith.edu/~111a/submit11.htm
 
* or use the submit form at http://cs.smith.edu/~111a/submit11.htm
 +
 +
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Latest revision as of 09:00, 8 December 2011

--D. Thiebaut 17:50, 6 December 2011 (EST)


This is a make-up homework. You can use it to bring back the current lowest grade on Hw 1 to 7, and 9 to 10. Homework 8 can only be brought up by doing Make-up Homework 8.
You can work on this homework in pair mode.
This homework is optional. You will not be penalized if you don't turn it in.
This homework is due on Tuesday 12/13/11, at midnight.
As specified in the syllabus, once all the homework assignments have been graded, the lowest one will be dropped automatically. In other words, only the top 9 grades for homework assignments will count toward the final grade.

Traffic Light: Part 1

TrafficLight.jpg
  • Create a class for a traffic light that can be drawn on the graphics window, along with the bus/car and the trees.
  • The traffic light is simply a tall long rectangle with three circles inside, one that is either black or yellow, one that is either black or green, and one that is either black or red.
  • Your class should contain methods that should allow one to:
    • Turn the green light on. This will automatically set the other ones off (i.e. black)
    • Turn the yellow light on. This will automatically set the other ones off (i.e. black)
    • Turn the red light on. This will automatically set the other ones off (i.e. black)
  • Add any other methods you feel are necessary for your program to work well.
  • By default TrafficLight objects are created with the green light on.
  • Test your program and make sure you can draw a traffic light and control it in some ways. Below is an example of how you could test this:


def main():
      win = GraphWin( "Hw 11 - 111b-xx", W, H )
      x = ...
      y = ...
      light = TrafficLight( Point( x, y ) )
      light.draw( win )

      click = 0
      while  True:
                   
          if win.checkMouse() != None:
               click = ( click + 1 ) % 3
               if click == 0:  light.setGreenOn()
               if click == 1:  light.setYellowOn()
               if click == 2:  light.setRedOn()
    
      win.close()

main()


Part 2

  • This is the continuation of Part 1: now the TrafficLight object will switch on its own from green to yellow, from yellow to red, and from red back to green.
  • The way to implement this is for the traffic light class to contain a counter, and every time the traffic light is called to update itself, it increments a counter. If the counter passes a particular threshold, say 10, it decides to turn on another light.
  • The main program should not be the place where the traffic light is told to change light! This should be done inside the TrafficLight class, in a method that is called every time through the animation loop.
  • There should be a car (or bus) going around the window, wrapping around the window as it leaves one edge and reappears on the other side.
  • There should be trees around as well. The trees do not move.
  • The car should stop if it is within 50 pixels in front of the traffic light and the light is red or yellow. Otherwise the car moves through the light.
  • Here is an example below of what your main program could look like:


position = Point( ..., ... )
light = TrafficLight( position )
light.draw( win )
car = Car( ... )
car.draw( win )

while True:
    light.update()

    if ( not light.isGreen() ) and car.distanceFrontTo( light.getPosition() ) <= 50 :
          car.move( )


Part 3

  • Once your program works well, put all the classes you have created in a separate file, called hw11class.py and your main program in a file called hw11.py where you will import hw11class or from hw11class import *.
  • Make sure your program works as well as when it was in one class.

Testing

  • Make your program stop when the user clicks the mouse on the graphics window (once to stop the animation, once to close the window)

Submission

  • Submit your program files as follows:
  rsubmit hw11 hw11.py
  rsubmit hw11 hw11class.py