Decision Exercises

advertisement
Decision Exercises
Use relational expressions to express the following conditions, use variable names of your own
choosing.
1. a person's age is equal to 30
2. a person's temperature is greater than 98.6
3. a person's height is less than 6 feet
4. the current month is 12
5. the letter input is m
6. the person's age is equal to 30 and the person is taller than 6 feet
7. a length is greater than 2 feet and less than 3 feet
8. taxRate is over 25% and income is less than $20000
9. temperature is less than or equal to 75 or humidity is less than 70%
10. age is over 21 and age is less than 60
11. age is 21 or 22
12. Translate each of the following English statements into logical tests that could be used
in an if/else statement. Write the appropriate if statement with your logical test. Assume
that three int variables x, y, and z, have been declared.
a. z is odd
b. z is not greater than y’s square root
c. y is positive
d. one of x and y is even, and the other is odd
e. z is a multiple of x
f. z is not zero
g. z is greater in magnitude than z
h. x and z are of opposite signs
i. y is a non-negative one-digit number
j. z is non-negative
k. x is even
l. x is closer in value to z than y is.
13. For what value(s) of x would this condition be true? ((x < 1) && (x > 10))
14. For what value(s) of y would this condition be true? ((y >= 1) && (y <= 10))
15. Given the following variable declarations:
int x = 4;
int y = -3;
int z = 4;
What are the resultsof the following relational expressions?
a) x == 4
b) x == y
c) x == z
d) y == z
e) x + y > 0
f) x – y != 0
g) y * y <= z
h) y / y == 1
i) x * (y + 2) > y – (y + z) * 2
16. Declare eligible to be a Boolean variable, and assign it the value true.
17. Write a statement that sets the Boolean variable available to true if
numberOrdered
is less than or equal to numberOnHand
minus
numberReserved.
18. Write a statement containing a logical expression that assigns true to the Boolean
variable isCandidate if satScore is greater than or equal to 1100, gpa is not less
than 2.5, and age is greater than 15. Otherwise, isCandidate should be false.
19. Given the declarations
boolean leftPage;
int
pageNumber:
write a statement that sets leftPage to true if pageNumber is even. (Hint: Consider what
the remainders are when you divide different integers by 2.)
20. Write an if statement (or a series of if statements) that assigns to the variable biggest
the greatest value contained in variables i, j, and k. Assume the three values are
distinct.
21. Rewrite the following sequence of ifs as a single if- else.
if (year % 4 == 0)
System.out.println(year + " is a leap year.");
if (year % 4 != 0)
{
year = year + 4 - year % 4;
System.out.println(year + " is the next leap year.");
}
22. Simplify the following program segment, taking out unnecessary comparisons. Assume
that age is an int variable.
if (age > 64)
System.out.println("Senior voter");
if (age < 18)
System.out.println("Under age");
if (age >= 18 && age < 65)
System.out.println("Regular voter");
23. What is the output?
(a) if (6 < 2 * 5)
System.out.print("Hello");
System.out.print(" There");
(b)if (7 <= 7)
System.out.println(6 - 9 * 2 / 6);
(c) if (5 < 3)
System.out.println("*");
else
if (7 == 8)
System.out.println("&");
else
System.out.println("$");
24. Given the float variables xl, x2, yl, y2, and m, write a program segment to find the
slope of a line through the two points (xl, yl) and (x2, y2). Use the formula
m = yl-y2
xl- x2
to determine the slope of the line. If xl equals x2, the line is vertical and the slope is undefined.
The segment should write the slope with an appropriate label. If the slope is undefined, it should
write the message "Slope undefined."
25.
Correct the following code so that it prints the correct message.
if (score > 60)
System.out.println("You pass.");
else;
System.out.println("You fail.");
26. State whether the following are valid switch statements. If not, explain why. Assume:
that n and digit are int variables.
a.
switch (n <= 2)
{
case 0:
System.out.println("Draw.");
break;
case 1:
System.out.println("Win.");
break;
case 2:
System.out.println("Lose.");
break;
}
b.
switch (digit / 4)
{
case 0: case 1:
System.out.println("low.");
break;
case 1: case 2:
System.out.println("middle.");
break;
case 3: System.out.println("high.");
c.
switch (n % 6)
{
case 1: case 2: case 3: case 4: case 5:
System.out.println(n);
break;
case 0:
System.out.print1n(j);
break;
}
d.
switch (n % 20)
{
case 0: case 2: case 4: case 6: case 8:
System.out.println(”Even”);
break;
case 1: case 3: case 5: case 7:
System.out.println(”Odd”);
break;
}
27. Suppose the input is 5.What is the value of alpha after the following java code
executes? (Assume that alpha is an int variable arid console is a Scanner object initialized to the
keyboard.)
alpha = console.nextlnt();
switch (alpha)
{
case 1:
case 2:
alpha = alpha + 2;
break;
case 4:
alpha++;
case 5:
alpha = 2 * alpha;
case 6:
alpha = alpha + 5;
break;
default:
alpha--;
}
28. Suppose the input is 3-What is the value of beta after the following java code executes?
(Assume that all variables are properly declared.)
beta = console.nextlntQ;
switch (beta)
{
case 3: beta = beta + 3;
case 1: beta++; break;
case 5: beta = beta + 5;
case 4: beta = beta + 4;
}
29.What is the output of the following code?
Point p1 = new Point(3, -2);
Point p2 = new Point(3, -2);
if (p1 == p2)
System.out.println(“equal”);
else
System.out.println(“non-equal”);
Download