condSolutions - The Lack Thereof

advertisement
Exercise Worksheet
Java Software Solutions
Conditionals (with solutions)
For exercises 1 to 27, indicate the output that will be produced. Assume the following
declarations:
final int MAX = 25, LIMIT = 100;
int num1 = 12, num2 = 25, num3 = 87;
1.
if (num1 < MAX)
System.out.println ("apple");
apple
2.
if (num2 <= MAX)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
3.
if (MAX > num3)
System.out.println ("apple");
System.out.println ("orange");
orange
4.
if (num3 >= LIMIT)
System.out.println ("apple");
System.out.println ("orange");
System.out.println ("pear");
orange
pear
poor indentation
5.
if (num2 == MAX)
{
System.out.println ("apple");
System.out.println ("orange");
}
System.out.println ("pear");
apple
orange
pear
6.
if (num3-num2 > 2*MAX)
System.out.println ("apple");
else
System.out.println ("orange");
apple
7.
if (LIMIT+num3 <= 150)
{
System.out.println ("apple");
System.out.println ("orange");
}
else
System.out.println ("pear");
pear
Exercise Worksheet
Java Software Solutions
8.
if (2*num1 != num2)
System.out.println ("apple");
else
{
System.out.println ("orange");
System.out.println ("pear");
}
apple
9.
if (LIMIT%num1 + 4 ==
{
System.out.println
System.out.println
}
else
{
System.out.println
System.out.println
}
pear
banana
num1 + (MAX-num2))
("apple");
("orange");
("pear");
("banana");
10.
if (num1 < MAX)
if (LIMIT >= num2)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
11.
if (LIMIT <= LIMIT)
if (num3 == num1)
System.out.println ("apple");
System.out.println ("orange");
orange
12.
if (num2 > 18)
if (num1 < 0)
System.out.println ("apple");
else
System.out.println ("orange");
System.out.println ("pear");
orange
pear
13.
if (LIMIT >= 4*num2)
if (MAX == 25)
System.out.println ("apple");
else
System.out.println ("orange");
else
System.out.println ("pear");
apple
Exercise Worksheet
Java Software Solutions
14.
if (num2 < num1)
if (num3 < LIMIT)
System.out.println ("apple");
else
System.out.println ("orange");
System.out.println ("pear");
pear
poor indentation
15.
if (num3 == 87)
{
if (num2 != MAX)
System.out.println ("apple");
}
else
System.out.println ("orange");
System.out.println ("pear");
pear
16.
if (num1+num2 > num3)
System.out.println ("apple");
else
if (num2*LIMIT != 3298)
System.out.println ("orange");
orange
17.
if (LIMIT%MAX == 3)
System.out.println ("apple");
else
if (num2 == MAX)
System.out.println ("orange");
else
System.out.println ("pear");
orange
18.
if (num3 >= MAX)
{
if (MAX/num2 == 1)
System.out.println ("apple");
System.out.println ("orange");
if (LIMIT-num3 > num1+2)
System.out.println ("pear");
else
{
System.out.println ("banana");
System.out.println ("kiwi");
}
}
else
if (num2*2 == MAX*2)
System.out.println ("grapefruit");
apple
orange
banana
kiwi
coconut
Exercise Worksheet
Java Software Solutions
else
System.out.println ("lime");
System.out.println ("coconut");
19.
if (num2 > num1 && LIMIT != 100)
System.out.println ("apple");
System.out.println ("orange");
orange
20.
if (num3 == num2 && MAX > 50)
System.out.println ("apple");
System.out.println ("orange");
orange
21.
if (num1 > 7 && LIMIT <= 100)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
22.
if (num3 < 40 || num3 > 50)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
23.
if (MAX == LIMIT || num1*2 == num2)
System.out.println ("apple");
System.out.println ("orange");
orange
24.
if (num2%2 != 0 || num3 > LIMIT)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
25.
if (MAX == 25 && num2 != MAX || num1 < num3)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
26.
if (num3 == 87 || num2 > num1 && MAX > LIMIT)
System.out.println ("apple");
System.out.println ("orange");
apple
orange
27.
if ((num3 == 87 || num2 > num1) && MAX > LIMIT)
System.out.println ("apple");
System.out.println ("orange");
orange
Exercise Worksheet
Java Software Solutions
For exercises 28 to 41, write code segments that will perform the specified action.
Assume that all variables have already been declared and given values.
28.
Print "Hurrah!" if sum is evenly divisible by count.
if (sum%count == 0)
System.out.println ("Hurrah!");
29.
Increment the integer variable total if total is zero and decrement total
otherwise.
if (total == 0)
total++;
else
total--;
30.
Print "num is zero", "num is negative", or "num is positive"
as appropriate based on the current value of num.
if (num == 0)
System.out.println ("num is zero");
else
if (num < 0)
System.out.println ("num is negative");
else
System.out.println ("num is positive");
31.
Print "num is zero", "num is even", or "num is odd" as appropriate
based on the current value of num.
if (num == 0)
System.out.println ("num is zero");
else
if (num%2 == 0)
System.out.println ("num is even");
else
System.out.println ("num is odd");
32.
Print "Victory" only if result is greater than or equal to 500 and penalty
is equal to zero (use nested ifs).
if (result >= 500)
if (penalty == 0)
System.out.println ("Victory");
Exercise Worksheet
33.
Java Software Solutions
Print "Victory" only if result is greater than or equal to 500 and penalty
is equal to zero (use logical operators).
if (result >= 500 && penalty == 0)
System.out.println ("Victory");
34.
Assign the smallest of two integer values num1 and num2 to the variable
smallest. (use an if-else statement)
if (num1 < num2)
smallest = num1;
else
smallest = num2;
35.
Assign the smallest of two integer values num1 and num2 to the variable
smallest. (use the conditional operator)
smallest = (num1 < num2) ? num1 : num2;
36.
Assign the smallest of three integer values num1, num2, and num3 to the
variable smallest. (do not use logical operators)
if (num1 < num2)
if (num1 < num3)
smallest = num1;
else
smallest = num3;
else
if (num2 < num3)
smallest = num2;
else
smallest = num3;
37.
Assign the smallest of three integer values num1, num2, and num3 to the
variable smallest. (use logical operators)
if (num1 < num2 && num1 < num3)
smallest = num1;
else
if (num2 < num3)
smallest = num2;
else
smallest = num3;
Exercise Worksheet
38.
Java Software Solutions
Print "This is a vowel." if the character stored in the variable letter is
a lowercase vowel.
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u')
System.out.println ("This is a vowel.");
39.
Of the two characters stored in the variables ch1 and ch2, print the one which
comes later in the Unicode character set.
if (ch1 < ch2)
System.out.println (ch1);
else
System.out.println (ch2);
40.
Print "Uppercase", "Lowercase", or "Not a letter" depending on
whether the character stored in ch is an uppercase alphabetic character, a
lowercase alphabetic character, or not an alphabetic character at all.
if (ch >= 'A' && ch <= 'Z')
System.out.println ("Uppercase");
else
if (ch >= 'a' && ch <= 'z')
System.out.println ("Lowercase");
else
System.out.println ("Not a letter");
41.
Print "Equal" if two floating point values stored in val1 and val2 are exactly
equal, "Essentially Equal" if they are within 0.0001 of each other, or
"Not Equal" otherwise.
if (val1 == val2)
System.out.println ("Equal");
else
if (Math.abs(val1-val2) < 0.0001)
System.out.println ("Essentially Equal");
else
System.out.println ("Not Equal");
Download