Computer Science APEA 15-100, Summer 2009
Lab 3
Read these instructions first!
- The homework collaboration policy does not apply to labs. In
labs, you may work alone or in small groups. And you may show each
other your code, and help each other in any manner. This is
encouraged, in fact!
- That said, the participation policy is still in effect -- you must
not only attend labs, but actually work during the labs. If you have
completed the assigned work, then you should either help others, if they
request it, or you should delve deeper into the assigned material.
But you must use the lab time exclusively to explore the material covered in
that lab.
- In particular, you may not use lab time to do your homework.
- There is nothing to submit for labs.
- In preparation for tomorrow's quiz, do practice
quiz 0.
- Run the examples from today's lecture. Fiddle around with them a
bit, changing this and that and seeing how it changes their runtime
behaviors.
- Write a program that reads in three doubles (using scanner.nextDouble()
instead of scanner.nextInt()) and prints out their median. Remember
not to use "if" or other conditionals!
- Write a program that reads in three doubles, a, b, and c, representing
the coefficients of the quadratic equation:
f(x) = ax2 + bx + c
This equation describes a parabola. Your program should use the
quadratic formula to find and print out the two roots of the parabola ---
that is, the values x1 and x2 such that the parabola
passes through the points (x1,0) and (x2,0). If
the parabola has only one root (say, f(x) = x2), your program may
print that root twice. While your program is not responsible for
parabolas that have no real roots (say, f(x) = x2+1), see what
happens when you enter such a parabola. What does NaN mean?
- The Extra Pixel Problem
Up until now, we have had a subtle problem with how we have been drawing
our flags. Here, we will study and fix the problem. First, run
the following code:
public void paint(Graphics page) {
int width = getWidth();
int height = getHeight();
page.setColor(Color.red);
page.fillRect(0,0,width,height);
page.setColor(Color.lightGray);
page.fillRect(width*0/3, 0, width/3, height);
page.setColor(Color.gray);
page.fillRect(width*1/3, 0, width/3, height);
page.setColor(Color.lightGray);
page.fillRect(width*2/3, 0, width/3, height);
}
Now, carefully look where the stripes meet as you slowly adjust the size of
the window. For some widths, you will see a vertical red line
appearing just where the stripes meet.
- What, exactly, is causing this problem?
- How can you fix it (in general, for an arbitrary number of stripes and
an arbitrary window size)?
Note: the problem is "fixed" if the flag fills the window completely
for all window sizes, and where the width (and height) of every stripe is
within one pixel of every other stripe.
Note: at the end of today's lab, the CA's will present our
canonical solution to this problem. Henceforth, including on
tonight's homework, you will be expected to correctly deal with this
extra-pixel problem.
Carpe diem!