Computer Science 15-110, Spring 2010
Recitation for Week #3


  1. Return and Review Quiz1
  2. Review Hw2b
  3. Instruction:  Data+Expressions
    1. (String values)
      1. Escape Sequences
    2. char values
      1. char literals
      2. char comparisons
    3. Type conversion
  4. CA-Directed Lab
    1. round
    2. letterGrade
  5. Student-Directed (Collaborative) Lab
    1. almostEqual
    2. distance
    3. Largest Double Value
    4. Smallest Double Value

  1. Return and Review Quiz1
    CA's will return and briefly review highlights of quiz1.  Students needing deeper review of the material should set up a separate meeting with their CA or instructor.
     
  2. Review Hw2b
    CA's will briefly review highlights of hw2b.  Students needing deeper review of the material should set up a separate meeting with their CA or instructor.
     
  3. Instruction:  Data+Expressions
    CA's will lecture on the following D+E topics (which shall appear on hw3):
    1. (String values)
      1. Escape Sequences
    2. char values
      1. char literals
      2. char comparisons
    3. Type conversion
       
  4. CA-Directed Lab
    Remember:  Do not use conditionals or loops yet!
    1. round
      Do not use the Math.round method here!
      1. Write a method named round that takes a non-negative double d and returns another double -- the value d rounded to the nearest integer.
      2. Write a robotic test method for the round method.
      3. Write a manual test method for the round method.
      4. Update the round method so it also works for negative values (which round down rather than up).  Your solution may/should use Math.signum (and it may also use Math.abs, though that's less critical).  Why is Math.signum useful here?
      5. Update your robotic test method to work with this new round method.
         
    2. letterGrade
      Write a method named letterGrade (and its robotic and manual test methods!) that takes an int value, numericGrade, and returns a char value, the corresponding letter grade, according to the normal 90/80/70/60 rule.  Scores over 100 still receive an A, and any score below 60 receives an E rather than an F or an R (to make the problem a bit easier).  Thus, letterGrade(103) should return 'A' and letterGrade(53) should return 'E'. Hint:  Math.min and Math.max may be handy here!
       
  5. Student-Directed (Collaborative) Lab
    1. almostEqual
      Trying not to look at the class notes, write a method named almostEqual that takes two doubles and returns true if they are within 0.00001 of each other and false otherwise.  Write a robotic test method, starting with this:
        public static void testAlmostEqual() {
          System.out.print("Testing almostEqual... ");
          double d1 = (29.0 / 7.0) * 7.0;
          double d2 = 29.0;
          assert(d1 != d2);
          assert(almostEqual(d1, d2));   // two very-nearly-equal values
          assert(almostEqual(-d1, -d2)); // and their negations
          System.out.println("Passed all tests!");
        }
    2. distance
      Write a method named distance that takes four doubles -- x1, y1, x2, y2 -- and returns a double value that is the distance from (x1,y1) to (x2,y2).  You should use the following robotic test method (that uses the almostEqual method you just wrote):
        public static void testDistance() {
          System.out.print("Testing distance... ");
          assert(almostEqual(distance(0,0,0,0), 0));
          assert(almostEqual(distance(0,0,1,0), 1));
          assert(almostEqual(distance(1,0,0,0), 1));
          assert(almostEqual(distance(0,0,1,1), Math.sqrt(2)));
          assert(almostEqual(distance(0,0,-1,1), Math.sqrt(2)));
          assert(almostEqual(distance(4,3,1,7), 5));
          System.out.println("Passed all tests!");
        }
    3. Largest Double Value
      Just as there is a largest int value (231-1, which is just over 2.1 billion), there is also a largest double value.  Try to find the largest power of 10 that can be expressed as a double.  You may wish to use Math.pow here and try a few values to narrow down your search.
      Hint #1:  10500 is too large!
      Hint #2:  When you are done (and only then), try the following:
        System.out.println(Double.MAX_VALUE);
       
    4. Smallest Double Value
      The smallest negative double value is roughly the same as the largest positive double value, only negative.  Clear enough.  But what is the smallest positive double value?  Try this:
        System.out.println(Double.MIN_VALUE);
      Then, run these lines and explain the output:
        double d = Double.MIN_VALUE;
        double half = d/2;
        System.out.println("Half of " + d + " = " + half);

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem