Page 45 - Introduction to Programming with Java: A Problem Solving Approach
P. 45
Programming Languages
A programming language is a language that uses specially defined words, grammar, and punctuation that a computer understands. If you try to run pseudocode instructions on a computer, the computer won’t under- stand them. On the other hand, if you try to run programming language instructions (i.e., source code) on a computer, the computer will understand them.
Just as there are many spoken languages in the world (English, Chinese, Hindi, etc.), there are many programming languages as well. Some of the more popular programming languages are VisualBasic, C, and Java. Each programming language defines its own set of syntax rules. In this book, we’ll focus on the Java programming language. If you write your program in Java, you must follow Java’s syntax rules pre- cisely in terms of words, grammar, and punctuation. If you write Java source code using incorrect syntax (e.g., you misspell a word or forget a semicolon), and you try to run such source code on a computer, the computer won’t be able to understand it.
Example—Using Java to Find Average Miles Per Hour
Continuing with the earlier example where you wrote pseudocode to find the average miles per hour value for a given car trip, let’s now translate the pseudocode into Java source code. In the table below, the pseudo- code at the left translates into the Java source code at the right. Thus, the first two pseudocode instructions translate into the single Java source code instruction at their right.
1.4 SourceCode 11
Pseudocode
Java Source Code
Apago PDF Enhancer
Calculateendinglocationminusstarting distanceTotal = locationEnd - locationStart; location. Put the result in total distance.
Calculate ending time minus starting time. Put the result in total time.
timeTotal = timeEnd - timeStart;
Divide total distance by total time.
averageMPH = distanceTotal / timeTotal;
Programmers normally refer to Java source code instructions as Java statements. For Java statements to work, they must use precise syntax. For example, as shown above, Java statements must (1) use a - for sub- traction, (2) use a / for division, and (3) have a semicolon at their right side. The precision required by Java statements contrasts with the flexibility of pseudocode. Pseudocode allows any syntax, as long as it is un- derstandable by a person. For example, in pseudocode, it would be acceptable to represent subtraction with a - or the word “subtract.” Likewise, it would be acceptable to represent division with a / or a or the word “divide.”
Skipping the Pseudocode Step
Initially, programming language code will be harder for you to understand than pseudocode. But after gain- ing experience with a programming language, you may become so comfortable with it that you’re able to skip the pseudocode step entirely and go right to the second step where you write the program using pro- gramming language code.
For larger programs, we recommend that you do not skip the pseudocode step. Why? Because with larger programs, it’s important to first focus on the big picture because if you don’t get that right, then noth- ing else matters. And it’s easier to focus on the big picture if you use pseudocode where you’re not required