OBJECT ORIENT PROGRAMMING OOP Objective You will be

OBJECT ORIENT PROGRAMMING (OOP) Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples, and completing a lab.

What do all rectangles have in common?

What do all automobiles have in common?

What do all students have in common?

How are the following items similar and different: 1. A rectangle vs. trapezoids 2. A car vs. truck 3. An elementary student vs. a high school student vs. a college student

What classification do each of the groups fall under? 1. A rectangle vs. trapezoids 2. A car vs. truck 3. An elementary student vs. a high school student vs. a college student


What is a program? What is a programmer? A program is a collection of instructions that, when performed, cause a particular task to be performed on a computer. Individuals who write programs are therefore called programmers. The terms software and code refer to a collection of one or more programs, so programmers are also referred to as software developers.

What is object-oriented programming? Object-Oriented Programming (OOP) is an attempt to make programs more closely model the way people think about and deal with the world. In object-oriented programming, a program consists of a collection of interacting objects.

To write an object-oriented program you need to: 1. describe different types of objects (attributes) 2. describe what they can do (methods) 3. describe how they are created 4. describe how they interact with other objects.

Object: A drawing tool The attributes (state) of a pencil are its drawing color, width of the line it draws, its location on the drawing surface, etc. Its behaviors (methods) consist of drawing a circle, drawing a line in a forward or backward direction, changing its drawing direction, changing the color, etc.

An object in programming is an abstraction for a real-world object. To create an object inside the computer program, we must 1. provide a definition for objects - how they behave and 2. what kinds of information they maintain is called a class.

What is a class? • A class is like a rubber stamp that can be used many times to make many imprints. Each imprint is an object and each one has its own individual properties such as “size” and “position. ”

Imprints (instances) Rubber Stamp (class) An example of a class and five instances having different values for instance variables “size” and “position” and “color. ”

Drawing. Tool my. Pencil In OOP terminology, we say the Drawing. Tool object mypencil is an instance of the Drawing. Tool class. An object can only be an instance of one class. In effect, an instance of the class belongs to the class. A Drawing. Tool object named my. Pencil

Classes vs Objects We first define classes, and while the program is running, we create objects from these classes to accomplish tasks.

What are ‘tasks” ? A task can range from drawing in a paint program, to adding numbers, to depositing money in a bank account. To instruct a class or an object to perform a task, we send a message to it.

How do I send the object a message? What kind of messages can I send? 1. You can send a message only to the classes and objects that understand the message. 2. For an object to process the message it receives, it must possess a matching method,

What is a ‘method’ ? A method is a sequence of instructions an object follows to perform a task.

Example: Consider the object tool - pencil. You can draw a line in the forward direction or change the drawing direction by turning left or get the current drawing color red

Using your object my. Pencil of type Drawing. Tool you could represent the behaviors of the Drawing. Tool class with the methods forward turnleft getcolor

To draw a line of a specified length, we send the message forward along with the distance to move the pencil. A value we pass to an object is called an argument of a message Drawing. Tool my. Pencil forward 100 forward turn. Left get. Color

Drawing. Tool my. Pencil forward 100 forward turn. Left get. Color The object mypencil carries out a request (it draws a line 100 units long) but does not respond to the message sender

In many situations, we need an object to respond by returning a value to the message sender. For example, suppose you want to know the current color that is being used for drawing. We can use the get. Color message to return the value. Drawing. Too l my. Pencil forward get. Color color turn. Left get. Color The result of get. Color (purple) is returned to the sender of the message

So what are object-oriented programs? A set of instructions that create objects and use these objects to solve a given problem. A programmer using an object-oriented strategy begins by selecting objects that can collectively solve the given problem.

What must a programmer do first? You must begins with a set of program requirements that specifies the desired task for a program. For example: Write a program to draw a square on a piece of paper with a pencil.

The program requirements suggest that there are two objects, namely a pencil and piece of paper. To determine the objects needed in a program , search for the nouns of the problem.

What must I do next in developing the program? The next step is to find or create a class corresponding to each object. Classes are essential because they serve as the places where the code of an object‑oriented program resides.

Finding a corresponding class Ideally, a programmer reuses an existing class, as opposed to writing code for a new class. For the purposes of our drawing example, we will use the preexisting Drawing. Tool and Sketch. Pad classes for the pencil and paper objects.
![import apcslib. *; { public class Draw. Square { public static void main(String[] args) import apcslib. *; { public class Draw. Square { public static void main(String[] args)](http://slidetodoc.com/presentation_image_h2/a7470df3b6c418de0be84e40dce7388d/image-30.jpg)
import apcslib. *; { public class Draw. Square { public static void main(String[] args) Drawing. Tool pencil; object declarations Sketch. Pad paper; paper = new Sketch. Pad(300, 300); pencil = new Drawing. Tool(paper); pencil. forward(100); pencil. turn. Left(90); pencil. forward(100); } instructions
![import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing.](http://slidetodoc.com/presentation_image_h2/a7470df3b6c418de0be84e40dce7388d/image-31.jpg)
import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. Tool pencil; Sketch. Pad paper; The execution of an object-oriented program begins with an initial object. This initial object serves as the starting point for the entire program. The initial object belongs to the Draw. Square class.
![import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing.](http://slidetodoc.com/presentation_image_h2/a7470df3b6c418de0be84e40dce7388d/image-32.jpg)
import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. Tool pencil; Sketch. Pad paper; The state of an object depends on its components (objects). The Draw. Square object includes one Drawing. Tool object and a Sketch. Pad object.
![import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing.](http://slidetodoc.com/presentation_image_h2/a7470df3b6c418de0be84e40dce7388d/image-33.jpg)
import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. Tool pencil; Sketch. Pad paper; The Drawing. Tool object is given the name pencil and the Sketch. Pad object is given the name paper.

An object’s behavior is determined by instructions. When a program executes, the program’s instructions are performed. paper = new Sketch. Pad(300, 300); pencil = new Drawing. Tool(paper); pencil. forward(100); pencil. turn. Left(90); pencil. forward(100);
![import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing.](http://slidetodoc.com/presentation_image_h2/a7470df3b6c418de0be84e40dce7388d/image-35.jpg)
import apcslib. *; public class Draw. Square { public static void main(String[] args) Drawing. Tool pencil; Sketch. Pad paper; paper = new Sketch. Pad(300, 300); pencil = new Drawing. Tool(paper); pencil. forward(100); pencil. turn. Left(90); pencil. forward(100); } Classes are composed from instructions, and these instructions are used in such a way that they manipulate objects to perform the desired tasks.


Common Terminology 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 simply source. The source code is stored in a file called the source file. For example in the Draw. Square example, the source program would be created and saved in a file named Draw. Square. java.

What is “compiling” ? 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 Draw. Square, the source statements in the Draw. Square. java source file would be compiled to generate the bytecode file Draw. Square. class.

Library files Editor your code Source Code Compiler Class files Java Bytecode Program Interpreter Hello World Running Program

Errors! 1. Errors detected by the compiler are called compilation errors. 2. Compilation errors are actually the easiest type of errors to correct. Most compilation errors are due to the violation of syntax rules. Examples: Forgetting a semi-colon at the end of the line Case-sensitive mistakes

Errors! 2. The Java interpreter will process the bytecode file and execute the instructions in it. 3. 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. Example: Trying to divide by zero

Edit-Compile-Run Cycle for a Java Program Begin Edit Program Compile program Compiler errors? True Run program Run-time errors? False End True
- Slides: 42