Difference between revisions of "CSC111 Homework 11 2014"

From dftwiki3
Jump to: navigation, search
(Hints)
(Hints)
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 21:28, 15 April 2014 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 21:28, 15 April 2014 (EDT)
 
----
 
----
<center> <font size="+2">Page under construction!</font> </center> <P> <center> [[File:UnderConstruction.jpg|300px]] </center>
+
<!--center> <font size="+2">Page under construction!</font> </center> <P> <center> [[File:UnderConstruction.jpg|300px]] </center-->
 
+
<br />
 +
<bluebox>
 +
<br />
 +
This assignment is due Thursday night (4/24/14) at midnight.  You can work in pair-programming mode on this homework.
 +
<br />
 +
</bluebox>
 
<br />
 
<br />
 
<center>[[Image:CSC111_HappyEaster.png|600px]]</center>
 
<center>[[Image:CSC111_HappyEaster.png|600px]]</center>
 
<br />
 
<br />
=Problem 1=
+
=Problem 1 (100 points)=
 +
<br />
 +
<font color="red">
 +
*100 points = 70 points for program, 30 points for image
 +
* program: 70 = 20 points for documentation, 50 points for program structure, logic, and organization.
 +
</font>
 
<br />
 
<br />
 
You have been hired for the summer by a company that makes wrapping paper.  The company has a Web form where customers can pick different colors, a general theme, and a message to print in the middle of the page.  Then a Python program generates an image that is printed by a large format printer.
 
You have been hired for the summer by a company that makes wrapping paper.  The company has a Web form where customers can pick different colors, a general theme, and a message to print in the middle of the page.  Then a Python program generates an image that is printed by a large format printer.
  
The image above shows one particular output, for somebody who has selected ''Cars'' as the theme, ''Yellow'' as the background, and "Happy Easter" as the message.
+
The image above shows one particular output, for somebody who has selected ''Cars'' as the theme, ''Blue'' as the background, and "Happy Easter" as the message.
 
<br />
 
<br />
 
==Your assignment==
 
==Your assignment==
 
<br />
 
<br />
  
You must write a program that generates a sample output, with a predefined color, a predefined scheme, and a predefined message.  Your program will '''NOT''' ask the user for any information.
+
You must demonstrate that you can have the programming job by writing a program that generates a sample output, with a predefined color, a predefined scheme, and a predefined message.  Your program will '''NOT''' ask the user for any information.
  
 
<br />
 
<br />
Line 23: Line 33:
 
You must submit 2 files:  
 
You must submit 2 files:  
 
* Your program, which should be called '''hw11a.py'''
 
* Your program, which should be called '''hw11a.py'''
* An image of the output generated by your program.  It should be called '''sheet.png''' or '''sheet.jpg'''
+
* An image of the output generated by your program.  It should be called '''hw11a.png''' or '''hw11a.jpg'''
 
<br />
 
<br />
  
 
==Requirements==
 
==Requirements==
 
<br />
 
<br />
* Your program should print rows of cars.  The color of the cars should be randomly generated.
+
* Your program should print rows of cars.  The color of the cars should be '''randomly generated'''.
* Your program uses the same car for each row (the image above shows an output of the optional and extra-credit program)
+
* Your program organizes the cars in rows, one above the other.  The car may or may not align vertically (the image above shows them aligned vertically).
* You are free to pick the shape of your car.  It must have at least 2 rectangles and at least two wheels.  You pick the shape that you like.
+
* You are free to design the shape of your car.  It must have at least 2 rectangles and at least two wheels.
* A given  car can have differently colored rectangles, or the same color for all its rectangles.  You decide.
+
* A given  car can have differently colored rectangles, or the same color for all its rectangles.  Your design decision.
 
* Cars are allowed to extend past the boundaries of the window.
 
* Cars are allowed to extend past the boundaries of the window.
 
* The message in the middle should appear inside a rectangle that hides the cars underneath it
 
* The message in the middle should appear inside a rectangle that hides the cars underneath it
 
* The message should be as centered in the rectangle as possible.
 
* The message should be as centered in the rectangle as possible.
* The message should be large enough to be easily readable
+
* The message should be large enough to be easily readable.
 +
* The actual text of the message is up to you.  It should be at least 5 characters long. 
 +
* Your program should have a main() function defined at the end, and one call to main() at the end of the program. 
 +
