Objects and Methods/Functions

If I wanted to write a basic obstacle avoidance program for a robotic car how would we do that? First we will need to understand the concept of Objects, and methods/functions.
In Object Orientated Programming, the programs are based on objects and how they function or how they interact with other objects. In our program, anything that can perform an action can be considered as an object. Therefore, the car is an object, so is the wheel, engine, obstacle sensor etc. To keep our program simple we will use only two objects (Car, and Sensor).
Each of these objects can perform a task; these tasks are called methods or functions.

Object Methods/Functions (possible actions)
Car Move Forward, Move Back, Turn Left, Turn Right
Sensor Check for Obstacle

Therefore, for example if we want to program a car to avoid an obstacle our program would have these instructions:

  • Sensor.CheckForObstacle

    1. Obstacle Detected

      1. Car.MoveBack
      2. Car.TurnRight
    2. No Obstacle Found

      1. Car.MoveForward

So to summarize; in programming, an object has methods & methods help an object to perform actions.
Now that we understand the relationship between Objects and Methods, let us focus on how to create a method.
Imagine methods as different specialists (Ex. Doctor, technician, accountant etc) When you need help with something, you can call a specialist. You do not need to know exactly how a specialist does their job, as long they can give us the result we are looking for we are happy. Similarly, Methods are pieces of code we can call to perform a task for us. Sometimes we need to give a method some information before it can do its job, sometimes we do not.
A typical method looks like this:


public static int myMethodName(char info1, boolean info2){  

	/*Code for performing the task goes here */

	return myMethodResult;
}

In the first line we put int before the method name to specify that the result this method will produce is going to be an integer. If the method requires information we will put it inside the brackets (), in this case the method requires a character and a boolean. Once the method has finished its task, you should deliver the result, using the return keyword.
Here is a simple program that uses a method to determine whether a number is greater than another number.

In this program the ‘isFirstGreater’ method is used to determine if 5>7 then it is used again to determine if 33>17. The first time the method inputs were variables, they second time they were explicit number inputs, both types of inputs will work. Here is the result of the program:

5 > 7 is false
33 > 17 is true
Method Calls

What is the correct method call for these methods?

public static int getAverage( int num1, int num2){
    return (num1+num2)/2;
}

public static boolean checkPassword( String pass ){
    if(pass.equals(A9nCd) return true;
    else return false;
}

Program: Create a method that performs additions, example if the input is 3 and 5 the result will be 3+5 = 8

Back to Course Menu
What do you think about this topic? Share your suggestions / comments