The primary problem with trial and error problem solving is that this approach can take too long for

“Aaaaaaggggghhh!, Another error! I can’t get this program to work… let me change this one variable and see… wait… wait a minute…  yes!!! It works!!! It works!! It works!!!”

Does the above quote sound like you? Have you said something like that during the course?

Making mistakes and fixing them is an integral part of computer programming and computer science. There is actually a problem-solving method that is called trial and error, and there are methods and techniques you can use to get better at trial and error and computer programming in general.

Trial and Error

Trial and error involves continuously testing possible solutions until you achieve the correct outcome.

Trial and error is a great problem-solving method in computer programming, because the cost of failure is very low. However, trial and error doesn’t work well in situations where the cost of failure is high.

Read over the above statement again. What does “cost of failure” mean? Perhaps it might be good to think of it this way:

  • Would you want a surgeon to try out various methods or techniques until she gets it right?
  • Would you want a government to develop, vote on, and then enact new policies and laws until they get it right?
  • Would you want a bridge builder to develop and build different designs, then let people try out the bridge until he gets it right?

Trial and error is a great problem-solving strategy when each trial doesn’t cost very much money and doesn’t take very much time. Oh, and of course it’s good when it doesn’t cost anyone their life.

In computer science, each trial involves changing a bit of code. Because of this, the cost in terms of time and money (and lives) is very low.
 

Did You Know?

One important thing to note is that errors and trials are cheap in the beginning stages of the Software Development Life Cycle (SDLC), but not in the later stages.

When software is in the development stage, a programmer can quickly alter some code and the cost in time and money is low. But if the software is just about to go out to the customer, or if it is already in the hands of the customer, the cost of fixing an error is quite high. Some refer to this as the Rule of Ten, where an error costs ten times more to fix at each subsequent stage of the SDLC.

Iteration

Iteration is a term that is often associated with computer programming and trial and error. Iteration means to continually try out solutions, refining each one, and getting closer to the end goal with each attempt. A major component of iteration is that you are attempting to get closer and closer to success with each attempt.

If you are a person that likes doing things perfectly the first time, then computer programming and computer science might be a little tricky.

Trial and error and iteration are very important in the development of software. People in this field get used to making mistakes most of the time; they just make sure that they don’t get discouraged and that their program keeps improving after each run.

Errors in computer programs can often be classified as one of three types:

  • Syntax Error
  • Run-Time Error
  • Logic Error

A syntax error occurs when you make a mistake in how the code is written. Misspelling a variable name or a function name after they are correctly declared, forgetting a semicolon, or forgetting a curly bracket at the end of a loop are examples of syntax errors. These errors are usually “caught” by the compiler and therefore are also referred to as "compile time errors."

A run-time error occurs when your program crashes. Dividing by zero or trying to open a file that doesn’t exist, causing your program to crash, are both examples of run-time errors.

A logic error occurs when your program runs but the output is incorrect. For example, when calculating the area of a circle given the radius by the user, if you calculate: Area = 3.14 * radius * 2, instead of Area = 3.14 * radius * radius, your program will still work, but the answer is incorrect.

Sometimes logic errors cause run-time errors as well. For example, if you code a loop that should stop when the counter reaches a certain number, but you forgot to increment the counter, this loop will never stop. This is also called an infinite loop because it loops forever. This is an example where your logic error causes a run-time error.

The following programs contain errors of all different types:

The primary problem with trial and error problem solving is that this approach can take too long for

This program above will not be compiled because every line of code in this program contains a syntax error. Can you figure out how to fix each one?

This program outputs the variable num1 to the screen instead of outputting the variable sum. This program will compile, but it won’t generate the correct output and so it is therefore a logic error.

The primary problem with trial and error problem solving is that this approach can take too long for

This program asks the user to enter his or her age and then stores the inputted data as an integer variable. The programmer forgot that anything read from the command line is read as a string. Therefore, it has to be converted into an int before being assigned to the variable age. This is an example of a run-time error.

This programmer has used commas instead of semi-colons in the for loop. This is an example of a syntax error that is caught by the compiler.

Can you spot the logic error in this program? Take a close look at the if statement. Does this make sense, that if your age is “less than or equal to 16” then you are old enough to get your driver’s license? This program will run, but the output generated will be logically incorrect.

The primary problem with trial and error problem solving is that this approach can take too long for

This program is supposed to output the quotient of 12 divided by 4, 3, 2 and 1. Unfortunately, the programmer made the loop run when the divisor is equal to 0. This means 12 will be divided by 0, which is a big no-no causing the program to crash! This is an example of a run-time error.

Debugging

Errors in programs are often called bugs. You may have found yourself spending a lot of time removing these bugs (a process programmers like to call “debugging”).

