Program Logic

Unit 5 Index

Solution Algorithms

5-Step Solution Algorithm

Algorithms in computer programming are the small, manageable, and logical pieces of code we write in order to solve a problem. If we correctly tell a computer to do something - the computer does it quickly and correctly every time. Writing algorithms is a part of the process we go through in software development. This chapter explores the 5-step Solution Algorithm process which we can use to create working robust computer programs.

Pseudocode

Words that convey your programming thoughts is called pseudocode. Some instructors think there should be strict guidelines for writing pseudocode. I do not, because then you worry about using the correct words instead of just thinking through the needed processes. Instead, I recommend using regular words to convey your programming thoughts. That being said, this chapter does outline some Java-like syntax guidelines that will make turning your pseudocode into Java code easier to do. I am convinced that “If you can say it, you can code it!” Pseudocode helps you say it.

Defining the Problem

Writing a program is all about solving a problem. In class, your assignment requirements represent the problem statement. In industry, end-user requirements are the problem statement. The key to solving the problem is carefully reading, understanding, and breaking-down the requirements into small, manageable, and logical steps.

Below is a problem statement that describes what a program needs to do.

A program is required that asks the end-user to enter three numbers. You will need to ask the end-user three separate times for a number. Add these numbers together and print a statement that includes the total value of the numbers.

Inputs, Processes, and Outputs

After carefully reading a problem statement, we identify three things:

Identify the Nouns (Step 1)

The first step is to identify the relevant nouns in the problem statement. Nouns are candidates for inputs and outputs. Inputs and outputs translate to needed variables. Sometimes it helps to include adjectives as they further clarify the problem statement.

We can see from our problem statement the nouns are numbers and total. However, we need to read carefully to reveal that we need three numbers. The numbers will be our inputs and total represents of our output.

Identify the Verbs (Step 1 continued)

Next, we identify the verbs and adverbs, which indicate processing that needs to be done in code. Remember, verbs convey an action. In our problem statement our relevant verbs are ask, read, add, enter, and print. These verbs help us determine the processing that the program needs to do. As we create larger programs, processing may be represented in several methods.

Creating a Defining Diagram a.k.a. IPO Chart (Step 2)

Once you identify nouns and verbs, you have a very good idea of the inputs, outputs, and basic processes needed to solve the problem. The next step is to organize inputs, processes, and outputs into a chart that we call a Defining Diagram or IPO Chart. There are several ways that programmers draw Defining Diagrams; however, for simplicity we write our diagrams in a plain text format.

For our problem statement, A program is required that asks the end-user to enter three numbers. You will need to ask the end-user three separate times for a number. Add these numbers together and print a statement that includes the total value of the numbers, we need three inputs, and one output. Furthermore, the inputs are gathered from the end-user.

Please review my defining diagram listed below. During the defining diagram step, it is okay to keep all the inputs, outputs, and processes at a high level. They get refined in the Solution Algorithm step.

INPUTS:
  first number
  second number
  third number

PROCESSING:
  ask the end-user to enter a number
  read and save the first number into the variable for first number
  ask the end-user to enter a number
  read and save the second number into the variable for second number
  ask the end-user to enter a number
  read and save the third number into the variable for third number
  add all three numbers together and save the value into the variable for total
  display a statement that includes total value

OUTPUTS:
  total

Developing a Solution Algorithm (Step 3)

Next, we design a detailed Solution Algorithm. For the Solution Algorithm step, I suggest using variable and method names that adhere to the PL Development Standards. If you include enough details in this step, then writing the actual Java code, should go smoothly. These details can also be used as comments in your code.

At this point, you may be saying: “I bet people in industry do not create solution algorithms”. However, here you would be wrong. In industry we usually do not write all the steps for an entire program, however, when the coding gets tough, we write solution algorithms for the same reasons you should: to map out processes and get our thoughts together without worrying about specific syntax.

It is okay to include some Java-like syntax in your solution algorithms. However, do not paste Java code into this section. Remember, you are supposed to write the Solution Algorithm steps BEFORE you write the Java code. Start writing your solution algorithm by first reviewing your defining diagram.

The Constructor

First define the constructor. In this course, your constructor always has public access. When writing a solution algorithm, the definition or signature of your constructor is:

public Constructor()

When you write the Java code for your algorithm, the constructor must have the same name as the Java class you put the constructor in, and for us, that class is always named Controller. In your solution algorithms, it is okay to define your constructor like this: public Controller()

Remember our standards, a Java class name should start with an uppercase letter. Since the class and constructor must have the same name, the constructor name must also start with an uppercase letter. Notice the definition or signature of the constructor includes the parentheses. These are necessary in Java, so including them in your solution algorithm helps to remember they are needed in code.

Remember, Java is case sensitive!

Next you need to indicate the body of the constructor. This is done with curly braces. I always type an open curly brace ( { ) and then immediately type a closing curly brace ( } ) that way I do not forget to close the method body. I then press enter to make an empty line. This helps me to keep my code inside the body of the constructor. The body says “the lines of code within my curly braces belong together”.

public Constructor(){

}

