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

11
Computer Programming 2 Lecture 2: 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: meryl-cook

Post on 25-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Computer Programming 2

Lecture 2: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 2: 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 2: 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 Methods in JavaExample: Search in 2-D ArrayX O Project.

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

4

Revision about Methods in Java

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

Three kinds of modules exist in Java methods,

classes and packages.

Methods (called functions or procedures in

other programming languages) allow the

programmer to modularize a program by

separating its tasks into self-contained units.

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

5

Revision about Methods in Java

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

To Create Method:

Access modifiers returned_type Method_name (Type par1, Type par2, … )

control access to a

class's variables

and methods.

The value which the method

will returned

The name of method with the rules of

identifiers

The arguments which the

method needs to perform its task

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

6

Example : Method accepts and return

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

Method return true if the parameter x is available in

in array Names else return false.

Method

Array

wanted

True/false

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

7

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

import javax.swing.JOptionPane; import java.math.*;public class Searchnumbers {public static void main(String[] args) { String input; int num [] = new int [10]; boolean flag= false; for (int i=0; i<5; i++) { input = JOptionPane.showInputDialog("Enter another

number"); num[i]= Integer.parseInt(input); } input = JOptionPane.showInputDialog("Enter the wanted

value"); int wanted = Integer.parseInt(input); int j; for ( j=0; j<5; j++) { if (num[j]==wanted) { flag = true; break; } } if (flag == true) JOptionPane.showMessageDialog(null,"the value is

existed at cell "+j); else JOptionPane.showMessageDialog(null,"the value does

not exist"); }}

Solution without

using Methods

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

8

Example : Method accepts and return

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

In the last example:

1. Set the type of array as string

2. Re-programming it using Methods

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

9

Practice

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

Write a program to simulate the Attendance status for

10 students’ names (fname, laname), in 5 days for each

student.

Write a methods to:

1. Calculate the sum of absent for each student.

2. Return the name of student who have the max number of

absent days.

3. If the attendance of one day have 2 marks, print the marks for

each student.

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

10

X O Project

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

Using Procedural programming, 2-d array, Write a

program to simulate the XO game between user and

computer.

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

11

Next Lecture…

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

Method in

depth