-
A program is a collection of instructions that performs a particular task on a computer. Software is a collection of one or more programs. Code refers to the actual symbols that a programmer types in that tell the computer what instructions to execute. Individuals who write programs are called programmers, software-engineers, software-architects, and coders among many other terms.
-
OOP is a strategy often employed by software developers. A programmer using an OOP strategy begins by selecting objects that can collectively solve the given problem.
-
To develop a particular program in an OOP fashion, the software developer might begin with a set of program requirements. For example:
Write a program to draw a square on a piece of paper with a pencil.
-
A way to determine the objects needed in a program is to search for the nouns of the problem. This technique suggests that the above program should have three objects: a pencil, a piece of paper, and a square.
-
Ideally, a programmer reuses an existing class to create objects, as opposed to writing code for a new class. For the purposes of our drawing example, we will use the preexisting DrawingTool
and SketchPad
classes for the pencil and paper objects. However, we don’t have a class for a square that is pre-made, so we must make our own.
- Programming languages can be compared to a foreign language - the first exposure to a written example is bound to seem pretty mysterious. You don't have to understand the details of the program shown below. They will be covered in more detail in the next lesson.
-
In OOP, we concern ourselves mostly with the objects themselves and how they relate to the other objects in the program. However, there must be a starting point for the program to begin creating objects, as the objects would obviously not be able to do anything if they did not exist. Code Sample 1.1 has no starting point and would therefore not be able to do anything by itself. Later on, we will learn how to utilize this code in an actual program.
-
The state of an object depends on its components. The DrawSquare
object includes one DrawingTool
object declared in the line that begins with the word DrawingTool
and a SketchPad
object declared in the line that begins with SketchPad
. The DrawingTool
object is given the name myPencil
and the SketchPad
object is given the name myPaper
.
-
A constructor is a method with the same name as the class. The first instruction will construct a new SketchPad
object named myPaper
with dimensions of 300 x 300 (read as 300 by 300). The next instruction will cause a new DrawingTool
object named myPencil
to be constructed using the SketchPad
object named myPaper
.
-
An object’s behavior is determined by instructions within its methods. When the method draw() for a DrawSquare() object is called, the instructions within the draw
method will execute in the order they appear. There are seven instructions in the draw method. The first instruction will cause the myPencil
to move forward 100 units drawing a line as it goes. The next line tells myPencil
to turn left. The remaining 5 steps repeat the process of steps to draw the remaining three sides of the square.
-
The DrawSquare
example illustrates the tools that a programmer uses to write a program. A program is built from objects and classes that a programmer writes or reuses. Classes are built from instructions, and these instructions are used in such a way that they manipulate objects to perform the desired tasks.