CSCI 241 Labs: PreLab 4
Think Before You Leap


Introduction

Way too often in CSCI 241 students think that working on a project or lab means typing things at the computer. In truth, professional programmers only spend about 25% of their time coding. Most of the rest of the time is spent designing and testing their systems.

The types of design decisions programmers make fall into two basic categories:

 

Pseudocode

A standard technique for documenting the design of a method is pseudocode. (Pseudocode is defined in your text in Chapter 2 on page 34.) The purpose of pseudocode is to document your thoughts about the logic needed for a task before you ever sit down at the computer. It documents the algorithm without worrying about the syntax of a particular language.

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:

 

Examples

Consider the problem that instructors face every semester: assigning letter grades to students based on their performance on a series of exams. In our case, let's assume there are three exams which all count equally. A program to process the data would require at least two sections: one to calculate the average and one to assign a grade based on that average. Here is pseudocode for each:
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?

 

A Range of Correct Solutions

It is important to realize that in many problems the algorithm is not unique. In the above example, we could written this alternatively:

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:

  1. Changed the order in which the grade ranges are checked.
  2. Removed local variable grade.
  3. Inserted additional print statements.
The algorithm is still completely correct.

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.