KEMBAR78
Programming Languages Lec 9 | PDF | Parameter (Computer Programming) | Computer Engineering
0% found this document useful (0 votes)
6 views26 pages

Programming Languages Lec 9

Uploaded by

omarahmady754
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Programming Languages Lec 9

Uploaded by

omarahmady754
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Spring Semester

2024-2025

CS112
Programming Languages Lecture 9
Mahmoud Bassiouni, PhD
mbassiouni@eelu.edu.eg
Table of Contents
• 2D-Arrays
• Creation of 2D Arrays
• Accessing 2D Array Elements
• Initializing a 2D Array
• 2D Array Length
• Examples on 2D Arrays
• Methods
• Built-in Methods
• User-defined Methods
• Why Methods?
2
2D Arrays
• A one-dimensional array stores a list of elements.
• A two-dimensional array can be thought of as a table of elements, with rows and
columns

3
2D Arrays
• It can be seen as a table, with rows and columns.
• It is sometimes called Array, of arrays.

column 0 column 1 column 2 column 3

row 0

row 1

row 2

row 3

4
Creation of 2D Arrays

double[][] scores = new double[3][4];

two dimensional array rows columns

column 0 column 1 column 2 column 3

row 0 scores[0][0] scores[0][1] scores[0][2] scores[0][3]

row 1 scores[1][0] scores[1][1] scores[1][2] scores[1][3]

row 2 scores[2][0] scores[2][1] scores[2][2] scores[2][3]

5
Accessing 2D Arrays Elements

• scores [2][1] = 95;

column 0 column 1 column 2 column 3

row 0 0 0 0 0

row 1 0 0 0 0

row 2 0 95 0 0

6
Initializing a 2D Array

• int [][] numbers = {{1,2,3},{4,5,6},{7,8,9}};

column 0 column 1 column 2

row 0 1 2 3

row 1 4 5 6

row 2 7 8 9

7
2D Array Length

• int [][] numbers = {{1,2,3,4},{5,6,7},{9,10,11,12}};


• To get the number of rows in the 2D-Matrix we use numbers.length,
and to get the length of each row we use numbers[row].length

for (int row = 0; row < numbers.length; row++)


{
for (int col = 0; col < numbers[row].length; col++)
System.out.println(numbers[row][col]);
}

8
Display a 2D Array

9
Summing 2D Arrays

10
Max / Min 2D Arrays

11
Left Diagonal of 2D Arrays

12
Right Diagonal of 2D Arrays

13
Transpose 2D Arrays

14
Methods
• A method is a collection of statements that are grouped together to perform an
operation.
– System.out.println() (println) is a method, that consists of many statements.
– Keyboard.nextInt() (nextInt()) is a method, that consists of several statements.

• Two types:
– Built-in
– User-defined

• Three parts:
– Input
– Output
– Processing
15
Methods
• To use a method:
– Know its name
– Know its input(s)
– Know its output
– Know what it does!

• Built-in methods:
– Mathematics
– Strings
– Other

16
Built-in Methods
• Let’s consider the following built-in method:
System.out.println (“HelloWorld”);
1- Method Name: println
2- Input: 1 input which is “HelloWorld”
3- Output: It does not return any output.
4- What is does: it takes the word “HelloWorld” and prints it
on the screen

17
Built-in Methods
• Let’s consider the following built-in method:
Scanner keyboard = new Scanner (System.in);
int x = keyboard.nextInt();
1- Method Name: nextInt();
2- Input: It does not take any input the parenthesis is empty
3- Output: It returns one output and it is in variable x.
4- What is does: It stores the value taken from the screen in
the variable x.

18
Built-in Math Methods
Method Description Example
abs( x ) absolute value of x (this method also has float, int and long versions) abs( 23.7 ) is 23.7
abs( 0.0 ) is 0.0
abs( -23.7 ) is 23.7
ceil( x ) rounds x to the smallest integer not less than x ceil( 9.2 ) is 10.0
ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x (x is in radians) cos( 0.0 ) is 1.0

floor( x ) rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0
floor( -9.8 ) is -10.0
log( x ) natural logarithm of x (base e) log( Math.E ) is 1.0
log( Math.E * Math.E ) is 2.0
max( x, y ) larger value of x and y (this method also has float, int and long versions) max( 2.3, 12.7 ) is 12.7
max( -2.3, -12.7 ) is -2.3
min( x, y ) smaller value of x and y (this method also has float, int and long versions) min( 2.3, 12.7 ) is 2.3
min( -2.3, -12.7 ) is -12.7
pow( x, y ) x raised to the power y (xy) pow( 2.0, 7.0 ) is 128.0
pow( 9.0, 0.5 ) is 3.0
sin( x ) trigonometric sine of x (x is in radians) sin( 0.0 ) is 0.0

sqrt( x ) square root of x sqrt( 900.0 ) is 30.0


sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x (x is in radians) tan( 0.0 ) is 0.0
19
Built-in Math Methods

19
Using Built-In Method
• Calling/invoking a method
– Called method
– Calling method

• Math.abs() Example
• Methods called by the class name are called static methods.
• Methods called by the reference are called non-static methods.

20
User Defined Methods
• User defined methods should be written inside the class and not
inside other methods.
public int max(int N1 , int N2)
{
if (N1>N2)
return N1; Method Header
else 1. Name
Return N2; 2. Inputs
} 3. return type
4. Public static

Calling max: Method Body


Statements

int larger;
larger = max(30, 40);
21
User Defined Methods
• Return Statements:
– Returns a value to the calling method
– Ends a method

public static int sum(int num1, int num2)


{
int result; Return type
result = num1 + num2;
return result; The return statement
} causes the method to end
execution and it returns a
value back to the
This expression must be of the statement that called the
same data type as the return type method.
22
User defined Methods
public class Program {

public static void main (String [] args){


int x = 20, y = 40;
int total = sum (x, y);
System.out.print(“total is =“ + total);
}
20 40
public static int sum(int num1, int num2)
{
int result; • (int num1, int num2) are called parameters.
60 result = num1 + num2; • x, y are called arguments.
return result; • Passing the arguments to parameters is
} called pass by value or pass by copy
}
23
Why Methods?
• Methods are commonly used to break a problem down into small sub problems.
This is called divide and conquer.

• Methods simplify programs. If a specific task is performed in several places in the


program, a method can be written once to perform that task, and then be
executed anytime it is needed. This is known as code reuse.

• The process of breaking a problem down into smaller pieces is called functional
decomposition.

20

You might also like