computer programming 2 lecture 1: advanced array data structure using methods prepared &...

16
Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE

Upload: rodger-hardy

Post on 02-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

Computer Programming 2

Lecture 1:Advanced Array Data Structure

Using Methods

Prepared & Presented by: Mahmoud Rafeek

Alfarra

MINISTRY OF EDUCATION & HIGHER EDUCATIONCOLLEGE OF SCIENCE AND TECHNOLOGY (CST)KHANYOUNIS- PALESTINE

Page 2: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

Downloaded from http://staff.cst.ps/mfarra

2

و من يتِق� الله ...

قال ربي سبحـانه:

و لو أن أهل القرى أمنوا و اتقوا لفتحنا عليهم بركات من السماء و األرض.

Mahmoud Rafeek Alfarra

شريحـة ثابتـة لعلنا نحسن من خاللها أخالقنـا و أفعالنا لنفوز يوم االمتحان الحقيقي

Page 3: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

3

Out Lines

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Revision about Array data structureExample: Store the even elements in arrayWhat is Multidimensional Arrays?Applications !!Example: X O’s Project.

Page 4: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

4

Revision about Array data structure

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

An array is a special type of object that can hold an

ordered collection of elements.

The type of the elements of the array is called the

base type of the array; the number of elements it

holds is a fixed attribute called its length.

Java supports arrays of all primitive and reference

types.

Page 5: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

5

Revision about Array data structure

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Arrays are static

data structure

(which means ?!!).

Page 6: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

6

Revision about Array data structure

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

To Create & initialize the one dim. array:basetype [] arrayname = new basetype [length];Arrayname[0] = val1;Arrayname[1] = val2;Arrayname[…] = valn;

Orbasetype [] arrayname = { val1, val2, val3, … };

Page 7: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

7

Revision about Array data structure

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Examples:int [] salary = new int [3];salary[0] = 487;salary[1] = 600;salary[2] = 894;

Orint [] salary = { 487, 600, 894 };

Page 8: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

8

Example : Store even elements in array

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Using Procedural programming, Write a program to

store the even numbers from int To int in array.

Then, print the summation of the array’s elements.

Page 9: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

9

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

import javax.swing.JOptionPane;public class SumEvenArray { public static void even (int low, int high){ int size = (high - low)/2 +2; int [] evenarr = new int [size]; int len=0; for (int i = low; i<=high; i++) { if (i%2==0){

evenarr[len] = i; len++;} } sumarr(evenarr); }public static void sumarr(int [] evenarr) { int sum=0; for (int i =0; i<evenarr.length; i++) sum= sum+evenarr[i]; JOptionPane.showMessageDialog(null, "Sum is:

"+sum); }public static void main(String[] args) { even(100, 110); }}

Page 10: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

10

What is Multidimensional Arrays?

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Multidimensional arrays with two dimensions are

often used to represent tables of values consisting of

information arranged in rows and columns.

To identify a particular table element, we must

specify two indices.

By convention, the first identifies the element's row

and the second its column.

Page 11: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

11

What is Multidimensional Arrays?

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

To Create & initialize the two dim. array:basetype [] [] arrayname = new basetype [r][c];Arrayname[0] [0]= val1;Arrayname[0] [1] = val2;Arrayname[…][…] = valn;

Orbasetype [] [] arrayname = { {val1, val2}, {val3, …} };

Page 12: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

12

What is Multidimensional Arrays?

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

To Create & initialize the two dim. array:int [] [] table = new int [3][2];table[0] [0]= 3;table[0] [1] = 5;table[0] [2] = 4;table[…][…] = valn;

Orint [] [] table = { {3, 5, 4}, {valn, …} };

Page 13: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

13

What is Multidimensional Arrays?

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Java does not support multidimensional arrays

directly, but it does allow the programmer to specify

one-dimensional arrays whose elements are also

one-dimensional arrays.

Page 14: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

14

Example :

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Using Procedural programming, Write a program to

store the following figure (multiple of 1, 2, 3, 4 by its

successors to three times).

4 3 2 1

10 8 6 2

18 15 12 3

28 24 20 4

Page 15: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

15

Lets thinking now…

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

4 3 2 1

10 8 6 2

18 15 12 3

28 24 20 4

3 2 1 0

0

1

2

3?How To

Page 16: Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER

16

Next Lecture…

Mahmoud Rafeek AlfarraDownloaded from http://staff.cst.ps/mfarra

Continue Array’s

Application