<br />
  
<br />
 
 
==Hints==
 
==Hints==
 
<br />
 
<br />
Line 53: Line 65:
 
:::<source lang="Python">
 
:::<source lang="Python">
  
    canvas.setTextFont( 'arial', 40, 'bold' )
+
canvas.setTextFont( 'arial', 40, 'bold' )
    canvas.drawText( 100, 100,  "CSC111 Homework 11" )
+
canvas.drawText( 100, 100,  "CSC111 Homework 11" )
  
 
</source>
 
</source>
 +
<br />
 +
* Do not hesitate to give your classes extra methods that can make your life easier in your main program.  For example, I used a '''setRandomColor()''' method for my Car class.  This way I can generate a black car, then ask it to pick its own random color:
 +
<br />
 +
:::<source lang="Python">
 +
# generate a black car by default
 +
c = Car( x, y, w, h, (0, 0, 0) )
 +
 +
# make it pick a random color for itself
 +
c.setRandomColor()
 +
</source>
 +
 +
<br />
 +
* Colors can also be specified by name, as in
 +
<br />
 +
:::<source lang="Python">
 +
canvas.setFill( "yellow" )
 +
</source>
 +
<br />
 +
In case you wanted to limit the colors to a specific set, you may find this [[Color_Charts_for_TKinter | chart of color names]] useful.  In this case you could pick a random color this way:
 +
<br />
 +
:::<source lang="Python">
 +
color = choice[ 'snow', 'ghost white', 'white smoke', 'gainsboro', 'floral white', 'old lace',
 +
    'linen', 'antique white', 'papaya whip', 'blanched almond', 'bisque', 'peach puff',
 +
    'navajo white', 'lemon chiffon' ]
 +
canvas.setFill( color )
 +
</source>
 +
<br />
 +
(Remember that '''choice''' must be ''imported'' from random before it can be understood by Python!)
 
<br />
 
<br />
  
Line 64: Line 104:
 
