This chapter goes into more depth about relational and logical operators. You will have to use these concepts to write complex programs that other people can read and follow.
9. Problem Solving = Representation + Action
As you have seen in this chapter, designing classes involves a careful interplay between representation (data) and action (methods). Our several modifications to the OneRowNim class illustrate that the data used to represent 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 important 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 relationship 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 if our civilization had to rely forever on the Roman way of representing numbers!
public class Test {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.public s t a t ic void main ( S t ring argv [ ] ) {
char ch = ’ a ’ ; // Local variables
int k= ( int ) ’b ’ ;
System . out . prin tln ( ch ) ;
System . out . prin tln (k ) ;
ch = ( char )k; // Cast needed here
System . out . prin tln ( ch ) ;
System . out . prin tln ( toUpperCase ( ’ a ’ ));
System . out . prin tln ( toUpperCase ( ch ) ) ;
System . out . prin tln ( digitToInteger ( ’ 7 ’ ));
}
public static char toUpperCase ( char ch ) {
i f ( ( ch >= ’ a ’ ) && ( ch <= ’ z ’ ) )
return ( char ) ( ch 32);
return ch ;
}
public static int digitToInteger ( char ch ) {
if ( ( ch >= ’ 0 ’ ) && ( ch <= ’ 9 ’ ) )
return ch ’ 0 ’ ;
return 1 ;
}
} // Test
if our civilization had to rely forever on the Roman way of representing numbers!
JAVA EFFECTIVE DESIGN Representation and Action. Representation (data) and action (methods) are equally important parts of the problem-solving process. |
---|