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 - October 29, 2008 Test 2 Solutions - Prof. David A. Sykes, Exams of Computer Science

The solutions to test 2 for cosc 235, including examples of function definitions, line segment drawing, and a function to count negative numbers in a list. It also includes an example of exception handling.

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-15a
koofers-user-15a 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC$235$ $ October$29,$2008$
Test%2%
Name:%______________________________% Pledged:%_________________________________%
Answer$all$questions$completely.$
1. Indicate$for$each$term$ONE$example$of$the$concept$in$the$program.$
0$function$definition$
1$formal$parameter$
2$actual$parameter$(argument)$
3$function$call$
4$local$variable$
5$docstring$$
6$object$construction$
7$method$call$
8$comment$
9 module
from graphics import *
def moveTo(shape, newCenter):
"Move the shape to point newCenter"
# Get the current center
currCenter = shape.getCenter()
# Compute the deltas for x and y
dx = newCenter.getX() - currCenter.getX()
dy = newCenter.getY() - currCenter.getY()
# Move the shape
shape.move(dx, dy)
# Test the function by moving a circle
print "Click once to move the circle, click again to close."
# Put a circle at the center of a graphics window
win = GraphWin()
circ = Circle(Point(100,100), 25)
circ.draw(win)
# Move the circle to the point the mouse is clicked
pt = win.getMouse()
moveTo(circ, pt)
win.getMouse()
win.close()
pf3
pf4

Partial preview of the text

Download COSC 235 - October 29, 2008 Test 2 Solutions - Prof. David A. Sykes and more Exams Computer Science in PDF only on Docsity!

Test 2

Name: ______________________________ Pledged: _________________________________

Answer all questions completely.

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

0 function definition

1 formal parameter

2 actual parameter ( argument )

3 function call

4 local variable

5 docstring

6 object construction

7 method call

8 comment

9 module from graphics import * def moveTo(shape, newCenter): "Move the shape to point newCenter"

Get the current center

currCenter = shape.getCenter()

Compute the deltas for x and y

dx = newCenter.getX() - currCenter.getX() dy = newCenter.getY() - currCenter.getY()

Move the shape

shape.move(dx, dy)

Test the function by moving a circle

print "Click once to move the circle, click again to close."

Put a circle at the center of a graphics window

win = GraphWin() circ = Circle(Point(100,100), 25) circ.draw(win)

Move the circle to the point the mouse is clicked

pt = win.getMouse() moveTo(circ, pt) win.getMouse() win.close()

2. Complete the segment to display a line segment in a graphics window. The user clicking

twice in the window determines the endpoints of the line segment.

from graphics import *

Open a graphics window

win = GraphWin("Line Segment", 600, 400)

Wait for 2 mouse clicks and capture the 2 click points, p1 and p

Draw a line segment between p1 and p

Wait for a third click and then close the window

3. Write a function to meet this specification:

negatives(numList) returns the count of the items in numList, that are negative, where

numList is a list of numbers. For example, negatives([-1, 2, - 3, - 4 , 5 ]) is 3.

5. Body mass index (BMI) is sometimes used to determine whether a person is overweight.

An alternative to BMI is waist‐to‐hip ratio (WHR), computed by dividing the waist

measurement by hip measurement. The interpretation is shown in the table.

Male Female Health Risk Based Solely on WHR 0.95 or below 0.80 or below Low Risk 0.96 to 1.0 0.81 to 0.85 Moderate Risk above 1.0 above 0.85 High Risk

Complete the definition of the function WHR() below. Floating point values can be rounded

using the built‐in round() function.^1 Make sure your function returns a integer value and

does not print it. Use as few comparisons as you can.

from math import round def WHR(sex, waist, hips): """Computes the waist-to-hip ratio and returns an integer value that interprets the ratio: - 1 indicates low risk, 0 indicates moderate risk, and 1 indicates high risk. sex is either 'M' or 'F', indicating male or female. For example, WHR('M', 32, 36) returns - 1 (since the ratio is 0.89).""" (^1) round( x, n)—Return the floating point value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(‐0.5) is ‐1.0).