Presentation is loading. Please wait.

Presentation is loading. Please wait.

METHODS, CLASSES, AND OBJECTS A FIRST LOOK

Similar presentations


Presentation on theme: "METHODS, CLASSES, AND OBJECTS A FIRST LOOK"— Presentation transcript:

1 METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Prof. Salim Arfaoui

2 Methods What is a Method? Syntax of a Method Method schema
Outline What is a Method? Syntax of a Method Method schema Information Passing Parameters and Arguments Scope and Side Effects of Value Parameters Returned Values

3 What is a method? Methods are reusable sections of code that perform actions. Many methods come from classes that are built into the Java language. For example, the println method is found in the System class that is automatically available in any Java program. It allows you to display a string with the statement System.out.println("hello world"); hello world is displayed The pow method comes from the Math class and can be used in any Java program as in: System.out.println(Math.pow(3, 2)); 9 is displayed since 3 squared equals 9

4 More about methods There are some classes that exist in Java for the sole purpose of containing handy, reusable methods. Such classes are called utility classes. Math, Integer, and Double are examples of utility classes since they contain so many useful methods including: Math.pow(), Math.sqrt(), Math.abs(), Integer.parseInt(),Double.parseDouble() constants like: Math.PI, Integer.MIN_VALUE

5 Syntax of a Method The minimum code required to create a method is:
The first line of the method is called the method’s signature. The signature is followed by a set of { } that define the bounds of the method’s code block. The statements to be executed when the method is invoked are coded inside this code block. returnType methodName( ){ //the code of the method is placed here }

6 Method schema No input (no parameters) No output (void return type)
Process No input (no parameters) No output (void return type) Executes a process static void greetings(){ System.out.println("hello"); } Takes input (Has parameters) static void add(int x, int y){ System.out.println(x+y); Returns a value ( must specify return Type) static int LuckyNumber(){ return 5; static double Div(int x, int y){ return (double)x/y; process input process output process input output

7 Information Passing For a method to function properly:
information must often be passed to it when it is invoked some must return one piece of information to the invoking statement. methods Parameters and Arguments If a method is to receive input, then its signature must contain a parameter list. each parameter consists of a variable name preceded by its type. To invoke the method outputAgeAndWeight: static void outputAgeAndWeight(int age, int weight){ System.out.println(age: " + age + " weight: " + weight); } outputAgeAndWeight(21, 170); This statement passes the integers 21 and 170 to the method.

8 Information Passing Returned Values
A worker method can return one, and only one value to the method that invokes it. A method that returns a value is called a value returning or non void method. Must specify the type of the information the method returns. A nonvoid method must contain a return statement. The method Multiply calculates and returns the product of two numbers passed to it. static double Multiply (int x, double y){ return x*y; }

9 OBJECT ORIENTED PROGRAMMING
Next What Are Classes and Objects? DEFINING CLASSES AND CREATING OBJECTS Specifying a class: UML Creating Objects Displaying an Object ADDING METHODS TO CLASSES


Download ppt "METHODS, CLASSES, AND OBJECTS A FIRST LOOK"

Similar presentations


Ads by Google