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 .
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 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
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
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
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
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
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
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
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
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
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