Quiz | Posted (Thursdays) |
Given in class (Thursdays) |
||
---|---|---|---|---|
12 | Sep | 19 | Sep |
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.
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.
PARAMETERS: angle, in radians RETURN VALUE: the number of degrees equivalent to the given angleFor example, radiansToDegrees(3.14159) would have 180.0 as its return value.
PARAMETERS: angle, a double holding a value between 0 and 360 RETURN VALUE: the number of radians equivalent to the given angleFor example, degreesToRadians(180.0) would have 3.14159 as its return value, since a complete circle is 2*PI radians.
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.)
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 gramFor example, totalCalories(6,8,26) would have 200 as its return value.
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 gramFor 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.