0% found this document useful (0 votes)
5 views

Lecture 03 04-Introduction to Java Part 02

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 03 04-Introduction to Java Part 02

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CS SE 1209 & IT 1210

Lecture 03 & 04
Introduction to Java Part 02
04/06/2024 & 11/06/2024
Bachelor of Science (Hons) in Computer Science | Software Engineering | Information Technology
Department of Computing
Faculty of Computing and Technology
Saegis Campus
Nugegoda

1
Lecturer Name Saegis Campus
Java Methods

• A method is a block of code which only runs when it is called.

• You can pass data, known as parameters, into a method.

• Methods are used to perform certain actions, and they are also known as functions.

• Why use methods? To reuse code: define the code once, and use it many times.

2
T.L.Navodi Sithara Saegis Campus
Java Methods

Standard Library Methods User defined Methods

3
T.L.Navodi Sithara Saegis Campus
Create a Method
• A method must be declared within a class.

• It is defined with the name of the method, followed by parentheses ().

• Java provides some pre-defined methods, such as System.out.println(), but you can also create your
own methods to perform certain actions:

Create a method inside Main:

• myMethod() is the name of the method.

• static means that the method belongs to the Main class


and not an object of the Main class.

• void means that this method does not have a return value.

4
T.L.Navodi Sithara Saegis Campus
Create a Method
❑This function calculates and returns the square of a number
passed as a parameter:

argument (value
passed to
parameter)
❑Example of using the function:
area = squareOf(sideLength);
5
T.L.Navodi Sithara Saegis Campus
Call a Method
• To call a method in Java, write the method's name followed by two parentheses () and a
semicolon;

6
T.L.Navodi Sithara Saegis Campus
Call a Method
A method can also be called multiple times:

7
T.L.Navodi Sithara Saegis Campus
Java Method Parameters
Parameters and Arguments
Information can be passed to methods as parameter. Parameters act as variables inside the method.

Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want,
just separate them with a comma.

When a parameter is passed to the method, it is


called an argument. So, from the example above:
fname is a parameter, while Liam, Jenny and Anja
are arguments.

8
T.L.Navodi Sithara Saegis Campus
Java Method Parameters
Multiple Parameters

9
T.L.Navodi Sithara Saegis Campus
Function return types can be ...
 void – does something but no data value is returned
Example:
void blankLines(int count) {
for (int line = 0; line < count; line ++)
System.out.println();
}

 any Java type (including an object) – does something that needs to hand back
data
Example:
int squareOf(int num) {
return num * num;
}

10
T.L.Navodi Sithara Saegis Campus
Function return types can be ...
Return Values

The void keyword, used in the examples above, indicates that the method should not return a value.

If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and
use the return keyword inside the method:

11
T.L.Navodi Sithara Saegis Campus
Java Method Parameters
Return Values

This example returns the sum of a method's two parameters:

12
T.L.Navodi Sithara Saegis Campus
Java Method Parameters
You can also store the result in a variable (recommended, as it is easier to read and maintain):

13
T.L.Navodi Sithara Saegis Campus
Standard Library Methods Usage
Program to find out square root of a number

Public class squareroot{


public static void main(String[] args)
{
S.O.P(“ Square root of 16 = “ + Math.sqrt(16) );
}
}

O/P : Square root of 16 = 4

14
T.L.Navodi Sithara Saegis Campus
Create an Object
In Java, an object is created from a class. We have already created the class named Main, so now we can use this to
create objects.

To create an object of Main, specify the class name, followed by the object name, and use the keyword new:

15
T.L.Navodi Sithara Saegis Campus
Access Methods With an Object

16
T.L.Navodi Sithara Saegis Campus
The constructor
• A constructor in Java is a special method that is used to initialize objects.

• The constructor is called when an object of a class is created. It can be used to set initial values for object
attributes:

17
T.L.Navodi Sithara Saegis Campus
Constructor Parameters
• Constructors can also take parameters, which is used to initialize attributes.

• The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y).

• When we call the constructor, we pass a parameter to the constructor (5), which will set the value of x to 5:

18
T.L.Navodi Sithara Saegis Campus
The constructor

• Note that the constructor name must match the class name, and it
cannot have a return type (like void).

• Also note that the constructor is called when the object is created.

• All classes have constructors by default: if you do not create a class


constructor yourself, Java creates one for you. However, then you
are not able to set initial values for object attributes.

19
T.L.Navodi Sithara Saegis Campus
Call static method
• Static methods are associated with the class itself, rather than with instances of the class (objects).

• They can be called directly on the class without needing an instance of the class.

• To call a method in Java, write the method's name followed by two parentheses () and a semicolon;

• In the following example, myMethod() is used to print a text (the action), when it is called:

20
T.L.Navodi Sithara Saegis Campus
Call non-static method
Non-Static methods require object reference.
• Non-static methods are associated
Class A { with instances of the class
public void test() (objects).
{
System.out.println(“test is called”); • Non-static methods require an
} instance of the class to be called.
public static void main(String[] args)
{
A obj1 = new A();
obj1.test();
}
}
21
T.L.Navodi Sithara Saegis Campus
Putting it together: the class
public class Marks Class header
{
private int coursework;
private int exam; Attributes
public Marks() {
coursework = -1; Constructor
exam = -1;
}
public int getCoursework()
: :
public void setCoursework(. . .)
: : Methods
public double getResult(. . .)
}
22
T.L.Navodi Sithara Saegis Campus
23
Lecturer Name Saegis Campus

You might also like