The types of design decisions programmers make fall into two basic categories:
In chapter 6, we will learn how to write code with separate methods. In chapter 9, we will learn how to write our own separate classes.
Chapter 3 discusses how a computer program makes decisions. This can greatly complicate your code. Thinking clearly about (and writing down) what the code should look like before you jump in and start typing is critical.
Here is the Merriam-Webster Dictionary definition of an algorithm:
a procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation; broadly : a step-by-step procedure for solving a problem or accomplishing some end especially by a computer.
Aside: In real world software development, the design of the system is considered an important piece of documentation even after the system is completed. When a programmer has to make a change to a method, say to fix a Y2K bug or make the system work under another operating system, she usually starts by reading through the design documentation for the method.
There are just a few rules for writing pseudocode:
calculateAverage section average = (exam1 + exam2 + exam3) / 3 print average end calculateAverage section
Note when looking at the above pseudocode that many of the details are missing. It does not include variable declarations, because the data types are language-specific. It does not indicate how the numbers for each exam score are collected by the program. They could come from standard input, a GUI input dialog box or read from a file. It is left for the reader to figure out that the average should be a double. The division is by three, where you should know that here we would want 3.0 when writing the method in Java. When the calculated average is printed, it will probably include text explaining the meaning of the number. Do you see how to translate the above pseudocode into Java code?
Here is the other section's pseudocode:
assignGrade section if average >= 90 grade = 'A' else if average >= 80 grade = 'B' else if average >= 70 grade = 'C' else if average >= 60 grade = 'D' else grade = 'F' print grade end assignGrade section
Pay particular attention to the indentation in this example. Note that the print statement is at the same level of indentation as the if..else. This means the print is not inside the if block.
What will be returned if the score entered is 50? 87? 90? Do you see how to translate the above pseudocode into Java code?
assignGrade section if average < 60 print 'F' else if average < 70 print 'D' else if average < 80 print 'C' else if average < 90 print 'B' else print 'A' end assignGrade section
We have:
When we are checking for equality against a range of values, we can also frequently choose between using an if or a switch statement.
There are many correct algorithms for some problems. The differences between them are often trivial and irrelevant. Sometimes the biggest difference between them is in how fast they run. We will look at the efficiency of algorithms later this semester and give it a more complete treatment in CSCI 242 and CSCI 340.
In this week's lab you will be asked to create pseudocode for algorithms to solve particular problems and then implement the algorithms in Java.