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

                10 Chapter 1 Introduction to Computers and Programming Program Design
After the requirements analysis step, the second step is program design, where you write a draft of your program and focus on the basic logic, not the wording details. More specifically, you write instructions that are coherent and logically correct, but you don’t worry about missing minor steps or misspelling words. That sort of program is referred to as an algorithm. For example, a cake recipe is an algorithm. It contains instructions for solving the problem of baking a cake. The instructions are coherent and logically correct, but they don’t contain every minor step, like covering your hands with pot holders prior to removing the cake from the oven.
Pseudocode
In writing an algorithm, you should focus on organizing the flow of the instructions, and you should try to avoid getting bogged down in details. To facilitate that focus, programmers often write an algorithm’s instructions using pseudocode. Pseudocode is an informal language that uses regular English terms to de- scribe a program’s steps. With pseudocode, precise computer syntax is not required. Syntax refers to the words, grammar, and punctuation that make up a language. Pseudocode syntax is lenient: Pseudocode must be clear enough so that humans can understand it, but the words, grammar, and punctuation don’t have to be perfect. We mention this leniency in order to contrast it with the precision required for the next phase in a program’s development. In the next section, we’ll cover the next phase, and you’ll see that it requires perfect words, grammar, and punctuation.
Example—Using Pseudocode to Find Average Miles Per Hour
Suppose you are asked to write an algorithm that finds the average miles per hour value for a given car trip.
Apago PDF Enhancer
Let’s step through the solution for this problem. To determine the average miles per hour, you’ll need to divide the total distance traveled by the total time. Let’s assume that you have to calculate the total distance from two given locations. To determine the total distance, you’ll need to take the ending-point location, called “ending location,” and subtract the starting-point location, called “starting location,” from it. Let’s assume that you have to calculate the total time in the same manner, subtracting the starting time from the ending time. Putting it all together, the pseudocode for calculating average miles per hour looks like this:
Calculate ending location minus starting location. Put the result in total distance.
Calculate ending time minus starting time.
Put the result in total time.
Divide total distance by total time.
At this point, some readers might want to learn about a relatively advanced form of program de- velopment—object-oriented programming, or OOP as it’s commonly called. OOP is the idea that when you’re designing a program you should first think about the program’s components (objects) rather than the program’s tasks. You don’t need to learn about OOP just yet, and you’re not properly prepared to learn about OOP implementation details, but if you’re interested in a high-level overview, you can find it in Chapter 6, Section 2.
1.4 Source Code
In the early stages of a program’s development, you write an algorithm using pseudocode. Later, you translate the pseudocode to source code. Source code is a set of instructions written in a programming language.
 



















































































   42   43   44   45   46