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

**Return and Review Quiz1**-
**Review Hw2b** **Instruction: Data+Expressions***(String values)***char values****Type conversion**

**CA-Directed Lab****Student-Directed (Collaborative) Lab**

**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.

**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.

**Instruction: Data+Expressions**

CA's will lecture on the following D+E topics (which shall appear on hw3):*(String values)***char values****Type conversion**

**CA-Directed Lab**

*Remember: Do not use conditionals or loops yet!***round**

Do not use the Math.round method here!- Write a method named round that takes a
*non-negative*double d and returns another double -- the value d rounded to the nearest integer. - Write a robotic test method for the round method.
- Write a manual test method for the round method.
- 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?
- Update your robotic test method to work with this new round method.

- Write a method named round that takes a
**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!*

**Student-Directed (Collaborative) Lab****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!"); }

**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!"); }

**Largest Double Value**

Just as there is a largest int value (2^{31}-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.System.out.println(Double.MAX_VALUE);

Hint #1: 10^{500}is too large!

Hint #2: When you are done (and only then), try the following:

**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**