Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Lab 3
Due:  Mon 14-Sep-2009 at 11:59pm (email copy to your CA)
(no late submissions accepted).


Read these instructions first!



Programming guidelines:

  1. Short Answers
  2. Tracing
  3. Mystery Code
  4. Non-Graphics Methods
    1. Rewrite previous problems with conditionals!
  5. Animations
    1. Bouncing Ball and Square
    2. BallAroundSquare

  1. Short Answers
    There are no short answers in this lab!
     
  2. Tracing
    In the written portion of your submission, indicate what each of the following will print or draw  Do not run these programs.  Figure this out by hand.  Remember to show your work.
     
    a)










     
    public static void main(String[] args) {
      int u = 11, v = 4;
      int w = u/v;
      double x = u/v;
      double y = 1.0+u/v;
      double z = 1.0*u/v;
      System.out.println(w + "," + x + "," + y + "," + z);
      w += ((u > v) ? u : v);
      x *= ((u%v > v%u) ? u : v);
      y /= ((w > x) ? ((x > Math.pow(y,2)) ? 4 : 3) : 2);
      z = ((z < x) ? (int)(x/z) : (x/z));
      System.out.println(w + "," + x + "," + y + "," + z);
    }
    b)





















     
    public static void main(String[] args) {
      // Remember:  unicode for 'a' is 97
      String s = "abcdefg";
      int i = (int)Math.sqrt(s.length());
      char c = s.charAt(i);
      boolean b = ((c == 'c') ? (c<100) : (i<c/100));
      System.out.println(s + "," + i + "," + c + "," + b);
      if (s.charAt(s.length()-1) < c)
        s += s;
      else if (b == true)
        s += c;
      else
        s += "Go Steelers!";
      if ((i >= Math.sqrt(c)) || (b != (s.length() == 0)))
        i *= 2;
      else if (i < s.length())
        i *= 4;
      else
        i *= 8;
      b = !b;
      c++;
      if (c != s.charAt(i))
        c++;
      c++;
      System.out.println(s + "," + i + "," + c + "," + b);
    }

     

  3. Mystery Code
    In the written portion of your submission, answer the following question in general, and in just a few words of plain English.
     
    1. What does this method do (in general)?

      public static boolean f(String s) {
        if ((s == null) || (s.length() > 3))
          return false;
        char c = s.charAt(0);
        char d = ((s.length() == 1) ? c : s.charAt(1));
        char e = ((s.length() == 3) ? s.charAt(2) : d);
        return (c == e);
      }

       
    2. The preceding method contains a bug!  It will crash for one value of s.  Which value of s causes the crash, what is the exact cause of the crash, and what exception is thrown?
       
    3. What does this method do (in general)?
        public static boolean g(int x, int y, int z) {
          if (x < y) {
            int temp = x;
            x = y;
            y = temp;
          }
          if (y >= z)
            return (x == z);
          else if (z >= x)
            return (y == z);
          else
            return (x == y);
        }
    4. What does this program do (in general)?
      import javax.swing.*;
      import java.awt.*;
      
      class MysteryAnimation1 extends JComponentWithEvents {
      
        private int rx = 0;
        private int ry = 0;
        
        public void timerFired() {
          rx = ((rx < getWidth()) ? (rx+20) : 0);
          ry += 20;
        }
      
        public void paint(Graphics2D page) {
          int height = getHeight();
          page.fillRect(rx, height-25-ry%height, 50, 50);
        }
      
        // Standard main method:
        public static void main(String[] args) { launch(300, 300); }
      }
    5. What does this program do (in general)?
      import javax.swing.*;
      import java.awt.*;
      
      class MysteryAnimation2 extends JComponentWithEvents {
        private double t = 0;
        
        public void timerFired()  {
          t += 0.5;
        }
      
        public void paint(Graphics2D page) {
          int cx = getWidth()/2, cy = getHeight()/2;
          int r = 100;
          int x = (int)Math.round(r*Math.cos(t)); // cosine, where t is in radians
          int y = (int)Math.round(r*Math.sin(t)); // sine, where t is in radians
          y = Math.abs(y);
          page.fillOval(cx + x, cy - y, 20, 20);
        }
      
        // Standard main method:
        public static void main(String[] args) { launch(300, 300); }
      }
       
  4. Non-Graphics Methods
    First, identify the 2 methods that you wrote for lab1, hw1, lab2, or hw2, that would be particularly clearer or easier or somehow better if written using conditionals rather than with arithmetic.  Choose wisely -- 2 of these methods are especially more difficult without conditions!  Then, in a class named "Lab3" in a file named "Lab3.java", rewrite these methods using conditionals.  At least one of your new methods must use "if" statements, and at least one other must use the ternary operator.  Also, include the test methods you previously wrote to be sure your new versions work properly!
     
  5. Animations
    a. Bouncing Ball and Square
    In the file Lab3BouncingBallAndSquare.java, write the animation in this jar file:
       BouncingBallAndSquare.jar  (also available in a zip file:  BouncingBallAndSquare-jarfile.zip)
    Notes:

    b.  Ball Around Square
    In the file Lab3BallAroundSquare.java, write the animation in this jar file:
       BallAroundSquare.jar (also available in a zip file:  BallAroundSquare-jarfile.zip)
    Notes:


Carpe diem!