Docsity

Parameters, Random Numbers, and Simple Graphics - Section Handout - Programming Methodology- 20, Exercises of Programming Methodologies

Toggle sidebar
bg1
Mehran Sahami Handout #20
CS 106A October 15, 2007
Section Handout #3
Parameters, Random Numbers, and Simple Graphics
Portions of this handout b y Eric Roberts and Patrick Young.
1. True/False questions
For each of the following statements below, indicate whether it is true or false in Java:
a) The value of a local variable named
i
has no direct relationship with that of a
variable named
i
in its caller.
b) The value of a parameter named
x
has no direct relationship with that of a variable
named
x
in its caller.
2. Tracing method execution
For the program below, trace through its execution by hand to show what output is
produced when it runs.
/*
* File: Hogwarts.java
* -------------------
* This program is just testing your understanding of parameter passing.
*/
import acm.program.*;
public class Hogwarts extends ConsoleProgram {
public void run() {
bludger(2001);
}
private void bludger(int y) {
int x = y / 1000;
int z = (x + y);
x = quaffle(z, y);
println("bludger: x = " + x + ", y = " + y + ", z = " + z);
}
private int quaffle(int x, int y) {
int z = snitch(x + y, y);
y /= z;
println("quaffle: x = " + x + ", y = " + y + ", z = " + z);
return z;
}
private int snitch(int x, int y) {
y = x / (x % 10);
println("snitch: x = " + x + ", y = " + y);
return y;
}
}
pf3
pf4
pf5

Partial preview of the text

Download Parameters, Random Numbers, and Simple Graphics - Section Handout - Programming Methodology- 20 and more Exercises Programming Methodologies in PDF only on Docsity!

Mehran Sahami Handout

CS 106A October 15, 2007

Section Handout

Parameters, Random Numbers, and Simple Graphics

Portions of this handout by Eric Roberts and Patrick Young.

1. True/False questions

For each of the following statements below, indicate whether it is true or false in Java:

a) The value of a local variable named i has no direct relationship with that of a

variable named i in its caller.

b) The value of a parameter named x has no direct relationship with that of a variable

named x in its caller.

2. Tracing method execution

For the program below, trace through its execution by hand to show what output is

produced when it runs.

  • File: Hogwarts.java

  • This program is just testing your understanding of parameter passing. / import acm.program.; public class Hogwarts extends ConsoleProgram { public void run() { bludger(2001); } private void bludger(int y) { int x = y / 1000; int z = (x + y); x = quaffle(z, y); println("bludger: x = " + x + ", y = " + y + ", z = " + z); } private int quaffle(int x, int y) { int z = snitch(x + y, y); y /= z; println("quaffle: x = " + x + ", y = " + y + ", z = " + z); return z; } private int snitch(int x, int y) { y = x / (x % 10); println("snitch: x = " + x + ", y = " + y); return y; } }
  • 2 โ€“

3. Random circles

Write a GraphicsProgram that draws a set of ten circles with different sizes, positions,

and colors. Each circle should have a randomly chosen color, a randomly chosen radius

between 5 and 50 pixels, and a randomly chosen position on the canvas, subject to the

condition that the entire circle must fit inside the canvas without extending past the edge.

The following sample run shows one possible outcome:

RandomCircles

On some runs of this program you might not see ten circles. Why?

4. Drawing lines

Write a GraphicsProgram that allows the user to draw lines on the canvas. Pressing the

mouse button sets the starting point for the line. Dragging the mouse moves the other

endpoint around as the drag proceeds. Releasing the mouse fixes the line in its current

position and gets ready to start a new line. For example, suppose that you press the

mouse button somewhere on the screen and then drag it rightward an inch, holding the

button down. What youโ€™d like to see is the following picture:

DrawLines

If you then move the mouse downward without releasing the button, the displayed line

will track the mouse, so that you might see the following picture:

DrawLines

Because the original point and the mouse position appear to be joined by some elastic

string, this technique is called rubber-banding. Although this program may seem quite

powerful, it is also simple to implement. The entire program requires fewer than 20 lines

of code.

  • 2 โ€“

3. Random circles

  • File: RandomCircles.java

  • This program draws a set of 10 circles with different sizes,
  • positions, and colors. Each circle has a randomly chosen
  • color, a randomly chosen radius between 5 and 50 pixels,
  • and a randomly chosen position on the canvas, subject to
  • the condition that the entire circle must fit inside the
  • canvas without extending past the edge. / import acm.program.; import acm.graphics.; import acm.util.; public class RandomCircles extends GraphicsProgram { /** Number of circles / private static final int NCIRCLES = 10; /* Minimum radius / private static final double MIN_RADIUS = 5; /* Maximum radius / private static final double MAX_RADIUS = 50; public void run() { for (int i = 0; i < NCIRCLES; i++) { double r = rgen.nextDouble(MIN_RADIUS, MAX_RADIUS); double x = rgen.nextDouble(0, getWidth() - 2 * r); double y = rgen.nextDouble(0, getHeight() - 2 * r); GOval circle = new GOval(x, y, 2 * r, 2 * r); circle.setFilled(true); circle.setColor(rgen.nextColor()); add(circle); } } / Private instance variable */ private RandomGenerator rgen = RandomGenerator.getInstance(); }
  • 3 โ€“

4. Drawing lines

  • File: RubberBanding.java

  • This program allows users to create lines on the graphics
  • canvas by clicking and dragging with the mouse. The line
  • is redrawn from the original point to the new endpoint, which
  • makes it look as if it is connected with a rubber band. / import acm.graphics.; import acm.program.; import java.awt.event.; /** This class allows users to drag lines on the canvas / public class RubberBanding extends GraphicsProgram { public void run() { addMouseListeners(); } /* Called on mouse press to create a new line / public void mousePressed(MouseEvent e) { double x = e.getX(); double y = e.getY(); line = new GLine(x, y, x, y); add(line); } /* Called on mouse drag to reset the endpoint / public void mouseDragged(MouseEvent e) { double x = e.getX(); double y = e.getY(); line.setEndPoint(x, y); } / Private instance variables */ private GLine line; }