Information Technology · Ch 2 — Introduction to Programming
Programming Guidelines — Modular Approach, Debugging and Problem Solving
Programming Guidelines — Modular Approach, Debugging and Problem Solving
Writing a working program is only half the job; writing one that is easy to read, test and correct is the other half. This is what programming guidelines are about.
The problem-solving procedure
Good programmers do not start by typing code. They follow a sequence:
- Understand the problem — what are the inputs, what output is wanted, and what are the rules connecting them?
- Design a solution — write the logic as an algorithm (numbered steps in plain language) or draw a flowchart (a diagram of the logic).
- Code — translate the algorithm into Java.
- Test — run the program with sample data, including tricky "edge" cases, and compare the output with the expected answer.
- Debug and refine — find and fix any errors, then test again.
Algorithm (find the larger of two numbers):
Step 1: Start
Step 2: Read A and B
Step 3: If A > B, then print A
Step 4: Otherwise print B
Step 5: Stop
Drawn as a flowchart, the logic runs: Start -> input A and B -> a decision box Is A > B? -> on Yes, print A; on No, print B -> both branches join at Stop.
The modular approach
Instead of one long main, split a program into small, self-contained modules — in Java these are methods. Each method does one well-defined job, can take parameters (inputs) and can return a result. Benefits: the code is easier to read, a method can be reused many times, and each piece can be tested on its own.
public class AreaDemo {
// a reusable module: it takes two inputs and returns one result
static int rectangleArea(int length, int breadth) {
return length * breadth;
}
public static void main(String[] args) {
System.out.println(rectangleArea(5, 3)); // 15
System.out.println(rectangleArea(10, 2)); // 20 — reused, not rewritten
}
}
Types of errors
- Syntax errors break the grammar of the language (a missing
;or brace). The compiler catches these and refuses to build the program. - Runtime errors appear while the program runs, e.g. dividing by zero. The program stops with an error message (an exception).
- Logical errors are the most dangerous: the program runs and produces output, but the output is wrong because the logic is faulty (e.g. using
+where*was meant). No error message appears — only careful testing reveals them.
Debugging techniques
Debugging is the process of finding and removing errors. Common techniques:
- read the compiler's error message carefully and go to the line it names;
- add temporary
System.out.println(...)statements to see the values of variables at each stage; …
A finite, ordered set of clear steps, written in plain language, that solves a problem — design …
A diagram that shows the logic of a solution using standard symbols (terminator, input/output, process, decisio …
A small, named, self-contained block of code that does one job, may take parameters and may return a result; the basis of …
Syntax errors break the language's grammar (caught by the compiler); runtime errors stop the program while it runs; logical errors let it run …
The process of locating and correcting errors in a program, using error messages, print statements and …