Expressions
For
exercises 1 to 14, indicate the order in which the expressions will be
evaluated by putting sequence numbers (1, 2, etc.) below each operator in the
expression. Refer to Java operator precedence.
1. a
– b – c – d 8. a – (b – c) – d
2. a
– b + c – d 9. (a – (b – c)) – d
3. a + b
/ c / d 10. a – ((b – c ) – d)
4. a + b
/ c * d 11. a % (b % c) * d * e
5. a / b
* c * d 12. a + (b – c ) * d – e
6. a % b
/ c * d 13. (a + b) * c + d * e
7. a % b
% c % d 14. (a + b) * (c / d) % e
For exercises 15 to 57, consider the declarations below, then indicate the value that is assigned in each assignment statement. That is, show what is stored in the iResult, fResult, or sResult variables after each assignment. Show each floating point value to three places past the decimal point.
int
iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
int
num5 = -14, num6 = -27;
double
fResult, val1 = 17.0, val2 = 12.78;
String
sResult, title = "Java Software Solutions";
15. iResult = num1 / num4;
16. fResult = num1 / num4;
17. iResult = num3 / num4;
18. fResult = num3 / num4;
19. fResult = val1 / num4;
20. fResult = val1 / val2;
21. iResult = num1 / num2;
22. fResult = num1 / num2;
23. fResult = (double) num1 / num2;
24. fResult = num1 / (double) num2;
25. fResult = (double) (num1 / num2);
26. iResult = (int) (val1 / num4);
27. fResult = (int) (val1 / num4);
28. fResult = (int) ((double) num1 / num2);
29. iResult = num3 % num4;
30. iResult = num2 % num3;
31. iResult = num3 % num2;
32. iResult = num2 % num4;
33. iResult = num5 % num4;
34. iResult = num6 % num5;
35. iResult = title.length();
36. fResult = title.length();
37. iResult = title.indexOf('t');
38. iResult = title.indexOf('q');
39. iResult = title.lastIndexOf('a');
40. sResult = title.toUpperCase();
41. sResult = title.replace('o', 'X');
42. sResult = title.substring(8);
43. sResult = title.substring(8, 16);
44. iResult = (title.substring(8,
16)).length();
45. sResult = title + num1;
46. sResult = title + num1 + num2;
47. sResult = title + (num1 + num2);
48. iResult = Math.abs(num6);
49. iResult = Math.abs(num1 – num2);
50. fResult = Math.sqrt(num2);
51. fResult = Math.pow(num4, 3);
52. iResult = Math.max(num2, num3);
53. iResult = Math.floor(val2);
54. iResult = Math.ceil(val2);
55. fResult = Math.sin(num2 + num1 * 2);
56. fResult = Math.PI * num4;
57. fResult = Math.pow(title.length(), 2) +
num3 *
Math.sqrt(num3
/ num4);
Conditionals
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");
2. if
(num2 <= MAX)
System.out.println ("apple");
System.out.println
("orange");
3. if
(MAX > num3)
System.out.println ("apple");
System.out.println
("orange");
4. if
(num3 >= LIMIT)
System.out.println ("apple");
System.out.println ("orange");
System.out.println
("pear");
5. if
(num2 == MAX)
{
System.out.println ("apple");
System.out.println ("orange");
}
System.out.println
("pear");
6. if
(num3-num2 > 2*MAX)
System.out.println ("apple");
else
System.out.println ("orange");
7. if
(LIMIT+num3 <= 150)
{
System.out.println ("apple");
System.out.println ("orange");
}
else
System.out.println ("pear");
8. if
(2*num1 != num2)
System.out.println ("apple");
else
{
System.out.println ("orange");
System.out.println ("pear");
}
9. if
(LIMIT%num1 + 4 == num1 + (MAX-num2))
{
System.out.println ("apple");
System.out.println ("orange");
}
else
{
System.out.println ("pear");
System.out.println ("banana");
}
10. if
(num1 < MAX)
if (LIMIT >= num2)
System.out.println ("apple");
System.out.println
("orange");
11. if
(LIMIT <= LIMIT)
if (num3 == num1)
System.out.println ("apple");
System.out.println
("orange");
12. if
(num2 > 18)
if (num1 < 0)
System.out.println ("apple");
else
System.out.println ("orange");
System.out.println
("pear");
13. if
(LIMIT >= 4*num2)
if (MAX == 25)
System.out.println ("apple");
else
System.out.println ("orange");
else
System.out.println ("pear");
14. if
(num2 < num1)
if (num3 < LIMIT)
System.out.println ("apple");
else
System.out.println ("orange");
System.out.println
("pear");
15. if
(num3 == 87)
{
if (num2 != MAX)
System.out.println ("apple");
}
else
System.out.println ("orange");
System.out.println
("pear");
16. if
(num1+num2 > num3)
System.out.println ("apple");
else
if (num2*LIMIT != 3298)
System.out.println ("orange");
17. if
(LIMIT%MAX == 3)
System.out.println ("apple");
else
if (num2 == MAX)
System.out.println ("orange");
else
System.out.println ("pear");
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");
else
System.out.println ("lime");
System.out.println
("coconut");
19. if
(num2 > num1 && LIMIT != 100)
System.out.println ("apple");
System.out.println
("orange");
20. if
(num3 == num2 && MAX > 50)
System.out.println ("apple");
System.out.println
("orange");
21. if
(num1 > 7 && LIMIT <= 100)
System.out.println ("apple");
System.out.println
("orange");
22. if
(num3 < 40 || num3 > 50)
System.out.println ("apple");
System.out.println
("orange");
23. if
(MAX == LIMIT || num1*2 == num2)
System.out.println ("apple");
System.out.println
("orange");
24. if
(num2%2 != 0 || num3 > LIMIT)
System.out.println ("apple");
System.out.println
("orange");
25. if
(MAX == 25 && num2 != MAX || num1 < num3)
System.out.println ("apple");
System.out.println
("orange");
26. if
(num3 == 87 || num2 > num1 && MAX > LIMIT)
System.out.println ("apple");
System.out.println
("orange");
27. if
((num3 == 87 || num2 > num1) && MAX > LIMIT)
System.out.println ("apple");
System.out.println
("orange");
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.
29. Increment the
integer variable total if total is zero and decrement total otherwise.
30. Print "num is zero", "num is negative", or "num is positive" as appropriate based on the
current value of num.
31. Print "num is zero", "num is even", or "num is odd" as appropriate based on the
current value of num.
32. Print "Victory" only if result is greater than or equal to 500 and penalty is equal to zero (use nested ifs).
33. Print "Victory" only if result is greater than or equal to 500 and penalty is equal to zero (use logical operators).
34. Assign the smallest of
two integer values num1 and num2 to the variable smallest. (use an if-else statement)
35. Assign the smallest of
two integer values num1 and num2 to the variable smallest. (use the conditional
operator)
36. Assign the smallest of
three integer values num1, num2, and num3 to the variable smallest. (do not use logical operators)
37. Assign the smallest of
three integer values num1, num2, and num3 to the variable smallest. (use logical operators)
38. Print "This character is a vowel." if the character stored in
the variable letter is a lowercase vowel.
39. Of the two characters
stored in the variables ch1 and ch2, print the one which comes later in the Unicode
character set.
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.
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.
Loops
For
exercises 1 to 15, indicate the output that will be produced. Assume the
following declarations are made just before each exercise. That is, assume
these initializations are in effect at the beginning of each problem:
final int MIN
= 10, MAX = 20;
int num = 15;
1. while
(num < MAX)
{
System.out.println (num);
num = num + 1;
}
2. while
(num < MAX)
{
num = num + 1;
System.out.println (num);
}
3. do
{
num = num + 1;
System.out.println (num);
}
while
(num <= MAX);
4. while
(num < MAX)
{
System.out.println (num);
num = num - 1;
}
5. while
(num > MIN)
{
System.out.println (num);
num = num - 1;
}
6. while
(num < MAX)
{
System.out.println (num);
num += 2;
}
7. while
(num < MAX)
{
if (num%2 == 0)
System.out.println (num);
num++;
}
8. do
{
num = num + 1;
if (num*2 > MAX+num)
System.out.println (num);
}
while
(num <= MAX);
9. for
(int value=0; value >= 7; value++)
System.out.println (value);
10. for
(int value=7; value < 0; value--)
System.out.println (value);
11. for
(int value=1; value >= 20; value+=4)
System.out.println (value);
12. for
(int value=num; value <= MAX; value++)
System.out.println (value);
13. for
(int value=num; value <= MAX; value++)
if (value%4 != 0)
System.out.println (value);
14. for
(int count1=1; count1 <= 7; count1++)
{
for (int count2=1; count2 <= 5;
count2++)
System.out.print ("#");
System.out.println();
}
15. for
(int count1=1; count1 <= 5; count1++)
{
for (int count2=1; count2 <= 5;
count2++)
System.out.print (count1*count2 +
" ");
System.out.println();
}
For
exercises 16 to 29, write code segments that will perform the specified action.
16. Verify that the user
enters a positive value. (use a while loop)
17. Verify that the user
enters an even value (use a do loop)
18. Read and print values
entered by a user until a particular sentinel value is encountered. Do not
print the sentinel value. Assume the sentinel value is stored in a constant
called SENTINEL.
19. Read values from the
user, quitting when a sentinel value of 0 is entered. Compute and print the
product of all values entered (excluding the sentinel value).
20. Print the odd numbers
between 1 and 100.
21. Print the multiples of 3
from 300 down to 3.
22. Print the numbers
between LOW and HIGH that are evenly divisible by four but not by five.
23. Print all of the factors
of a value stored in the variable number. Assume the value is
positive.
24. Read 10 values from the
user and print the lowest and highest value entered.
25. Determine and print
the number of times the character 'a' appears in the String variable str.
26. Print the characters
stored in the String variable str backwards.
27. Print every other
character in the String variable str starting with the first character.
28. Print a sequence of
asterisk characters in the following configuration, continuing for LINES number
of asterisks.
*
*
*
*
*
*
*
29. Print the characters of
a String variable str in a diagonal line downward. For example, if str contained "Compile", the output would be:
C
o
m
p
i
l
e
Writing Methods
For
each exercise below, write the method described. Give all of the methods public
visibility. Assume all ranges are inclusive (include both end points).
1.
Write
a method called powersOfTwo that
prints the first 10 powers of 2 (starting with 2). The method takes no
parameters and doesn't return anything.
2.
Write
a method called alarm that prints the word "Alarm!" multiple
times on separate lines. The method should accept an integer parameter that
specifies how many times the output line is printed.
3.
Write
a method called sum100 that returns
the sum of the integers from 1 to 100.
4.
Write
a method called sumRange that
accepts two integer parameters that represent a range. You may assume the first
parameter is less than or equal to the second. The method should return the sum
of the integers in that range.
5.
Write
a method called maxOfTwo that
accepts two integer parameters and returns the larger of the two.
6.
Write
a method called larger that accepts
two floating point parameters (of type double) and returns true if the
first parameter is greater than the second, and false otherwise.
7.
Write
a method called countA that accepts
a String parameter and returns the
number of times the letter ‘A’ is found in the string.
8.
Write
a method called evenlyDivisible that
accepts two integer parameters and returns true if the first parameter is
evenly divisible by the second, or vise versa, and false otherwise. You may
assume that neither parameter is zero.
9.
Write
a method called average that accepts
three integer parameters and returns their average as a floating point value.
10.
Overload
the average method of the previous
exercise such that if four integers are provided as parameters, the method
returns the average of all four.
11.
Overload
the average method once more to
accept five integer parameters and return their average.
12.
Write
a method called multiConcat that
takes a String and an integer as
parameters, and returns a String that is the parameter
string concatenated with itself n number of times (where n is the second
parameter). For example, if the parameters are "hi" and 4, the return
value is "hihihihi".
13.
Overload
the multiConcat method from the
previous example such that if the integer parameter is not provided, the method
returns the string concatenated with itself. For example, if the parameter is
"test" the return value is "testtest".
14.
Write
a method called isAlpha that accepts
a character parameter and returns true if that character is either an uppercase
or lowercase alphabetic letter.
15.
Write
a method called validate that
accepts three integer parameters. The first two parameters represent a range,
and the purpose of the method is to verify that the value of the third
parameter is in that range. You may assume that the first parameter is less
than or equal to the second. If the third parameter is not in the specified
range, the method should prompt the user and read a new value. This new value
should be tested for validity as well. The method should only return to the
calling method once a valid value has been obtained, and it should return the
valid value.
16.
Write
a method called floatEquals that
accepts three floating point values as parameters. The method should return
true if the first two parameters are essentially equal, within the tolerance of
the third parameter.
17.
Write
a method called reverse that accepts
a String as a parameter and returns
a String that contains the
characters of the parameter in reverse order. Note: there is actually a method
in the String class that performs this
operation, but for the sake of this exercise you will write your own.
18.
Write
a method called isIsoceles that
accepts three integer parameters that represent the lengths of the sides of a
triangle. The method should return true if the triangle is isosceles but not
equilateral, meaning that exactly two of the sides have an equal length, and
false otherwise.
19.
Write
a method called randomInRange that
accepts two integer parameters representing a range. You may assume that the
first parameter is less than or equal to the second, and that both are
positive. The method should return a random integer in the specified range.
20.
Overload
the randomInRange method of the
previous exercise such that if only one parameter is provided, the range is
assumed to be from 1 to that value. You may assume the parameter value is
positive.
1.
What
is the difference between an object and a class?
Int primes = {2,3,4,5,7,9}
Float elapsedTimes[] = {11.47,12.04,11.72,13.88}
Int[ ] scores = int [30];
Int[] scores = new int[30];
Int[] primes
= new {2,5,7,9};
45
23 25 7
67 90 100 26