[G1 Android Development] mohsinkhan47:
C.	Objects in Software

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

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

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

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

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

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

import gpdraw.*;

public class DrawSquare{

private DrawingTool myPencil;

private SketchPad myPaper;

public DrawSquare(){

myPaper = new SketchPad(300, 300);

myPencil = new DrawingTool(myPaper);

}

public void draw(){

myPencil.forward(100);

myPencil.turnLeft();

myPencil.forward(100);

myPencil.turnLeft();

myPencil.forward(100);

myPencil.turnLeft();

myPencil.forward(100);

}

}

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

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

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

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

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

D.	Compiling and Running a Program

1.	A programmer writes the text of a program using a software program called an editor. The text of a program in a particular programming language is referred to as source code, or you can simply use source or code individually. The source code is stored in a file called the source file. For example in the DrawSquare example given above, source code would be created and saved in a file named DrawSquare.java.

2.	Compiling is the process of converting a program written in a high-level language into the bytecode language the Java interpreter understands. A Java compiler will generate a bytecode file from a source file if there are no errors in the source file. In the case of DrawSquare, the source statements in the DrawSquare.java source file would be compiled to generate the bytecode file DrawSquare.class. Classes inside a package, such as the gpdraw.jar, have already been compiled into bytecode for you.

3.	Errors detected by the compiler are called compilation errors. Compilation errors are actually the easiest type of errors to correct. Most compilation errors are due to the violation of syntax rules. These are the basic rules of languages that programmers must follow so that the interpreter understands what to do. It is similar to grammar in a spoken language and varies from language to language.

4.	The Java interpreter will process the bytecode file and execute the instructions in it.

5.	If an error occurs while running the program, the interpreter will catch it and stop its execution. Errors detected by the interpreter are called run-time errors. Run-time errors are usually caused by a fault in the logic of the program, such as accidentally causing the computer to try and divide a number by zero.

SUMMARY/ REVIEW:	One can think of an OOP application as a simulated world of active objects. Each object has a set of methods that can process messages of certain types, send messages to other objects, and create new objects. Programmers can either define new classes for use in their program, or they can use pre-existing classes to create the objects for their application.