Page 60 - Introduction to Programming with Java: A Problem Solving Approach
P. 60

                26 Chapter 2 Algorithms and Design
results in bad programs that work poorly and are hard to fix because poor organization makes them hard to understand. Therefore, for all but the very simplest problems, it’s best to start by thinking about what you want to do and then organize your thoughts.
As part of the organization process, you’ll want to write an algorithm.1 An algorithm is a sequence of instructions for solving a problem. It’s a recipe. When specifying an algorithm, two formats are common:
1. The first format is a natural-language outline called pseudocode, where the prefix “pseudo-” means “fictitious or pretended,” so it’s not “real” code. Pseudocode, like real code, is composed of one or more statements. A statement is the equivalent of a natural language “sentence.” If the sentence is simple, the corresponding statement usually appears on one line, but if the sentence is complex, the statement may be spread out over several lines. Statements can be nested inside each other, as in an outline. We’ll use the term “statement” a lot, and you’ll get a better appreciation for it as we go along.
2. The second format is an arrangement of boxes and arrows that help you visually step through the algo- rithm. The most detailed form of boxes and arrows is called a flowchart. The boxes in a flowchart typi- cally contain short statements that are similar to pseudocode statements.
This chapter shows you how to apply pseudocode and flowcharts to a fundamental set of standard pro-
gramming problems—problems that appear in almost all large programs. The chapter also shows you how to trace an algorithm—step through it one statement at a time—to see what it’s actually doing. Our goal is to give you a basic set of informal tools which you can use to describe what you want a program to do. The tools help you organize your thinking before you start writing the actual program. Tracing helps you figure out how an algorithm (or completed program) actually works. It helps you verify correctness and identify problems when things are not right.
            Apago PDF Enhancer
2.2 Output
The first problem to consider is the problem of displaying a program’s final result—its output. This may sound like something to consider last, so why consider it first? The output is what the end user—the client,
        Put yourself in user’s place.
the person who eventually uses the program—wants. It’s the goal. Thinking about the out- put first keeps you from wasting time solving the wrong problem.
 Hello World Algorithm
In Chapter 1, we showed you a Java program that generated “Hello, world!” output on the computer screen. Now we’ll revisit that problem, but focus on the algorithm, not the program. You may recall that Chapter 1’s Hello World program was seven lines long. Figure 2.1 shows the Hello World algorithm—it contains just one line, a pseudocode print statement. The point of an algorithm is to show the steps necessary to solve a problem without getting bogged down in syntax details. The Hello World algorithm does just that. It shows a simple print statement, which is the only step needed to solve the Hello World problem.
Figure 2.1’s “Hello, world!” message is a string literal. A string is a generic term for a sequence of characters. A string literal is a string whose characters are written out explicitly and enclosed in quotation marks. If you print a string literal, you print the characters literally as they appear in the command. So Fig- ure 2.1’s algorithm prints the characters H, e, l, l, o, comma, space, w, o, r, l, d, and !.
1 Ninth century Persian mathematician Muhammad ibn Musa al-Khwarizmi is considered to be the father of algebra. The term algo- rithm comes from Algoritmi, which is the Latin form of his shortened name, al-Khwarizmi.
 


















































































   58   59   60   61   62