Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

COSC 235 Test 3 - Python Programming Concepts - Prof. David A. Sykes, Exams of Computer Science

The third test for the cosc 235 course focusing on python programming concepts. It includes instructions for a class definition exercise, a method definition exercise, and an explanation of short-circuit evaluation. The document also covers the creation of a poolball class and the use of the find function.

Typology: Exams

Pre 2010

Uploaded on 08/17/2009

koofers-user-71p-2
koofers-user-71p-2 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC$235$ $ November$19,$2008$
Test%3%
Name:%_______________________________% Pledged:%_______________________________%
Answer$all$questions$completely.$
1. Indicate$for$each$term$in$the$box$ONE$example$of$the$concept$in$the$program.$
class Button:
"A button is a labeled rectangle in a window."
def __init__(self, win, center, width, height, label):
"Creates a rectangular button"
w,h = width/2.0, height/2.0
x,y = center.getX(), center.getY()
self.xmax, self.xmin = x+w, x-w
self.ymax, self.ymin = y+h, y-h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1,p2)
self.rect.setFill('lightgray')
self.rect.draw(win)
self.label = Text(center, label)
self.label.draw(win)
self.deactivate()
def clicked(self, p):
"Returns true if button active and p is inside"
return self.active and \
self.xmin <= p.getX() <= self.xmax and \
self.ymin <= p.getY() <= self.ymax
def getLabel(self):
"Returns the label string of this button."
return self.label.getText()
def activate(self):
"Sets this button to 'active'."
self.label.setFill('black')
self.rect.setWidth(2)
self.active = True
def deactivate(self):
"Sets this button to 'inactive'."
self.label.setFill('darkgrey')
self.rect.setWidth(1)
self.active = False
$method$definition$
1$formal$parameter$
2$actual$parameter$(argument)$
3$method$call$
4$local$variable$
5$Boolean‐valued$expression$$
6$initialization$method$(constructor)$
7$class$definition$
8$instance$variable$
9$object$construction$
Key
0
$
0
1$
2$
3$
4$
5$
7$
8$
9$
pf3
pf4

Partial preview of the text

Download COSC 235 Test 3 - Python Programming Concepts - Prof. David A. Sykes and more Exams Computer Science in PDF only on Docsity!

COSC 235 November 19, 2008

Test 3

Name: _______________________________ Pledged: _______________________________

Answer all questions completely.

1. Indicate for each term in the box ONE example of the concept in the program.

class Button: "A button is a labeled rectangle in a window." def init(self, win, center, width, height, label): "Creates a rectangular button" w,h = width/2.0, height/2. x,y = center.getX(), center.getY() self.xmax, self.xmin = x+w, x-w self.ymax, self.ymin = y+h, y-h p1 = Point(self.xmin, self.ymin) p2 = Point(self.xmax, self.ymax) self.rect = Rectangle(p1,p2) self.rect.setFill('lightgray') self.rect.draw(win) self.label = Text(center, label) self.label.draw(win) self.deactivate() def clicked(self, p): "Returns true if button active and p is inside" return self.active and
self.xmin <= p.getX() <= self.xmax and
self.ymin <= p.getY() <= self.ymax def getLabel(self): "Returns the label string of this button." return self.label.getText() def activate(self): "Sets this button to 'active'." self.label.setFill('black') self.rect.setWidth(2) self.active = True def deactivate(self): "Sets this button to 'inactive'." self.label.setFill('darkgrey') self.rect.setWidth(1) self.active = False

method definition

1 formal parameter

2 actual parameter ( argument )

3 method call

4 local variable

5 Boolean‐valued expression

6 initialization method ( constructor )

7 class definition

8 instance variable

9 object construction

Key

2. A pool ball is a ball with a number between 1 and 15. Balls numbered 1‐8 are

solid colors while balls numbered 9‐15 are ivory‐colored with a colored

stripe.

a. Define a class to represent a pool ball. Omit the docstrings.

PoolBall(number, color) Initialize an instance to the given number and color, where color is given by a string--for example, "yellow". getNumber() Returns the number of this ball. getColor() Returns the color of this ball. isSolid() Returns True if this ball has a solid color, False if striped. str() Return a string representation of this ball--for example, "1 ball [solid yellow]"

b. Using your class, write two statements, one to create a 1 ball, which is a solid yellow

ball, and one to create a 10 ball, which is a ball with a blue stripe. Then print both balls.

class PoolBall: def init(self, number, color): self.number = number self.color = color def getNumber(self): return self.number def getColor(self): return self.color def isSolid(self): return self.number <= 8 def str(self): if self.isSolid(): kind = "solid" else: kind = "striped" return "%d ball [%s %s]" % (self.number, kind, self.color) ball1 = PoolBall(1, "yellow") ball10 = PoolBall(10, "blue") print ball print ball

4. In Chapter 9 , Zelle develops a simulation of a racquetball game to explore whether small

difference in two players’ abilities in racquetball could result is in a disproportionate

number of wins for the slightly better player. In the process of developing a simulation

program, he uses a design process called top down design.

A structure chart is used to document a top‐down design. It is a graphical representation of

the modules (each implemented by a function in the program) and the uses relationship

between them (module M 1 uses module M 2 indicates that function M 1 will call M 2 in the

program).

Based on the structure chart shown below, describe a reasonable order in which the

designer identified the various modules. The problem being solved is to compute a child’s

allowance based on a rate of 75¢ per year for children under 10 years of age and $1.00 per

year for children 10 years of age and older. The input is a child’s age. The output is the

amount of the allowance for a child of that age.

5. Given the class defined in Problem 1, complete the code to do what comments indicate.

from graphics import * def main():

Create a graphics window

win = GraphWin("Test #3 Window", 600, 400)

Add an activated Submit 60x40-pixel button centered at (35, 25)

The main module would come first and then calcAllowanceTotal and then displayTotal. The designer would identify getAge and then calcAllowance when considering an algorithm for calcAllowanceTotal. submitBtn = Button(win, Point(35, 25), 60, 40, “Submit”) submitBtn.activate()