You are on page 1of 5

6th Edition, Java Software Solutions

Disclaimer: In what follows, you will find the solutions to the exercises given in the 6th
edition of the book. If you happen to be using an earlier edition of the book, you should
match each one of the problems presented below to the proper exercise in the book that
you have. So keep in mind that the order in which the solutions are listed may not agree
with the order of the exercises in your book.

Please note that Secitons 2.7 (Graphics), 2.8 (Applets), and 2.9 (Drawing shapes) of
Chapter 2 are not required in the curriculum. Hence exercises 2.12 to 1.18 (which are
marked in red) can be disregarded.

Chapter 2 – Data and Expressions


Exercises Solutions

EX 2.1. Explain the following programming statement in terms of objects and the
services they provide.
System.out.println ("I gotta be me!");
The System.out object has a println method which accepts a string, enclosed in
parentheses and quotation marks, which it displays on the monitor.

EX 2.2. What output is produced by the following code fragment? Explain.


System.out.print ("Here we go!");
System.out.println ("12345");
System.out.print ("Test this if you are not sure.");
System.out.print ("Another.");
System.out.println ();
System.out.println ("All done.");
The output produced is:
Here we go!12345
Test this if you are not sure.Another.
All done.
After printing its data, the println method moves to the next line of output, whereas the
print method does not. A println statement with no data has the effect of moving down
to the next line.

EX 2.3. What is wrong with the following program statement? How can it be fixed?
System.out.println ("To be or not to be, that
is the question.");
The string to be printed is not all on one line. The problem can be fixed by using the
string concatenation operator (+) or by using a print statement for part of the string and
a println statement for the remainder of the string.

EX 2.4. What output is produced by the following statement? Explain.


System.out.println ("50 plus 25 is " + 50 + 25);
6th Edition, Java Software Solutions

The output produced is:


50 plus 25 is 5025
First the string “50 plus 25” is concatenated with the integer 50; since one of the
operands associated with the “+” operator is a string, the result is a string. Then the
string “50 plus 25 is 50” is concatenated with the integer 25, with similar results.

EX 2.5. What is the output produced by the following statement? Explain.


System.out.println ("He thrusts his fists\n\tagainst" +
" the post\nand still insists\n\the sees the \"ghost\"");
The output produced is:
He thrusts his fists
against the post
and still insists
he sees the "ghost"
Escape characters are used to go to the beginning of new lines (\n), to tab (\t), and to
print quotation marks (\").

EX 2.6. What value is contained in the integer variable size after the following
statements are executed?
size = 18;
size = size + 12;
size = size * 2;
size = size / 4;
The final value stored in size is 15.

EX 2.7. What value is contained in the floating point variable depth after the following
statements are executed?
depth = 2.4;
depth = 20 – depth * 4;
depth = depth / 5;
The final value stored in depth is 2.08.

EX 2.8. What value is contained in the integer variable length after the following
statements are executed?
length = 5;
length *= 2;
length *= length;
length /= 100;
The final value stored in length is 1.

EX 2.9. Write four different program statements that increment the value of an integer
variable total.
total = total + 1;
total += 1;
total++;
++total;
6th Edition, Java Software Solutions

EX 2.10. Given the following declarations, what result is stored in each of the listed
assignment statements?
int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;

a. iResult = num1 / num4;


iResult is assigned 5
b. fResult = num1 / num4;
fResult is assigned 5.0
c. iResult = num3 / num4;
iResult is assigned 3
d. fResult = num3 / num4;
fResult is assigned 3.0
e. fResult = val1 / num4;
fResult is assigned 3.4
f. fResult = val1 / val2;
fResult is assigned 1.3302034...
g. iResult = num1 / num2;
iResult is assigned 0
h. fResult = (double) num1 / num2;
fResult is assigned 0.625
i. fResult = num1 / (double) num2;
fResult is assigned 0.625
j. fResult = (double) (num1 / num2);
fResult is assigned 0.0
k. iResult = (int) (val1 / num4);
iResult is assigned 3
l. fResult = (int) (val1 / num4);
fResult is assigned 3.0
m. fResult = (int) ((double) num1 / num2);
fResult is assigned 0.0
n. iResult = num3 % num4;
iResult is assigned 2
o. iResult = num 2 % num3;
iResult is assigned 6
p. iResult = num3 % num2;
iResult is assigned 17
q. iResult = num2 % num4;
iResult is assigned 0
6th Edition, Java Software Solutions

EX 2.11. For each of the following expressions, indicate the order in which the operators
will be evaluated by writing a number beneath each operator.
a. a – b – c – d
1 2 3
b. a – b + c – d
1 2 3
c. a + b / c / d
3 1 2
d. a + b / c * d
3 1 2
e. a / b * c * d
1 2 3
f. a%b/c*d
1 2 3
g. a % b % c % d
1 2 3
h. a – (b – c) – d
2 1 3
i. (a – (b – c)) – d
2 1 3
j. a – ((b – c) – d)
3 1 2
k. a % (b % c) * d * e
2 1 3 4
l. a + (b – c) * d – e
3 1 2 4
m. (a + b) * c + d * e
1 2 4 3
n. (a + b) * (c / d) % e
1 3 2 4

EX 2.12. Explain the role played by the World Wide Web in the translation and execution
of some Java programs.
Java applets are designed to be moved across the web and executed in a browser.
The applet source code is compiled into Java bytecode which is sent across the
Internet using HMTL and the World Wide Web software. A Web browser with a Java
interpreter interprets and execute the bytecode.
6th Edition, Java Software Solutions

EX 2.13. Compare and contrast a traditional coordinate system and the coordinate system
used by Java graphical components.
A traditional coordinate system has the origin in the lower-left corner, with x increasing
to the right and y increasing upward. The coordinate system used by Java has the
origin in the upper-left corner with x increasing to the right and y increasing downward.

EX 2.14. How many bits are needed to store a color picture that is 400 pixels wide and 250
pixels high? Assume color is represented using the RGB technique described in
this chapter and that no special compression is done.
Allowing 8 bits for each of the 3 (red, green, and blue) color components of each of 400
x 250 pixels comprising the picture means that 8 x 3 x 400 x 250 = 2,400,000 bits are
needed.

EX 2.15. Assuming you have a Graphics object called page, write a statement that will
draw a line from point (20, 30) to point (50, 60).
page.drawLine(20,30,50,60);

EX 2.16. Assuming you have a Graphics object called page, write a statement that will
draw a rectangle with height 70 and width 35, such that its upper left corner is at
point (10, 15).
page.drawRect(10,15,35,70);

EX 2.17. Assuming you have a Graphics object called page, write a statement that will
draw a circle centered on point (50, 50) with a radius of 20 pixels.
page.drawOval(30,30,40,40);

EX 2.18. The following lines of code draw the eyes of the snowman in the Snowman applet.
The eyes seem centered on the face when drawn, but the first parameters of each
call are not equally offset from the midpoint. Explain.
page.fillOval (MID-10, TOP+10, 5, 5);
page.fillOval (MID+5, TOP+10, 5, 5);
The first parameter specifies the upperleft corner of the eyes, not the center of the
eyes. The eyes are circles of diameter 5. Hence, the center of the first eye is –10 + 2.5
= -7.5 units to the left of MID, and the second eye is 5 + 2.5 = 7.5 units to the right of
MID.

You might also like