Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Homework 2
Due:  Thu 10-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. isPerfectCube
  5. Painting Flags
    1. Romania
    2. Azerbaijan
  6. Bonus/Optional:  quadrantOfIntersection
  7. Bonus/Optional:  Short Answers

  1. Short Answers
    In the written portion of your submission, answer each of the following questions.  Be very brief, but show your work where appropriate.
    Assume that "x", "y", and "z" are non-negative integer values, and ignore any errors (such as dividing by zero).
    Also assume, for graphical problems, that the variables "width" and "height" are set to the dimensions of the window:
    Hint:  Don't forget to show your work!  For example, for True or False questions, either "prove" (well, at least "argue convincingly", if briefly) that it is true or provide a counterexample if it is false.
     
    1. True of False:  (x % y) can never equal a multiple of y.
       
    2. (x % 1) always equals ___________.
       
    3. True or False:  % is associative.  That is, ((x % y) % z) is always equal to (x % (y % z)).
       
    4. The bottom-left corner of a graphics window has coordinates ________________.
       
    5. If page.fillRect(x+z, z%y, x+y, y+z) paints a square, then we know that x _____________.
       
    6. One version of DeMorgan's Law states that (x && y) is equivalent to !(!x || !y).  Prove this using truth tables.  Be sure to use appropriate helper columns.
       
    7. Another version of DeMorgan's Law states that (x || y) is equivalent to !(!x && !y).  Prove this using truth tables.  Be sure to use appropriate helper columns.
       
    8. Note:  We can often use one or both of DeMorgan's Laws to simplify logical expressions.  To demonstrate:
      prove that !(x && !y) is equivalent to (!x || y)
      1. Prove this using DeMorgan's Laws (and not truth tables)
      2. Prove this again using truth tables.

  2. Tracing
    In the written portion of your submission, indicate what each of the following will print or (if it is graphical) draw.  Do not run these programs.  Figure this out by hand.  Remember to show your work.
     
    a)
    b)
    c)

     

  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.

    Hint:  while you should definitely trace the code in this problem by hand to make some sense out of it, you may also wish to actually test the code in a compiler with samples of your choosing, at least to verify that it works as you think it does!

    Note:  some of these methods may seem a bit confusing, and in fact they are!  There are some much easier, clearer, and more sensible ways to do the same thing.  This is to test your understanding of data and expressions, not to teach the best way to write these methods.
     

    1. What does this method do (in general)?

        public static boolean mysteryMethod(int x) {
          return ((""+x).length() < x);
        }


      Remember:  Do not merely say what this does at a low level.  For instance, this answer gets zero credit:  "This method concatenates x to the empty string, takes the length of that string, and returns true if that value is less than x."  That's all true enough, but it misses the essence of this computation, which can be captured in just a few words of plain English.
       
    2. What does this method do (in general)?
        public void mysteryPaintMethod(Graphics page, int left, int top, int width, int height) {
          page.setColor(Color.black);
          page.drawRect(left, top, width, height);
      
          int hWidth = width/4;
          int hHeight = height/5;
          page.setColor(Color.blue);    
          page.fillOval(left+width/2-hWidth/2, top, hWidth, hHeight);
      
          top += hHeight;
          int bWidth = width/3;
          int bHeight = height/5*2;
          int bLeft = left+width/2-bWidth/2;
          page.setColor(Color.blue);    
          page.fillRect(left, top+bHeight/4, width, bHeight/6);
          page.setColor(Color.red);    
          page.fillRect(bLeft, top, bWidth, bHeight);
          
          page.setColor(Color.blue);    
          top += bHeight;
          int lWidth = width/7;
          int lHeight = height/5*2;
          page.fillRect(bLeft, top, lWidth, lHeight);
          page.fillRect(bLeft+bWidth-lWidth, top, lWidth, lHeight);    
        }
  4. Non-Graphics Methods
    Starting from a blank compiler (and not from a file we supply to you), in a class named Hw2 in a file named Hw2.java, write the following method:
      public static boolean isPerfectCube(int x)
    This method takes a possibly-negative integer x and returns true if x is a perfect cube, and false otherwise.  For example, isPerfectCube(-64) returns true because -64 = (-4)3.

    You should also write a suitable test method, and include a main method that calls your test method (and also calls checkAssertsAreEnabled -- the workings of which you do not need to know, and which you can copy-paste from an earlier assignment).

    Hint:  Recall from math that if x=y3, then y=x(1/3).

    Another hint:  Math.pow() does not work for negative numbers raised to fractional powers, even if the result would be an integer.  Instead, it returns NaN, which means "Not a Number".  For example, Math.pow(-32, 0.2) returns NaN even though (-32)0.2 = -2.  The same is true for many calculators (for example, if you try to raise -32 to 0.2 using the built-in Windows calculator, it says "Invalid input for function").  Fortunately, there is a simple workaround that allows your method to work properly for negatives as well as non-negatives.
     
  5. Painting Flags
    Paint each of the following flags (one flag per Java file) using only filled rectangles and filled ovals.  You should use custom colors, matching the colors in the flags as closely as possible.  Also, your flags may not be fixed-sized, but rather they must entirely fill the window, even when the window is resized, leaving a 10-pixel gray margin on all sides (you are not responsible for dealing with window sizes below 50x50).  While the window's size may change, you may assume the window will be roughly "flag-shaped" -- you will not be graded on how your flags appear in, say, a tall thin window (which is not at all "flag-shaped").

    Note: All these flag images are from the very informational CIA World Factbook, which includes a flags-of-the-world page

    Note: As we are limited to just rectangles and ovals this week, you should replace any stars in flags with ovals (of the same size, location, and color).

    Note:
      This week, you are not responsible for the "thin white stripe" that occurs between stripes when the screen size is not an even multiple of the stripe size.  Ask your CA for details.

    a. Romania
    (file:  Hw2FlagOfRomania.java)
    (larger image with details)

    b. Azerbaijan
    (file:  Hw2FlagOfAzerbaijan.java)
    (larger image with details)
    You are responsible for the crescent, but you should replace the 8-pointed star with an oval.
     
  6. Bonus/OptionalquadrantOfIntersection
    Write the following method:
       public static int quadrantOfIntersection(double m1, double b1,
                                                double m2, double b2)
    This method takes four doubles representing the two lines y=m1*x+b1 and y=m2*x+b2 and returns an int value representing the quadrant where these two lines intersect.  You are guaranteed they do intersect and in just a single point, so you can ignore the case of parallel lines or the case of identical lines.  Specifically, your method should return a 1 if the lines intersect in the top-right quadrant, a 2 for the top-left, a 3 for the bottom-left, and a 4 for the bottom-right.  If the two lines intersect at (or "very nearly" at) the origin, you should return a 0.  Also, you should write your own test method for this problem:
       public static void testQuadrantOfIntersection()
    Be thoughtful about your test cases, trying to test all the different conditions that might arise.

    Hint:  You may wish to use trigonometry (Math.sin, Math.cos, Math.tan) to find the angle to the point of intersection, and then you can divide this appropriately to convert from an angle between 0 and 2pi (in radians) and the quadrant number.  Alternatively:  on only this problem, you may (for a small deduction) use an "if" statement.

    Note: You may make any reasonable assumption as to how to handle intersections lying precisely on the x or y axes.
     
  7. Bonus/Optional:  Short Answers
    In the written portion of your submission, answer each of the following questions.  Be very brief, but show your work where appropriate.
    Assume that "x" and "y" are non-negative integer values, and ignore any errors (such as dividing by zero):
      
    1. If  (x / y) equals (y % x), then what do we know must be true about x and y?  Prove it (as best you can).
      Hint:  consider these cases separately:  y<x, y==x, y>x.
       
    2. If ((x - 1) % y) / (y - 1)  is non-zero, then what do we know must be true about x and y?  Again, prove it as best you can.  Hint:  remember that this expression uses integer division, and so truncates as needed!

Carpe diem!