The submission URL is [http://cs.smith.edu/~thiebaut/111b/submit11.php http://cs.smith.edu/~thiebaut/111b/submit11.php]
 
The submission URL is [http://cs.smith.edu/~thiebaut/111b/submit11.php http://cs.smith.edu/~thiebaut/111b/submit11.php]
 
<br />
 
<br />
=Optional and Extra-Credits: Problem 2=
 
 
<br />
 
<br />
 +
<br />
 +
 +
=Optional and Extra-Credits: Problem 2 (20 points)=
 +
<br />
 +
<center>[[Image:CSCcarsAndTrucks.png|600px]]</center>
 +
<br />
 +
<font color="red">20 extra points = 10 points for program, 10 points for image</font>
 +
<br />
 +
This second problem requires you to use two different types of cars.  They should be different enough  to be easily distinguished from one another.
 +
 +
The program must generate rows of cars, alternating between the two types of car.  The first row should have cars of Type 1, the second row cars of Type 2, and continuing alternating between the two.
 +
 +
The program must use a for-loop to display the rows of cars!
 +
 +
You need to submit two files, a program called '''hw11b.py''' and a screen copy of your image, called '''hw11b.jpg''' or '''hw11b.png''' (all lower-case).  Submit both to the same URL as above. 
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<!--
 +
<source lang="python">
 +
# homework 11
 +
from graphics import GraphicsWindow
 +
from random import seed
 +
from random import randrange
 +
 +
MAXWIDTH = 800
 +
MAXHEIGHT = 600
 +
  
 +
class Circle2:
 +
    def __init__( self, x, y, diameter, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._diameter= diameter
 +
        self._color  = color  # list of 3 ints between 0 and 255
 +
 +
    def draw( self, canvas ):
 +
        xAnchor = self._x-self._diameter//2
 +
        yAnchor = self._y-self._diameter//2
 +
        canvas.setFill( self._color[0], self._color[1], self._color[2] )           
 +
        canvas.drawOval( xAnchor,
 +
                        yAnchor,
 +
                        self._diameter,
 +
                        self._diameter )
 +
 +
class Wheel:
 +
    def __init__( self, x, y, diameter ):
 +
        self._c1 = Circle2( x, y, diameter, ( 0,0,0 ) )
 +
        self._c2 = Circle2( x, y, diameter/2, ( 200, 200, 200 ) )
 +
 +
    def draw( self, canvas ):
 +
        self._c1.draw( canvas )
 +
        self._c2.draw( canvas )
 +
 +
class Rectangle:
 +
    def __init__( self, x, y, width, height, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._width  = width
 +
        self._height = height
 +
        self._color  = color  # list of 3 ints between 0 and 255
 +
 +
    def setRandomColor( self ):
 +
        r = randrange( 256 )
 +
        g = randrange( 256 )
 +
        b = randrange( 256 )
 +
        self._color = (r, g, b )
 +
 +
    def draw( self, canvas ):
 +
        canvas.setFill( self._color[0], self._color[1], self._color[2] )           
 +
        canvas.drawRect( self._x, self._y, self._width, self._height )
 +
       
 +
class Car:
 +
    def __init__( self, x, y, width, height, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._width  = width
 +
        self._height  = height
 +
        self._color  = color  # list of 3 ints between 0 and 255
 +
 +
        # build the body
 +
        self._top  = Rectangle( x+width//4, y-height//2,
 +
                                width//2, height//2,
 +
                                color )
 +
        self._body = Rectangle( x, y, width, height, color )
 +
       
 +
        # build the wheels
 +
        self._w1 = Wheel( x + width//4, y + height, width//5 )
 +
        self._w2 = Wheel( x + 3*width//4, y + height, width//5 )
 +
 +
    def setRandomColor( self ):
 +
        self._top.setRandomColor()
 +
        self._body.setRandomColor()
 +
       
 +
    def getTotalHeight( self ):
 +
        return self._height*2 +  self._width//10
 +
 +
    def getTotalWidth( self ):
 +
        return self._width
 +
       
 +
    def draw( self, canvas ):
 +
        self._body.draw( canvas )
 +
        self._top.draw( canvas )
 +
        self._w1.draw( canvas )
 +
        self._w2.draw( canvas )
 +
 +
class Truck:
 +
    def __init__( self,  x, y, width, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._width  = width
 +
        height        = 5 * width//14
 +
        self._height  = height
 +
        self._color  = color  # list of 3 ints between 0 and 255
 +
 +
       
 +
        # build the body
 +
        self._top  = Rectangle( x+ 9* width//14, y-height,
 +
                                width*5//14, height,
 +
                                color )
 +
        self._window = Rectangle( x+ 10* width//14, y-4*height//5,
 +
                                width*4//14, 4*height//5,
 +
                                (0, 0, 0 ) )
 +
        self._body = Rectangle( x, y, width, height, color )
 +
       
 +
        # build the wheels
 +
        self._w1 = Wheel( x + 2*width//14, y + 13*height//10, 3*height//5 )
 +
        self._w2 = Wheel( x + 11*width//14, y + height, width//2 )
 +
 +
    def getTotalHeight( self ):
 +
        return self._height*2 +  self._width//2
 +
 +
    def getTotalWidth( self ):
 +
        return self._width
 +
 +
    def setRandomColor( self ):
 +
        self._top.setRandomColor()
 +
        self._body.setRandomColor()
 +
   
 +
    def draw( self, canvas ):
 +
        self._body.draw( canvas )
 +
        self._top.draw( canvas )
 +
        self._window.draw( canvas )
 +
        self._w1.draw( canvas )
 +
        self._w2.draw( canvas )
 +
   
 +
 
 +
           
 +
def main():
 +
    seed()
 +
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
 +
    canvas = win.canvas()
 +
   
 +
    canvas.setBackground( 0, 250, 250 )
 +
   
 +
    carWidth = 90
 +
    carHeight = 25
 +
    oddEven = True
 +
    for y in range( 0, MAXHEIGHT, carHeight*2 ):#+20 ):
 +
        for x in range( 0, MAXWIDTH, carWidth+10 ):
 +
            if oddEven:
 +
                c = Car( x, y, carWidth, carHeight, ( 0, 0, 0 ) )
 +
            else:
 +
                c = Truck( x, y, carWidth, (0, 0, 0 ) )
 +
            c.setRandomColor()
 +
            c.draw( canvas )
 +
        #oddEven = not oddEven
 +
 +
    banner = Rectangle( MAXWIDTH//3, 2*MAXHEIGHT//5,
 +
                        MAXWIDTH//3, MAXHEIGHT//5, (255, 255, 255 ) )
 +
    banner.draw( canvas )
 +
    canvas.setTextFont( 'arial', 40, 'bold' )
 +
    canvas.drawText( MAXWIDTH//3+10, 2*MAXHEIGHT//5+30,
 +
                    "Happy Easter" )
 +
    win.wait()
 +
    win.close()
 +
   
 +
main()
 +
 +
</source>
 +
 +
-->
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 
<br />
 
<br />
 +
[[Category:CSC111]][[Category:Homework]][[Category:Python]]

Latest revision as of 16:09, 17 April 2014

--D. Thiebaut (talk) 21:28, 15 April 2014 (EDT)




This assignment is due Thursday night (4/24/14) at midnight. You can work in pair-programming mode on this homework.


CSC111 HappyEaster.png


Problem 1 (100 points)


  • 100 points = 70 points for program, 30 points for image
  • program: 70 = 20 points for documentation, 50 points for program structure, logic, and organization.


You have been hired for the summer by a company that makes wrapping paper. The company has a Web form where customers can pick different colors, a general theme, and a message to print in the middle of the page. Then a Python program generates an image that is printed by a large format printer.

The image above shows one particular output, for somebody who has selected Cars as the theme, Blue as the background, and "Happy Easter" as the message.

Your assignment


You must demonstrate that you can have the programming job by writing a program that generates a sample output, with a predefined color, a predefined scheme, and a predefined message. Your program will NOT ask the user for any information.


Submission


You must submit 2 files:

  • Your program, which should be called hw11a.py
  • An image of the output generated by your program. It should be called hw11a.png or hw11a.jpg


Requirements


  • Your program should print rows of cars. The color of the cars should be randomly generated.
  • Your program organizes the cars in rows, one above the other. The car may or may not align vertically (the image above shows them aligned vertically).
  • You are free to design the shape of your car. It must have at least 2 rectangles and at least two wheels.
  • A given car can have differently colored rectangles, or the same color for all its rectangles. Your design decision.
  • Cars are allowed to extend past the boundaries of the window.
  • The message in the middle should appear inside a rectangle that hides the cars underneath it
  • The message should be as centered in the rectangle as possible.
  • The message should be large enough to be easily readable.
  • The actual text of the message is up to you. It should be at least 5 characters long.
  • Your program should have a main() function defined at the end, and one call to main() at the end of the program.


Hints


  • To set the color of the background, you can use the setBackground() canvas method:


win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
canvas = win.canvas()
    
canvas.setBackground( 0, 250, 250 )


  • Printing text can be used using two other canvas methods: setTextFont() and drawText():


canvas.setTextFont( 'arial', 40, 'bold' )
canvas.drawText( 100, 100,  "CSC111 Homework 11" )


  • Do not hesitate to give your classes extra methods that can make your life easier in your main program. For example, I used a setRandomColor() method for my Car class. This way I can generate a black car, then ask it to pick its own random color:


# generate a black car by default
c = Car( x, y, w, h, (0, 0, 0) ) 

# make it pick a random color for itself
c.setRandomColor()


  • Colors can also be specified by name, as in


canvas.setFill( "yellow" )


In case you wanted to limit the colors to a specific set, you may find this chart of color names useful. In this case you could pick a random color this way:

color = choice[ 'snow', 'ghost white', 'white smoke', 'gainsboro', 'floral white', 'old lace',
    'linen', 'antique white', 'papaya whip', 'blanched almond', 'bisque', 'peach puff',
    'navajo white', 'lemon chiffon' ]
canvas.setFill( color )


(Remember that choice must be imported from random before it can be understood by Python!)

Submission


Remember that you must submit 2 files, the program and a screen capture of your window. The submission URL is http://cs.smith.edu/~thiebaut/111b/submit11.php


Optional and Extra-Credits: Problem 2 (20 points)


CSCcarsAndTrucks.png


20 extra points = 10 points for program, 10 points for image
This second problem requires you to use two different types of cars. They should be different enough to be easily distinguished from one another.

The program must generate rows of cars, alternating between the two types of car. The first row should have cars of Type 1, the second row cars of Type 2, and continuing alternating between the two.

The program must use a for-loop to display the rows of cars!

You need to submit two files, a program called hw11b.py and a screen copy of your image, called hw11b.jpg or hw11b.png (all lower-case). Submit both to the same URL as above.