15-100 Sections U-V-AA / Spring 2009 / Quiz 1
20 Minutes
SHOW YOUR WORK!
1.
Arithmetic (Hundreds
Digit)
Assuming the variable x holds a non-negative integer value, write one line of
code that declares and initializes the integer y to hold the hundreds digit of x.
For example, if x is 1234567, then y should be set to 5. If x is less than 100, then y should be
set to 0.
2.
Tracing
Indicate what the following will
print. Show your work.
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);
}
3.
Graphics + Arithmetic
For this part, assume a rectangle was
drawn with the following method call (where a, b, c, and d all hold integer
values between 100 and 200):
page.fillRect(a, b, c, d);
Now write a few lines of code that
draws each of the following:
a. An oval centered inside the rectangle, with 20 pixels
margin between each side of the oval and the corresponding side of the
rectangle.
b. A second rectangle that is the mirror image of the
first rectangle Ð of the same width and height, with the same top, but with its
right edge as far from the right edge of the window as the first rectangleÕs
left edge is from the left edge of the window. You may not assume that ÒwidthÓ and ÒheightÓ are set for
you.
4.
More Graphics +
Arithmetic
In hw1, you were required to deal
with the Òthin white stripeÓ problem.
a. Very briefly explain the cause of the problem.
b. Very briefly describe one way to resolve the problem.
5. Bonus/Optional: Non-Zero Digits (Arithmetic)
Assuming the variable x holds a non-negative integer value less than one
million, write a code snippet that declares and initializes the integer y to
hold the number of non-zero digits in x.
So, for example, if x is 102204, then y should be set to 4. Hint: you may want to write a helper method that takes a digit (or
perhaps the Unicode value of a digit) and returns 0 if that digit is 0 and 1
otherwise (if it is 1 through 9).