8. Java Data and Operators

8.9. Problem Solving = Representation + Action

 

As you have seen in this chapter, designing classes involves a careful in- terplay between representation (data) and action (methods). Our several modifications to the OneRowNim class illustrate that the data used to rep- resent an object’s state can either complicate or simplify the design of the methods needed to solve a problem.

We hope that it is becoming clear to you that in writing object-oriented programs, choosing an appropriate data representation is just as impor- tant as choosing the correct algorithm. The concept of an object allows us to encapsulate representation and action into a single entity. It is a very natural way to approach problem solving.

If you look closely enough at any problem, you will find this close re- lationship between representation and action. For example, compare the task of performing multiplication using Arabic numerals—65 * 12 = 380— and the same task using Roman numerals—LXV * XII = DCCLXXX. It’s doubtful that our science and technology would be where they are today

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 5.12: A Java program illustrating character conversions. When run, the program will generate the following outputs, one per line: a, 98, b, A, B, 7.

 

if our civilization had to rely forever on the Roman way of representing numbers!

 

 

 

CHAPTER SUMMARYTechnical Terms

action

binary operator binary digit (bit) boundary value cast operation class constant

input-process-output


 

named constant operand

operator overloading precedence order promotion

round off error


 

short-circuit evaluation

type conversion unary operator Unicode representation

 

 

Summary of Important Points

 

CHAPTER 5 Chapter Summary239

The way we approach a problem can often help or hinder us in our ability to solve it. Choosing an appropriate representation for a problem is often the key to solving it.

In order to evaluate complex expressions, it is necessary to understand the precedence order and associativity of the operators involved. Paren- theses can always be used to override an operator’s built-in precedence. Java provides several types of integer data, including the 8-bit byte, 16-bit short, 32-bit int, and 64-bit long types. Unless otherwise specified, integer literals are represented as int data in a Java program. Java provides two types of floating-point data, the 32-bit float type and the 64-bit double type. Unless otherwise specified, floating-point literals are represented as double data.

In general, if a data type uses n bits in its representation, then it can

represent 2n different values.

The fact that Java’s primitive types are defined in terms of a specific number of bits is one way that Java promotes platform independence.

It is necessary to distinguish integer operations from floating-point op- erations even though the same symbols are used. For example, (7/2) is 3, while (7.0/2) is 3.0.

In revising a class that is used by other classes it is important to pre- serve as much of the class’s interface as possible.

In Java, character data are based on the Unicode character set, which provides 216 = 65,536 different character codes. To provide backward compatibility with the ASCII code, the first 128 characters are the ASCII coded characters.

Java operators are evaluated according to the precedence hierarchy shown in Table 5.15. The lower the precedence number, the earlier an operator is evaluated. So the operators at the top of the table are evalu- ated before operators that occur below them in the table. Operators at the same precedence level are evaluated according to their association, either left to right (L to R) or right to left (R to L).

 

TABLE 5.15 Java operator precedence and associativity table

 

 

 

 

 

 

 

 

 

 

 

 

 

&&Boolean ANDL to R

10Boolean ORL to R

11= += -= *= /= %= Assignment operatorsR to L

 

 

 

EXERCISES

Note: For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.

 

 

 

 

 

 

 

 

 

Chapter 6