Skip to main content
ICT
Lesson A1 - Introduction to Object Oriented Programming (OOP)
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

B. Methods page 4 of 8

  1. While a program is running, we create objects from class definitions to accomplish tasks. A task can range from drawing in a paint program, to adding numbers, to depositing money in a bank account. To instruct an object to perform a task, we send a message to it.

  2. In Java, we refer to these messages as methods.

  3. An object can only receive a message that it understands, which means that the message must be defined within its class.

  4. Suppose we take the DrawingTool class (provided by this curriculum in the package gpdraw.jar) and create an object myPencil. In OOP terminology, we say the object myPencil is an instance of the DrawingTool class. An object can only be an instance of one class. We can visually represent an object with an object diagram, as shown in Figure 1.1.

Figure 1.1 - A DrawingTool object named myPencil

  1. These are some of the behaviors that the DrawingTool class provides:

    • forward
    • turnLeft
    • getColor
  2. To draw a line of a specified length, we call the method forward along with passing the distance to move the pencil. A value we pass to an object’s method is called an argument. A diagram of calling a method is shown below in Figure 1.2.

Figure 1.2 Calling the forward method of a DrawingTool object

  1. If we need to change the direction myPencil is facing, we can call the turnLeft method. This will bring a ninety-degree turn to the left. Two left turns can give us a complete reversal of direction, and three left turns essentially gives us a right turn. Notice that we do not need to send any arguments with the turnLeft method. A left turn is simply a left turn and does not need any additional information from the user. A diagram calling turnLeft is shown below in Figure 1.3.


Figure 1.3 Calling the turnLeft method of a DrawingTool object

  1. The diagrams shown in Figures 1.2 and 1.3 illustrate situations in which an object carries out a request by the user but does not respond to the sender. Figure 1.2 requires arguments from the user because the user must specify how far to move, whereas Figure 1.3 operates without any specific details. However, in many situations we need an object to respond by returning a value to the sender. For example, suppose we want to know the current color that is being used for drawing. We can use the getColor method to return the value. The getColor method is illustrated returning a value to the sender in Figure 1.4 below.


Figure 1.4 - The result of getColor is returned to the sender

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.