Programming Fundamentals Essay

Submitted By nisagrl
Words: 5217
Pages: 21




Chapter 2: Programming Fundamentals

Self-review Questions
2.1 Explain the meaning of piece.forward();. Why is the semicolon present?
The object referred to by piece is instructed to move forward. Actually, to be more precise: the method forward is called for the object referenced by piece. The actual movement made by the piece depends on how it interprets an invocation of the method forward—in the examples given in the chapter a piece will move one square forward, but different kinds of piece object could move differently. The dot associates the method call (forward) with the object that will undertake the method (the object referred to by piece), while the parentheses (()) cause the method call to take place.
The semicolon is a bit of syntax to mark the end of the method call statement. It can also be seen as a separator, separating this statement from the next. One reason for having the semicolon present is to simplify the implementation of the Java compiler by precisely marking the end of a statement. The semicolon also improves the readability of the code for people and can help remove ambiguities that otherwise might occur in more complicated sections of code. The semicolon in Java is directly analogous to the use of full stop in natural language, it marks the end of a sequence of syntactic features.

2.2 How many different interpretations of the following statement sequence are there? piece.forward ( ) ; piece.forward ( ) ; piece.right ( ) ; piece.forward ( ) ; piece.left ( ) ;

The result of executing these statements depends on the behaviour of the methods called.
Hence, the number of interpretations depends on the number of different versions of the methods—at the extreme, the number of interpretations is infinite (for example, a new version of forward can always be created that moves one square more than the previous version).
In practice, of course, the behaviour of each method will be fixed for a given program, so there will only be a single interpretation of the statement sequence. This makes the statement sequence deterministic, meaning that it will always perform the same movement actions when executed. The location of where the piece ends up will depend on its starting position.

2.3 Write down a sequence of statements that describes making a telephone call. Can it be done without using control statements?

Self-review Questions

One of the simplest sequences might be:
1. Pick up handset.
2. Dial number
3. Speak
4. Hang-up
This requires no control statements but is not robust nor particularly realistic. For example, it assumes the number being called is not engaged and that the conversation is limited to speaking a message and then hanging up. As soon as we want to manage errors or exceptions, not to mention actual human interaction (!) then control flow features are required.

2.4 Does the pseudocode below (from Section 2.5, Page 23) describe a general-purpose solution to starting from and moving to any square on a chessboard? while ( not at destination ) if ( destination is to the left ) piece moves left else if ( destination is to the right ) piece moves right; piece moves forward

The way to approach this question is to attempt to find a start position that does not lead to a solution which will immediate show that the algorithm is not a solution to the problem. Pencil and papers sketches can be used to try out ideas, while working with a small-sized board, say
5×5, would help speed up the search.
Following this approach, you would hopefully notice that the algorithm has a serious flaw—the piece can move forward, left or right but not backwards. Hence, for example, if the destination square is behind the starting square the forward direction of travel is away from the destination and the destination cannot be reached. Moreover, once the piece reaches the edge of the board the algorithm fails, as the