Exercises 5.2
1. yes
2. no
3. yes
4. yes
5. 5
6. out of range
7. 0
8. if (taxCode == ‘T’)
price +=
(taxRate * price);
9.
if (code == 1)
{
int x = theKeyboard.readInt();
int y = theKeyboard.readInt();
theScreen.println((x+y));
}
10. if ((0 < a) && (a < 5))
b = 1 / (a * a);
else
b = a * a;
11.
public static double
cost(int distance)
{
if (distance >= 1000)
return 12.00;
else if (distance > 500)
return 10.00;
else if (distance > 100)
return 8.00;
else
return 5.00;
}
12.
/* rootsAreReal(a, b, c)
returns true if a quadratic function has
* real roots and
false if not. This is based on the
coefficients
* a, b, and c.
*
* Receive: a, b, and c, the coefficients of a quadratic
equation.
* Return: true if the roots are real, false if not
*/
public static boolean
rootsAreReal(double a, double b, double c)
{
return (b * b - 4 * a * c >= 0);
}
13.
/*
printPollutionClass(index) outputs a string
* corresponding to the
given pollution index.
*
* Receive:
index, a pollution index
* Output:
the pollution class
*/
public static void
printPollutionClass(double index)
{
Screen theScreen = new Screen();
if (index < 35.0)
theScreen.println("Pleasant\n");
else if (index > 60.0)
theScreen.println("Hazardous\n");
else
theScreen.println("Unpleasant\n");
}
14.
/*
classifyWindChill(windChill) will classify wind chill's
* effect on a person.
*
* Receive: windChill, the value of a wind chill.
* Output: the classification
of the wind chill.
*/
public static void
classifyWindChill(double windChill)
{
Screen theScreen = new Screen();
if (windChill >= 10)
theScreen.println("Not
dangerous or unpleasant\n");
else if (windChill >= -10)
theScreen.println("Unpleasant\n");
else if (windChill >= -30)
theScreen.println("Frostbite is
possible\n");
else if (windChill >= -70)
theScreen.println("Dangerous:
Frostbite likely\n");
else
theScreen.println("Instant Ice
Cube: "
+
"Frostbite in 30 seconds\n");
}