Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming (OOP) LAB # 5

Similar presentations


Presentation on theme: "Object Oriented Programming (OOP) LAB # 5"— Presentation transcript:

1 Object Oriented Programming (OOP) LAB # 5
TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal

2 Methods

3 Method A Java method is a collection of statements that are grouped together to perform an operation. In java, Methods must be defined within a class. When you use methods: Programs will be easier to understand Programs will be easier to change, if needed Programs will be easier to write, test, and debug Programmers don’t need to repeat the same instructions many times.

4 Defining a Method in java (access modifier)
When you declare a method, the first thing you typically provide is called the access modifier. You can use one of three terms, public, private, or protected. Public means that the method is available to the entire application or at least to any part of the application that can see the class itself. So if the class is public and the method is public, then it can be called from anywhere. Private is the opposite. The method is only available to code within this class. Protected has to do with inheritance. A protected method is available to the current class and to any of its sub classes.

5 Defining a Method in java (static method or instance method)
If you want the method to be static, you can put the word static after the access modifier you choose . And if you don't want to it be static , don’t put any thing. When do you want a method to be static? the term static means that the method is also called a class method, as opposed to an instance method. A class method is called directly from the class definition, whereas an instance method is called from an instance of the class or an object. For example: (next slide)

6 Defining a Method in java (static method or instance method)- example

7 Defining a Method in java (void or other variable type)
After choosing the method to be static or not, you need to declare if it returns a value or not. If the method doesn’t return any value, it should be defined as void function. If it returns a value, it will take the same type of the variable it returns.

8 Defining a Method in java (function name)
After declaring the method as void or not, you should write name for your function. it's important to follow the right conventions when you name your methods and variables. The initial character must be lowercase or other java developers will think you don't know what you are doing, and it must be an alphabetical character, not numeric. You can also use underscores, but that's fairly in frequent.

9 Defining a Method in java (arguments)
The method can take arguments or not. For example, if you want to write a method that adds two numbers, the arguments passed into the function should be the two integer variables. But if you want to write a function that prints a statement, it can take no argument.

10 Defining a Method in java (statements)
At the end of the method name with its arguments you always add opening and closing parenthesis. You can write the instructions inside your method which you want the function to perform.

11 Exercise #1 Write a method that used to print even numbers from 1 to 10.

12 Define the main class as usual
Define the main function. Static method. So, we can call it easily from the main function. Call the function in the main method. the method’s name is printeven. This function doesn’t have arguments. Private method. So, it is only seen inside Printing class which is the class that it is defined inside. Void function. It returns no value. Set of statements to print even numbers in the range (1-10).

13 So, when we called it in the main function, we defined an object within the same class and then call the function. The method here is instance method.

14 Testing#1

15 Exercise#2 Write a java method which returns a rectangle area. Hint:
Rectangle Area= width*height

16 Define the main class as usual
Define the main function. Static method. So, we can call it easily from the main function. int means that it will return integer number. Call the function in the main method. Private method. So, it is only seen inside Area class which is the class that it is defined inside. the method’s name is rectanglearea. This function has two arguments (height and width). Compute rectangle area and return it.

17 Testing #2

18 The End !!


Download ppt "Object Oriented Programming (OOP) LAB # 5"

Similar presentations


Ads by Google