Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computer Programming 2 Lecture 2: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER."— Presentation transcript:

1 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

2 و من يتقِ الله... ذكر معروف الكرخي عن بكر بن خنيس رحمهما الله قال : كيف يكون مُتَّقِيًا مَنْ لا يَدْرِي مَا يَتَّقِي Mahmoud Rafeek Alfarra 2 Downloaded from http://staff.cst.ps/mfarra شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي

3 Out Lines Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 3 Revision about Methods in Java Example: Search in 2-D Array X O Project.

4 Revision about Methods in Java Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 4 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.

5 Revision about Methods in Java Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 5 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

6 Example : Method accepts and return Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 6 Method return true if the parameter x is available in in array Names else return false. Method Array wanted True/false

7 Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 7 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

8 Example : Method accepts and return Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 8 In the last example: 1. Set the type of array as string 2. Re-programming it using Methods

9 Practice Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 9 Write a program to simulate the Attendance status for 10 students’ names (fname, laname), in 5 days for each student. Write a program to simulate the Attendance status for 10 students’ names (fname, laname), in 5 days for each student. Write a methods to: 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.

10 X O Project Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 10 Using Procedural programming, 2-d array, Write a program to simulate the XO game between user and computer.

11 Next Lecture … Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 11 Method in depth


Download ppt "Computer Programming 2 Lecture 2: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER."

Similar presentations


Ads by Google