with solutions - The Lack Thereof

advertisement
Exercise Worksheet
Java Software Solutions
Expressions (with solutions)
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
Appendix D in your textbook as needed regarding Java operator precedence.
1.
a – b – c – d
1
2.
2
2
2
1
10.
1
1
1
3
1
3
4
2
4
(a + b) * c + d * e
1
14.
3
a + (b – c ) * d – e
3
13.
2
a % (b % c) * d * e
2
12.
3
a – ((b – c ) – d)
3
11.
3
(a – (b – c)) – d
2
3
a % b % c % d
1
9.
2
a % b / c * d
1
7.
1
2
2
a / b * c * d
1
6.
1
a – (b – c) – d
3
a + b / c * d
3
5.
2
a + b / c / d
3
4.
3
a – b + c – d
1
3.
2
8.
2
4
3
(a + b) * (c / d) % e
1
3
2
4
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. Refer to Appendix M in your textbook as needed
regarding specific methods.
int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
int num5 = -14, num6 = -27;
Exercise Worksheet
Java Software Solutions
double fResult, val1 = 17.0, val2 = 12.78;
String sResult, title = "Java Software Solutions";
15.
iResult = num1 / num4;
5
16.
fResult = num1 / num4;
5.0
17.
iResult = num3 / num4;
3
18.
fResult = num3 / num4;
3.0
19.
fResult = val1 / num4;
3.4
20.
fResult = val1 / val2;
1.330
21.
iResult = num1 / num2;
0
22.
fResult = num1 / num2;
0.0
23.
fResult = (double) num1 / num2;
0.625
24.
fResult = num1 / (double) num2;
0.625
25.
fResult = (double) (num1 / num2);
0.0
26.
iResult = (int) (val1 / num4);
3
27.
fResult = (int) (val1 / num4);
3.0
28.
fResult = (int) ((double) num1 / num2);
0.0
29.
iResult = num3 % num4;
2
30.
iResult = num2 % num3;
6
31.
iResult = num3 % num2;
17
32.
iResult = num2 % num4;
0
33.
iResult = num5 % num4;
-4
34.
iResult = num6 % num5;
-13
35.
iResult = title.length();
23
36.
fResult = title.length();
23.0
Exercise Worksheet
Java Software Solutions
37.
iResult = title.indexOf('t');
8
38.
iResult = title.indexOf('q');
-1
39.
iResult = title.lastIndexOf('a');
10
40.
sResult = title.toUpperCase();
JAVA SOFTWARE SOLUTIONS
41.
sResult = title.replace('o', 'X');
Java SXftware SXlutiXns
42.
sResult = title.substring(8);
tware Solutions
43.
sResult = title.substring(8, 16);
tware So
44.
iResult = (title.substring(8, 16)).length();
45.
sResult = title + num1;
8
Java Software Solutions25
46.
sResult = title + num1 + num2;
Java Software Solutions2540
47.
sResult = title + (num1 + num2);
Java Software Solutions65
48.
iResult = Math.abs(num6);
27
49.
iResult = Math.abs(num1 – num2);
15
50.
fResult = Math.sqrt(num2);
6.3245553
51.
fResult = Math.pow(num4, 3);
125.0
52.
iResult = Math.max(num2, num3);
40
53.
iResult = Math.floor(val2);
12.0
Exercise Worksheet
Java Software Solutions
54.
iResult = Math.ceil(val2);
13.0
55.
fResult = Math.sin(num2 + num1 * 2);
0.89399666
Note: The sin function of the Math class assumes that the parameter describes the
angle in radians, not degrees.
56.
fResult = Math.PI * num4;
57.
fResult = Math.pow(title.length(), 2) + num3 *
Math.sqrt(num3 / num4);
558.44486
15.70796326
Download