Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


86


This application does exactly what the original version did, yet it is longer. In some
ways it seems like a waste to make this extra effort. The advantage, however, is that
now we have a Nameclass that we can use in other applications. We also have the ability
to instantiate multiple names just by adding declarations and constructor calls to the
application. To add another format, we could simply write another method as part of
the class and add another statement to the driver. The object-oriented version is much
easier to reuse, to maintain, and to extend. The original version had fewer lines of code,
but was monolithic in its construction, and thus solved one specific problem. The
object-oriented version is divided into useful pieces that can be applied to solve many
different problems. We’ve done a little more work now, but have potentially saved much
more work in the future.

2.5 Testing and Debugging


Testing and Debugging Hints

1.You must declare every identifier that isn’t a Java reserved word. If you use a name that hasn’t
been declared, you will receive an error message.
2.If you try to declare an identifier that is the same as a reserved word in Java, you will receive an
error message from the compiler. See Appendix A for a list of reserved words.
3.Java is a case-sensitive language, so two identifiers that are capitalized differently are treated as
different identifiers. The word mainand all Java reserved words use only lowercase letters.
4.Check for mismatched quotes in charand Stringliterals. Each charliteral begins and ends with
an apostrophe (single quote). Each Stringliteral begins and ends with a double quote.
5.Use only the apostrophe (') to enclose charliterals. Most keyboards include a reverse apostrophe
(') that is easily confused with the apostrophe. If you use the reverse apostrophe, the compiler
will issue an error message.
6.To use a double quote within a literal string, use the two symbols \”in a row. If you use just a
double quote, it ends the string, and the compiler then sees the remainder of the string as an er-
ror. Similarly, to write a single quote in a charliteral, use the two symbols \’without any space
between them (that is,'\''is the charliteral for a single quote).
7.In an assignment statement, make sure that the identifier to the left of =is a variable and not a
named constant.
8.In assigning a value to a Stringvariable, the expression to the right of =must be a Stringexpres-
sion or a literal string.
9.In a concatenation expression, at least one of the two operands of +must be of the class String.
Free download pdf