110 likes | 222 Views
MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE. Computer Programming 2. Lecture 2: Advanced Array Data Structure Using Methods. Prepared & Presented by: Mahmoud Rafeek Alfarra. و من يتقِ الله.
E N D
MINISTRY OF EDUCATION & HIGHER EDUCATION • COLLEGE OF SCIENCE AND TECHNOLOGY (CST) • KHANYOUNIS- PALESTINE Computer Programming 2 Lecture 2: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra
و من يتقِ الله ... ذكر معروف الكرخي عن بكر بن خنيس رحمهما الله قال : كيف يكون مُتَّقِيًا مَنْ لا يَدْرِي مَا يَتَّقِي شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي Downloaded from http://staff.cst.ps/mfarra
Out Lines Revision about Methods in Java Example: Search in 2-D Array X O Project. Downloaded from http://staff.cst.ps/mfarra
Revision about Methods in Java 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. Downloaded from http://staff.cst.ps/mfarra
Revision about Methods in Java To Create Method: Access modifiers returned_typeMethod_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 Downloaded from http://staff.cst.ps/mfarra
Example : Method accepts and return Method return true if the parameter x is available in in array Names else return false. Method Array True/false wanted Downloaded from http://staff.cst.ps/mfarra
Solution without using Methods import javax.swing.JOptionPane; import java.math.*; public class Searchnumbers { public static void main(String[] args) { String input; int num [] = new int [10]; booleanflag= 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"); } } Downloaded from http://staff.cst.ps/mfarra
Example : Method accepts and return In the last example: Set the type of array as string Re-programming it using Methods Downloaded from http://staff.cst.ps/mfarra
Practice • Write a program to simulate the Attendance status for 10 students’ names (fname, laname), in 5 days for each student. • Write a methods to: • Calculate the sum of absent for each student. • Return the name of student who have the max number of absent days. • If the attendance of one day have 2 marks, print the marks for each student. Downloaded from http://staff.cst.ps/mfarra
X O Project Using Procedural programming, 2-d array, Write a program to simulate the XO game between user and computer. Downloaded from http://staff.cst.ps/mfarra
Next Lecture … Method in depth Downloaded from http://staff.cst.ps/mfarra