Did You Know?

In 1945, programmer Grace Hopper was trying to determine the problem with the Mark II computer that she was working on. After close inspection, she found an actual moth inside that was preventing electrical signals from passing through a relay.

She removed the moth, taped it to the error log book and wrote: “First actual case of bug being found.”

Some people believe that this was the first time the term “bug” was used to indicate an error or malfunction. In fact, the term bug had been used long before this.

Thomas Edison used the term as the Oxford English Dictionary quotes a newspaper from 1889: “Mr. Edison, I was informed, had been up the two previous nights discovering 'a bug' in his phonograph.”

You can read more about it at the Carnegie Mellon School of CS and Computerworld.

Bugs are an inevitable part of programming. It is very difficult to write code that runs for the first time bug-free. Fortunately, there are some things that you can do to prevent bugs from occurring, and also ways you can efficiently get rid of bugs in your code.

Proper Program Design

Taking the time to properly plan and design your program is an important first step in limiting the bugs that your program will have. You have already looked at how flowcharts can help with the design of programs, and you have practiced breaking down your program into subroutines, which can be very helpful as well.

Another planning tool that can be used is called top-down design.

Top-down Design

Top-down design is a “solution method where the problem is broken down into smaller sub-problems, which in turn are broken down into smaller sub-problems, continuing until each sub-problem can be solved in a few steps.”

~ Virginia Tech - Introduction to Programming in C++

As you can tell, you have done some top-down design when you designed and created programs with methods. A main advantage of top-down design is that you divide the problem into smaller and smaller pieces, and therefore each piece becomes easier and easier to solve.

"The subdivision of subproblems should continue until a subproblem can easily be solved. This is similar to the refining of an algorithm. In large projects, subproblems may be assigned to different programmers, or teams, who will be given precise guidelines about how that subproblem fits into the overall solution, but who will have relatively complete freedom in attacking the particular subproblem at hand."

~ Virginia Tech - Introduction to Programming in C++

In some of the programs you create from now on, you are going to create structure charts which will allow you to

  • visualize a top-down design of the program.
  • understand each subroutine involved in the program.

A structure chart can be made with word processing software, or with the same software that you use to create your flowcharts.

The following is a structure chart for a modified game of blackjack or 21. Notice how the programmer divided the tasks into input, process and output. The programmer can now see the variety of smaller subroutines that will make up the large program.

Having Resources at Hand

Very few programmers program without referring to books, reference materials, past programs, or online tutorials and discussion forums. Some programmers believe that it’s not important to memorize every single line of code or command. Instead, it’s just important to be aware that they exist and to understand how and why each need to be used. If you can do this, then when it comes time to use the code or command, then you can quickly check resource materials to ensure proper syntax.

As you continue to create more complex programs, make sure that you have resource materials close at hand. You may have already determined your favourite Java website or forum. Be sure to have quick links to them, or have them stored as favourites.

Make the Most Out of the Integrated Development Environment

The tools and features of the IDE are intended to make programming easier. Depending on the IDE, the programmer might get help with identifying and fixing syntax errors, or keeping track of the value of variables as the program is run. The IDE can also help manage projects and keep track of associated files. Whichever IDE you are using, it is helpful to get to know the tools available. This reference for the NetBeans IDE is very helpful.

Why is my code underlined in red?

Remember this short little program? There are four lines of code and each one contains a syntax error?

You have probably figured out by now that code underlined in red indicates an error. The NetBeans IDE not only underlines this error, but also provides hints that may be useful in fixing the error.

The hints can be seen by rolling your mouse over the error icons:

The primary problem with trial and error problem solving is that this approach can take too long for

NetBeans Code Assistance

NetBeans Code Assistance is a feature that helps you as you type and develop your code. There are a number of advantages to properly using Code Assistance, including the fact that lines of code can be completed for you, and hints can pop up to show you options of what you might want to include next in your code.

The NetBeans website provides a thorough description of the features included in Code Assistance.

Reading Error Messages

When an error is encountered in NetBeans, the programmer is given a few clues about the location of the error and what might be the likely cause of the error. It is very important to pay attention to this information and to use it to debug your programs.

The program below prompts users to enter their weight. The weight variable is declared as an integer, but the user enters “136.1”, which is a string. (Remember that anything read from the command line will always be a string. You, the programmer, will have to convert it into a different form, such as an int.)

The output message from NetBeans indicates the type of error and the location of the error.

The primary problem with trial and error problem solving is that this approach can take too long for

Generate Testing Output

In many of the programs you write, you only see the value of specific variables, and these are only seen at the end of the program. What some programmers find helpful is to output the value of variables during the running of the program. This output helps them determine what the program is doing, and to see how the program is behaving before it runs to completion. These output statements are then deleted when the program is debugged and it’s time to complete the project. This is called tracing.

