## CS 101 (Spring 1999) Quiz 2:Simple Methods

Quiz Posted
(Wednesdays)
Given in class
(Wednesdays)
20 Jan 27 Jan

• On the date a quiz is given, a die will be thrown by a student in the class.
• Books and notes are put away.
• A question very similar to the chosen published question will be written on the board.
• You have 5 minutes to answer the question.
The questions are intended to be straightforward. If you keep up with the material in the class, almost no preparation may be necessary. The Collaboration Policy allows you to study in groups for the quizzes, but you are on your own when you take the quiz.

You will fare better on the quiz if you try working the problems before looking at the solutions. If you don't understand the question or its answer, please get help.

Directions: Each of the following specifications describes the input parameters and return values of a desired procedure. For each specification, write a Java procedure that correctly implements that specification. Java's built-in class Math may be helpful for some of these exercises.

1. Write a Java procedure named distance for the following specification.
2. ```PARAMETERS:   x1, y1, x2, and y2, all integers
RETURN VALUE: the Euclidian distance between the
points (x1,y1) and (x2,y2)
(the return type should be double)```
For example, distance(-1,4,2,8) would return 5.0, the square root of the sum of the squares of the distances in the x and y dimensions.

3. Write a Java procedure named radiansToDegrees for the following specification.
4. ```PARAMETERS:   angle, in radians
RETURN VALUE: the number of degrees equivalent to the given angle```
For example, radiansToDegrees(3.14159) would have 180.0 as its return value.

5. Write a Java procedure named degreesToRadians for the following specification.
6. ```PARAMETERS:   angle, a double holding a value between 0 and 360
RETURN VALUE: the number of radians equivalent to the given angle```
For example, degreesToRadians(180.0) would have 3.14159 as its return value, since a complete circle is 2*PI radians.

7. Write a Java procedure named angle for the following specification.
8. ```PARAMETERS:   x and y, both doubles, with x > 0
RETURN VALUE: the angle (in degrees), relative to the x-axis, of the
line segment from the origin to the point (x,y)```
For example, angle(3,-3) would have 315.0 as its return value. (Hint: Math.atan will be useful in conjunction with the slope of the line.)

9. Write a Java procedure named totalCalories for the following specification.
10. ```PARAMETERS:   prot, an integer representing grams of protein
fat, an integer representing grams of fat
carb, an integer representing grams of carbohydrates
RETURN VALUE: the integer number of calories, assuming
fat contains 9 calories per gram and
protein and carbohydrates each have 4 calories per gram```
For example, totalCalories(6,8,26) would have 200 as its return value.

11. Write a Java procedure named percentFromFat for the following specification.
12. ```PARAMETERS:   prot, an integer representing grams of protein
fat, an integer representing grams of fat
carb, an integer representing grams of carbohydrates
RETURN VALUE: the percentage of total calories from fat, assuming
fat contains 9 calories per gram and
protein and carbohydrates each have 4 calories per gram```
For example, percentFromFat(6,8,26) would have 36 as its return value. Remember that using integer division, 72/200 is zero. Therefore, you'll want to use type double for your constants (9.0, for example) so that floating point division is used.
Note: If a trigonometric formula is needed on a quiz, it will be supplied for you.
[ solution ]