Object Oriented Programming. Object-Oriented Programming Algorithms Testing and Debugging Software Reuse. Outline. Programming can be learned by discovering the techniques used by experienced programmers .
Testing and Debugging
Programming can be learned by discovering the techniques used by experienced programmers.
These techniques are applicable to almost every programming language, including Java.
The Object Oriented Programming treats a program as a collection of objects that interact by means of actions.
General speaking, our world consists of objectslikepeople, companies, cars, trees, animals, etc.
Objects can perform actions (a.k.a, behaviors) through their methods which affect themselves and other objects in the world.Programming and OOP
Objects of the same kind/type (Porsche, BMW) belong to the same class(car).
Objects within a class have the same kinds of attributes (state), e.g., speed or weight, and a common set of methods (behavior), e.g., moving forward or backward.
Note that each object has its own data values, e.g., speed of 142 mph or 130 mph.
OOP adheres to three primary design principles:
Encapsulation, Polymorphism, Inheritance.OOP Terminology
The attributes and methods associated with any particular class are encapsulated.
That is, put together in a capsulebut only a part of the contents is made accessible.
Awareness of the pedals and steering wheel is important to the driver but awareness of the stability control system or braking system is not important to the driver.
Encapsulation provides a means of using the class, however, it omits the details of how the class works, a.k.a., information hiding.Encapsulation
Comes from a Greek word meaning “many forms”. The same method adapts to mean different things in different contexts.
Enables an object to behave appropriately based on a specific context.
Consider the class “athlete” and its two objects “soccer player” and “baseball player”.
Method “play()” has two different meanings, i.e., playing soccer and playing baseball accordingly.Polymorphism
Classes can be organized using inheritance, that is, it is used to organize classes in a hierarchy manner.
A class at lower levels inherits all the characteristics of classes above it in the hierarchy.
At each level, classifications become more specialized by addingnew characteristics.
Higher classes are more inclusive and the inheritedcharacteristics do not need to be repeated.Inheritance
4, 2, 6 wheels accordingly
Extra steels in the body
By creating methods, programmers provide actions for objects to perform.
An algorithm describes a means of performing an action, i.e., a set of instructions for solving a problem. For instance, finding a certain “value” in a list of 10 numbers.
An algorithm must be expressed completely and precisely. It usually is expressed in Englishor pseudocode.
Once an algorithm is defined, expressing it in Java (or in another programming language) usually is easy.Algorithms
First write the number “0” on the whiteboard.
For each item on the list, repeat the following:
Addthe cost of the item to the number on the whiteboard.
Replacethe number on the whiteboard with the result of this addition.
Announce that the answer is the number written on the whiteboard.Example: Total Cost of All Items
An error in a program is called a bug. Eliminating errors is called debugging.
There exist three kinds or errors as follows: syntax errors, runtime errors and logic errors.
Avoid the errors in the first place:
Carefully design classes, algorithms and methods.
Carefully code everything into Java.
Testyour program with appropriate test cases where the answer is known. Discoverand fix any errors, then retest.Testing and Debugging
Refers to grammatical mistakes in a program:
The grammatical rules (syntax) for writing a program are very strict.
The compiler catches syntax errors and prints an error message.
Example of syntax error: using a period where a program expects a semicolon.Syntax Errors
Errors that are detected while your program is running, but not during compilation.
When the computer detects an error, it terminates the program and prints an error message.
Example of a runtime error: attempting to divide a number by 0, which is mathematically incorrect.Runtime Errors
Errors that are not detected during compilation or while running the program.
Logic errors cause the program to produce incorrect results.
Example: an attempt to calculate a Fahrenheit temperature from a Celsius temperature:
Multiplying by 9/5 and adding 23 instead of 32.Logic Errors
Most programs are created by combining components that already exist.
Reusing components saves time and money, as many programs do not created entirely from the scratch.
Reused components are likely to be betterdeveloped and more reliable.
Components should be designed to be reusable by other applications, e.g., Java provides many reusable classes.
Idea: design classes / objects that are general.Reusable Components