CS100A Sample #2 Prelim 1
Question 1 (20 points). For each of the statements shown below, show the new values of any variables whose values change after execution of the statement. (We have provided two examples to show you what we mean.) Variables a, b, c are of type int. Here are the values of the variables before each statement is executed:
Question 2 (20 points). Fill in the body for method computePay. It should do exactly what is described in the comments that precede it. (Hints: You do not need to define any objects. Only simple arithmetic and possibly one or more conditional statements are required. Declare any local variables that you need.) For full credit, your code should include proper comments and indentation.
// Compute the pay for a worker given the hours they worked in a week and their rate of pay given in dollars_per_hour.
// Print the result to the output window. Assume that workers are paid 2 times their regular rate of pay for any time
// worked over 40 hours in a week.
public void computePay (int hours, int dollars_per_hour )
//your code goes in here...
Question 3 (10 points). Here is a specification for method drawOval, which appears in class Graphics. Don’t fill in its body. You will just need to use the method below.
// Draw an oval that is bounded by the rectangle with upper-left corner (x,y), width w, and height h.
public void drawOval(int x, int y, int w, int h)
Assume that you have been asked to write a program that simulates the movement of a rolling ball on a tilted frictionless surface. At every timestep, the ball moves 3 pixels to the right and 3 down and must be drawn using drawOval as shown here. The squares are included only to indicate the coordinates of the upperleft corner of the box that encompasses the ball.
Fill in the blanks in the body of the following method according to the comments that precede it. No local variables should be necessary.
// Using Graphics g, draw the next ball, given the radius r and upper-left corner (x,y) of the square that encompasses
// the current ball. In other words, the formal parameters x and y indicate where the ball is now; the call to drawOval
// should draw the next ball. Don’t worry about checking that the ball remains within the drawing window.
public void drawNextBall ( Graphics g, int r, int x, int y )
g.drawOval( ____________, ____________, ____________, ____________);