Computer Science APEA 15-100, Summer 2009
Lab 3


Read these instructions first!


  1. In preparation for tomorrow's quiz, do practice quiz 0.
     
  2. 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.
     
  3. 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!
     
  4. 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? 
     
  5. 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.
     
    1. What, exactly, is causing this problem?
       
    2. 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!