Lab 04h, Part 1, Two Meanings of Plus
In Java, the symbol
+
can be used to add numbers or to concatenate
strings. This lab illustrates both uses.
When using a string literal (a sequence of characters enclosed
in double quotation marks) in Java the complete string must fit
on one line. The following is NOT legal (it would result in a
compile-time error).
System.out.println ("It is NOT okay to go to the next line
in a LONG string!!!");
The solution is to break the long string up into two shorter strings that are joined using the concatenation operator (which is the + symbol). This is discussed in Section 2.1 in the text. So the following would be legal
System.out.println ("It is OKAY to break a long string into "
+ "parts and join them with a + symbol.");
(Note how the continued line is indented one space to indicate it is
a continuation line. Also note that the operator is placed at the beginning
of the continuation line instead of at the end of the first line,
to improve readability.)
So, when working with strings the +
symbol means to concatenate the strings (join them). BUT,
when working with numbers the
+
means what it has always meant, add!
- Download the program
L04h1.java
.
Do not compile or run the program (yet). The program uses the
length method.
Look up its meaning in Appendix M under the
String
class. The term "current object" in the book's description
refers to the object that
length
is being applied to, in this case the variable
sample.
- "Hand trace" the program. That is, act as if you were a computer and
execute each statement in the program like a computer would. Draw storage
boxes on a piece of paper to simulate variable/constant storage containers.
- Write the output your hand tracing produces on your paper.
- Compile and run the program. Compare your "expected result" with
the "actual result".
- For each line where the "expected result" differed from the "actual result",
do you understand why your prediction was wrong? If you cannot figure it out
so you know for sure, ASK!
- Correct the second output statement so blanks are printed around the 55.
Recompile and run.
Lab 04h, Part 2, Integer and Rational Operations
- Download the program
L04h2.java
.
- Compile the program. One line has a syntax error because it attempts
to store a
float
value in an
int
variable.
Correct the error by converting the value to be assigned to an
int.
- After you have gotten the program to compile and run, modify
it by adding a
printf statement for
each assignment statement after the first.
The
printf
after each assignment should print out the value of the first operand,
a space, the operator used in the calculation, a space, an equals sign,
a space, and the value of the variable which received the result.
Thus the additional
printf
statements will be similar to the first but the format string
must be adjusted
to use the appropriate
format
codes (
%d,
%f,
or
%s)
and to specify the appropriate operator.
For those lines performing the
"mod" (remainder) operation,
the
printf
format string must output a percent sign as the operator.
But like the backslash character has a special meaning in any
character string, the percent character has a special meaning
in
printf
format strings.
Java uses two backslashes to specify the string should
contain one backsplash. Similarly, two percents in a
printf
format string
tell
printf
to output a single percent.
- When you get the program working correctly, submit it.
You do not need to generate a printout from submit, just the
electronic submission is needed for this lab.
- Modify the program by moving all the
printf statements so they follow the last
assignment statements. Recompile and run your program. Has its output
changed at all?
Date: 09/21/05