The following video will provide you with an example of how generating test output can be helpful:

NetBeans Debugger

The NetBeans debugger allows you to execute a program one line at a time. As you do this, you can also see program components change, such as the values of the variables within the program.

There are three steps you need to follow to use the debugger:

  1. Set a breakpoint, or several breakpoints, in your program. A breakpoint is a point where the program will pause its execution.
  2. Instead of running the program normally, select Debug > Debug File.
  3. Step through your code line by line and take note of the variable values at each step.

The following video does a great job of explaining the debugger features in NetBeans.

Did You Know?

Professional programmers are always trying to make their programs as efficient as possible, both in terms of the storage space used by the program, and the time it takes to run and perform processes.

The NetBeans IDE provides something called a profiler that "can provide important information about the runtime behavior of your application. The NetBeans profiling tool easily enables you to monitor thread states, CPU performance, and memory usage of your application from within the IDE, and imposes relatively low overhead" (from Netbeans).

Java programs can have thousands of lines of code or more. The profiler is designed to optimize programs, but keep in mind that your programs are quite small and there won’t be much room for optimization. You can read more about the profiler here, and, if you like, you can even try using it as you create programs.

The debugging tips above relate to informal bugs that might come up as you write your programs. However, to create truly great software, developers don’t just deal with bugs that “come up” - they actively seek out ways to uncover bugs. In this way, they can guarantee that once the software is in the user’s hands, it will be bug-free.

Test Plan

A formal test plan includes the following:

  • aspects of the program that will be tested
  • the outcomes of those tests (either positive or negative, as well as details related to any errors)
  • any changes or alterations that were required to be made

The following document lays out some of the considerations when creating a formal test plan:

  • Software Testing Fundamentals
  • Software Testing Help
  • Test Plan Template: Would you like to fill out the 21 pages?

Test Data

One component of the test plan involves the specific test data. Imagine a program that calculates the average of four marks. The programmer would want to test a wide variety of marks to ensure that all different cases will work.

When it comes to test data, a good testing plan would include

  • specifying four marks.
  • using an alternative method of finding the expected output (like a reliable calculator, etc).
  • noting the expected output.
  • testing the program with the same marks.
  • comparing the output from the computer to the expected output.

This spreadsheet provides you with an example of how a test data document could be implemented. You can use this document as a template for your own test data documents.

As you select test data, make sure that you consider strange and different data that the user might enter. Examples might include the value 0, large numbers, negative numbers, decimal values or strings instead of numbers.

Logging Test Data

Another important part of the testing plan is making sure that data and testing information is logged accurately. In large software projects, a variety of people might consult the test plan log, so it’s important to keep the information clear and precise.

Data Validation

Data validation is a “check to ensure that the data entered is sensible and reasonable.”

If you are creating a program that prompts users for their age, you wouldn’t want them entering decimal values, or any value less than 0 or greater than say… 130.

It’s important, therefore, for your programs to check the validity of data before you start to store and process the data.

Do you remember this program from earlier in the activity?

The primary problem with trial and error problem solving is that this approach can take too long for

This program prompted the user to enter an integer, but the user entered a string which caused the program to crash.

One way to prevent this from happening is to use try…catch commands in Java.  The try…catch commands are pretty cool. Java will try to do something, but if it causes the program to crash, then that “problem” will be caught by the catch command and it will save the program. You can think of the try...catch command as sort of being a safety net, or a spotter, that double checks to see that everything is okay, and helps out if mistakes happen.

The primary problem with trial and error problem solving is that this approach can take too long for

The program below demonstrates a great way to implement the try…catch commands to validate data. The program continually loops until the user enters an integer:

The primary problem with trial and error problem solving is that this approach can take too long for

What is the trial and error approach to problem

Trial and Error A trial-and-error approach to problem-solving involves trying a number of different solutions and ruling out those that do not work. This approach can be a good option if you have a very limited number of options available.

Which problem

An algorithm is a strategy that guarantees that eventually one will arrive at a solution if the strategy is correctly applied.

What is meant by trial and error learning?

a way of achieving an aim or solving a problem by trying a number of different methods and learning from the mistakes that you make: There's no instant way of finding a cure - it's just a process of trial and error.

When trying to solve a problem it is best to?

Six step guide to help you solve problems.
Step 1: Identify and define the problem. State the problem as clearly as possible. ... .
Step 2: Generate possible solutions. ... .
Step 3: Evaluate alternatives. ... .
Step 4: Decide on a solution. ... .
Step 5: Implement the solution. ... .
Step 6: Evaluate the outcome..