Computer Science APEA 15-100, Summer 2009
Homework 5
Due:  Tue 7-Jul-2009 at 8:59am (email copy only)
(no late submissions accepted).


Read these instructions first!


  1. Reading
    Finish reading Chapter 2 carefully.  Note that the next quiz will cover all of Chapter 2 (and then some!).
     
  2. Finish Lab 5
    Finish all the examples from lab 5.  Include the solutions in your submission, all in one file named Lab5.java.  Note: the lab collaboration policy continues to apply to the lab problems.  So you can collaborate at will to solve these problems.  This only applies to the lab problems -- all other problems in this assignment are subject to the usual homework collaboration policy.
     
  3. digitAt
    Write the following method:
       public static int digitAt(String s, int index)
    This method takes a String and an index and returns the integer value of the digit at the given index.  For example, given "84+23" and index 0, the method returns the value 8.  If the index is not in bounds or there is not a digit at that location of the String, the method should return -1.

    Hint: you should not use the number 48 in your solution.  Instead, use '0', which means "the Unicode value of the character '0'", which happens to be 48.

    Here is a test method for you:
      public static void testDigitAt() {
        System.out.print("Testing digitAt... ");
        assert(digitAt("84+23",-1) == -1);
        assert(digitAt("84+23", 0) == 8);
        assert(digitAt("84+23", 1) == 4);
        assert(digitAt("84+23", 2) == -1);
        assert(digitAt("84+23", 3) == 2);
        assert(digitAt("84+23", 4) == 3);
        assert(digitAt("84+23", 5) == -1);
        System.out.println("Passed all tests!");
      }
     
  4. quadrantOfIntersection
    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, or -1 if they do not intersect in a single point (either because they are parallel or they are in fact the same line).  Quadrants are numbered normally, so 1 is the top-right, 2 top-left, 3 bottom-left, and 4 bottom-right.  If the two lines intersect at (or "very nearly" at) the origin, you should return a 0.  If the intersect directly on the x or y axis, but not at the origin, do something reasonable (your choice, just be consistent and document your design).  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.  You will be graded both on your solution and also on the completeness/robustness of your test cases.

    Note:  you do not need to use trigonometry to solve this problem!
     
  5. IsFibonacciNumber
    Write the following method:
       public static boolean isFibonacciNumber(int n)
    This method takes an integer n and returns true if it is a Fibonacci number and false otherwise, where the Fibonacci numbers are obtained by summing the two previous numbers, as such:  1, 1, 2, 3, 5, 8, 13, 21,...  While there are several ways to solve this problem, your method must use this approach:  it turns out that for every Fibonacci number z, one or both of 5z2+4 or 5z2-4 is a perfect square.  For example:
        1:  5(1)2-4 = 5-4 = 1 = 12.  Also, 5(1)2+4 = 5+4 = 9 = 32.
        2:  5(2)2-4 = 20-4 = 16 = 42.  Though, 5(2)2+4 = 20+4 = 24 (not a perfect square).
        3:  5(3)2-4 = 45-4 = 41 (not a perfect square).  But, 5(3)2+4 = 45+4 = 49 = 72.
        5:  5(5)2-4 = 125-4 = 121 = 112.  Though, 5(5)2+4 = 125+4 = 129 (not a perfect square).
    Fascinatingly, it is also true that this pattern is only true for the Fibonacci numbers!!!  For example:
        4:  5(4)2-4 = 80-4 = 76 (not a perfect square).  Also, 5(4)2+4 = 80+4 = 84 (not a perfect square).
        6:  5(6)2-4 = 180-4 = 176 (not a perfect square).  Also, 5(6)2+4 = 180+4 = 184 (not a perfect square).

    So?  With a little thought, we can use this amazing fact as a quick test to determine if a number n is in fact a Fibonacci number.  Simply check whether 5n2+4 or 5n2-4 is a perfect square!  This is the approach your method must use.  It must also call a helper method, isPerfectSquare (which was covered in lab5), which takes an integer and returns true if and only if it is a perfect square.  Also, you should write your own test method for this problem:
       public static void testIsFibonacciNumber()
    Again, be thoughtful about your test cases, trying to test all the different conditions that might arise.  You will be graded both on your solution and also on the completeness/robustness of your test cases.

Carpe diem!