Remember, a constructor contains the necessary code to build a class. In our case, we just need to include a call the method that kicks off our processing. Give the method a name that briefly describes what its contents will do. Remember to start the method name with a verb and camel case its name. It is also wise to include parentheses, and then a semicolon. This just helps you to remember that in Java, methods require parentheses and when you call a method, you end the statement with a semicolon.

public Constructor(){
   call the method addThreeNumbers();
}

The Method

Your method directly corresponds with one or more of the processes you identified in the defining diagram. In Program Logic, we always us the access modifier private with our methods. Include the word method so you get familiar with what you are writing. The name of your method is the same as the call to it. In our case, that is addThreeNumbers(). Just like with the constructor, include the parentheses and curly braces. This just helps you to remember that they are required in Java. The curly braces denote the body of your method. The word method just helps to remember that is what is being created. Including private helps you to remember it is needed in your Java methods. Notice when writing your method, you do not use a semicolon.

private method addThreeNumbers(){

}

So far we created a constructor, included a call to our processing method, and started writing the method. Collectively, they comprise our solution algorithm. Please review the partial solution algorithm belong. It is partial because we have not yet included all the processing steps to solve the problem. Notice that the code in the constructor body is indented. Indenting code makes it easier to read.

public Constructor(){
   call the method addThreeNumbers();
}

private method addThreeNumbers(){
   //processing steps go here
}

The Method Body - Processes

Now for the body of the method...

Each of the steps relates directly to statements in the PROCESSES section of the defining diagram, however, these steps, should be more detailed. Details will make the Java coding part easier!

Be sure to put the processing steps in the correct sequence, because, ORDER MATTERS!

Please carefully review my processing example below. Remember, slash slash denotes a comment. Feel free to add comments to your solution algorithms.


private method addThreeNumbers(){

  declare and initialize double firstNumber, it will hold end-user entry
  declare and initialize double secondNumber, it will hold end-user entry
  declare and initialize double thirdNumber, it will hold end-user entry
  declare and initialize double total, it will hold the output
  
  instantiate the Scanner object using input as its variable

  print to the console "Please enter the first number."
  save the input.nextDouble() value into the firstNumber variable
  //Note, input.nextDouble() reads the end-user input
    
  print to the console "Please enter the second number."
  save the input.nextDouble() value into the secondNumber variable
  //Note, input.nextDouble() reads the end-user entry
  
  print to the console "Please enter the third number."
  save the input.nextDouble() value into the thirdNumber variable
  //Note, input.nextDouble() reads the end-user input
  
  using the variable names, add the numbers and save into the variable total 
  //this can be done in one statement: 
  //total = firstNumber + secondNumber + thirdNumber;
  
  print a statement that includes the total value
  
}

Writing a Test Plan (Step 4)

If you want to create programs that end-users want to use, then thoroughly testing your application is essential. I always test an application with data that I know works as well as data that I know will cause the application to fail. When you get into industry, testing may be automated using a comprehensive test plan. For our purposes, your test plan should consists of these steps.

  1. Choose 3 or more simple input test cases.
  2. One test case must cause your program to fail. This means you must consider your inputs and outputs before designing your test plan.
  3. Write the relevant variables in the testing section of the project.
  4. Establish what the expected result should be for each test case.
  5. Repeat this for all your test data.

When you write the Java code for your solution algorithm, make sure you use these test cases.

Below is an example of a test plan for our solution algorithm. Notice the test plan uses the variable names and statements for the expected results. Please study the test plan below.

TEST PLAN:

VARIABLES: firstNumber, secondNumber, thirdNumber, total

TEST CASE 1:

  INPUT VALUES: 10, 20, 30

  EXPECTED RESULT: The program will run. The output should be
  the result of 10 + 20 + 30

TEST CASE 2:

  INPUT VALUES: 40.99, 41, 42.5

  EXPECTED RESULT: The program will run. The output should be
  the result of 40.99 + 41 + 42.5
  
TEST CASE 3:

  INPUT VALUES: 40, Z, 42

  EXPECTED RESULT: The program will fail when "Z" is entered because a string cannot be used in a math calculation. The end-user will not be asked to enter the third number.

Just to clarify...you will be writing test plans for future assignments. Testing your code is extremely important. Your end-user should not be the first tester of your application. A test plan should be part of the design on all your projects. A test plan helps you to think through the various ways your program will work and to discover things you never thought about before. This process helps you to understand code and anticipate errors so you can develop code that gracefully handles the errors.

Never let your end-users be your only testers!

IMPORTANT: This algorithm does not have statements to validate the end-user input. However, validation is an extremely important part of coding. You will be required to validate end-user input in future assignments.

Translating the Solution Algorithm to Java Code (Step 5)

Remember, the more detail you use in your solution algorithms, the easier it is to translate them into Java source code. Also remember, the intention is to do the solution algorithms BEFORE you attempt the Java.

Whenever your write code, write it incrementally. That means, write just enough to test your code with good data and data that will make if fail. Once you are confident your code is working, then you can add to your code and test again.

What You Learned

What's next?

The next chapter in this unit is: Methods Overview