Mada za sehemu hiiDemonstrate mastery of basic principles of Object Oriented Programming (Using C++; Java; Python; etc)Mada 4
- Describe the concept of Object Oriented Programming (Output, Directives, input, type bool, set width manipulator, type conversion, Object oriented paradigm differences between Object Oriented Programming and Procedure oriented programming, Encapsulation, Inheritance, composition and Polymorphism, Benefits of OOP, Structure of C++/Java/python, namespace, Data types, C++/Java/python tokens, Identifiers, Variables, Constants, Operators, Control structures and Loops)
- Describe the general structure of Object Oriented Program (Using C++, Java, Python, etc)
- Apply appropriate syntax and constructs to create Object Oriented programs
- Debug Object Oriented programs using appropriate skills (Use C++, Java, Python, etc)
Debugging Object Oriented Programs
Debugging is the process of finding and fixing errors (bugs) in computer programs. In Object Oriented Programming, errors can occur at different stages—from writing the code to running the program. Understanding the types of errors and how to debug them is an essential skill for every programmer.
When you write a program, errors may occur at different stages. The textbook identifies four main types of errors that every programmer must understand.
1. Syntax Errors
Syntax errors occur when you violate the rules of the programming language. These are also called compile-time errors because the compiler detects them before the program runs. Common syntax errors include:
- Missing semicolons (;)
- Misspelled keywords
- Mismatched parentheses or braces
- Using a variable that has not been declared
Example in C++:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 15;
cout << " " << (a, b) // semicolon missed
return 0;
}
Error message: error: expected ';' before '}'
The compiler tells you exactly where the problem is. To fix it, simply add the missing semicolon after the cout statement.
2. Semantic Errors
Semantic errors occur when statements are syntactically correct but have no meaning or cannot be interpreted by the compiler. These errors are often caused by using variables incorrectly or writing expressions that make no logical sense.
Example in C++:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
a + b = c; // semantic error: cannot assign to an expression
return 0;
}
Error message: error: value required as left operand of assignment
The error occurs because a + b is an expression, not a variable, so you cannot assign a value to it.
3. Logic Errors
Logic errors occur when a program runs without producing error messages but produces incorrect results. The program compiles and executes successfully, but the output is not what you expected. These errors are the most difficult to find because the compiler does not warn you.
Example in C++:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
// logical error: semicolon after loop makes it run empty
for(i = 0; i < 3; i++);
{
cout << "loop ";
continue;
}
return 0;
}
Output: No output (the loop executes three times with an empty body)
To fix this, remove the semicolon after the for statement.
4. Run-time Errors
Run-time errors occur during program execution after successful compilation. These errors cause the program to crash or behave abnormally. Common examples include dividing by zero, accessing invalid array indices, or trying to open a file that does not exist. Run-time errors are also called bugs.
Example in C++:
#include <iostream>
using namespace std;
int main()
{
int x = 10;
int div = x / 0; // division by zero causes run-time error
cout << "result = " << div;
return 0;
}
Result: The program terminates abnormally.
When your program contains errors, follow these systematic steps to identify and fix them:
- Read error messages carefully — The compiler usually tells you the line number where the error occurred
- Identify the type of error — Is it syntax, semantic, logic, or run-time?
- Fix syntax errors first — These prevent the program from compiling
- Test your program — Run it with different inputs to check for logic and run-time errors
- Use debugging tools — IDEs provide debuggers to step through code and inspect variables
Modern Integrated Development Environments (IDEs) provide powerful debugging tools:
- Breakpoints — Pause program execution at specific lines
- Step-through execution — Execute code line by line
- Variable inspection — Watch how values change during execution
- Call stack view — See the sequence of method calls
Consider the following C++ program that calculates the area of a rectangle:
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() {
return length * width;
}
};
int main()
{
Rectangle r1(5, 3);
cout << "Area = " << r1.area() << endl;
return 0;
}
Suppose the programmer makes the following mistakes:
Mistake 1: Forgets to include the header file
// Missing: #include <iostream>
Mistake 2: Declares length and width as private but tries to access them directly
// In main():
cout << r1.length; // Error: length is private
How to debug:
- The compiler will show an error about the missing header
- To access private members, use public getter methods:
// Add to class:
double getLength() { return length; }
double getWidth() { return width; }
| Error Type | When Detected | Example | Solution |
|---|---|---|---|
| Syntax | Compile-time | Missing semicolon | Add the missing symbol |
| Semantic | Compile-time | Assigning to expression | Rewrite the statement |
| Logic | Runtime (wrong output) | Wrong formula | Review program logic |
| Run-time | During execution | Division by zero | Add validation checks |
In Tanzania, debugging skills are essential when developing systems like mobile money applications (such as M-Pesa or Tigo Pesa) or inventory management software for local businesses. For example, if a small shop owner in Arusha uses a Python program to track sales and calculate profits in Tanzanian shillings, a logic error in the program (such as using addition instead of subtraction when calculating change) would give wrong results. Knowing how to debug helps the programmer identify and fix such errors quickly, ensuring accurate financial records for the business.
Swali
Which type of error occurs when a program compiles and runs without any error messages but produces incorrect results?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuuliza