Computer Science 15-110 (Lecture 4), Spring 2010
Quiz 1 Practice
Remember: You may not use Java constructs that we have not yet covered in class, including String methods, doubles, loops, and conditionals.
What
is overflow? Write a few lines of Java that demonstrate
overflow.
Sample exercise: What will this code draw (answer both in words and
with a sketch):
page.drawRect(a,b,c,d);
page.drawRect(a-10,b,c,d/2);
Sample solution: two rectangles, the second 10
pixels to the left of, and half as tall as, the first, like this:
For clarity, it is preferable that you shade the original rectangle
like this:
[Rectangles] For each of the following, what will the code draw (answer both in words and with a sketch, and shade the original rectangle).
page.drawRect(a,b,c,d);
page.drawRect(a-10,b,c+10,d);
page.drawRect(a,b,c,d);
page.drawRect(a-10,b,c+20,d);
page.drawRect(a,b,c,d);
page.drawRect(a-e,b,c+(2*e),d);
page.drawRect(a,b,c,d);
page.drawRect(a-c,b,c,d);
page.drawRect(a,b,c,d);
page.drawRect(a,b+d,c,d)
page.drawRect(a,b,c,d);
page.drawRect(a+e,b+f,c-(2*e),d-(2*f))
page.drawRect(a,b,c,d);
page.drawRect(0,0,a+c/2,b);
Write a few lines of code that draws each of the following (assume the rectangle's left-top point is at (a,b) and its width is c and height is d):
An oval centered inside a rectangle.
An oval centered inside a rectangle, but with 20 pixels of margin on each side.
A 3x3 red-and-black checkerboard that fills the
rectangle (the top-left square should be red).
Explain how to draw the "mirror image" of a picture
containing rectangles. Provide a precise answer that clearly explains
the process in general. Using your technique, provide a mirror image of
the flag of Benin.
Explain how to "zoom" a picture containing rectangles,
say to make it 2 times larger. Demonstrate your technique by drawing a
simple picture that draws (and does not fill!) a rectangle all in black,
then redraws the zoomed version all in orange.
Repeat the previous problem, but zoom out to make the
orange picture 2 times smaller.
Explain how to "pan" a picture containing rectangles,
say to move it 10 pixels to the right and 20 pixels down. Once again,
demonstrate your technique by drawing a simple picture that draws (and
does not fill!) a rectangle all in black, then redraws the panned
version all in orange.
Explain how to rotate a picture (containing rectangles)
by 90 degrees counterclockwise (note that the right edge of the original
picture will become the top edge of the rotated picture). Using your
technique, provide a rotated image of the flag of Benin.
a) |
public static void main(String[] args) {
int x = 17 % 6;
int y = x - 8;
x++;
y--;
int z = Math.max(y/x, Math.abs(x/y));
System.out.println(x + "," + y +
"," + z);
} |
b) |
public
static void main(String[] args) { |
c) |
public static void main(String[] args) { int x = 10, y = 3; System.out.println(4 + x % y); System.out.println(99 / x); System.out.println(x / 99); System.out.println(99 % x); System.out.println(x % 99); System.out.println(7 % 7 / 6 % 6); } |
Carpe diem!