Computer Programming 2 Lecture 3: Methods in Advanced & Practices Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE
و من يتقِ الله... كلمة التقـوى : وَأَلْزَمَهُمْ كَلِمَةَ التَّقْوَى ( إِذْ جَعَلَ الَّذِينَ كَفَرُوا فِي قُلُوبِهِمُ الْحَمِيَّةَ حَمِيَّةَ الْجَاهِلِيَّةِ فَأَنْزَلَ اللَّهُ سَكِينَتَهُ عَلَى رَسُولِهِ وَعَلَى الْمُؤْمِنِينَ وَأَلْزَمَهُمْ كَلِمَةَ التَّقْوَى وَكَانُوا أَحَقَّ بِهَا وَأَهْلَهَا وَكَانَ اللَّهُ بِكُلِّ شَيْءٍ عَلِيماً ) ( الفتح :26) Mahmoud Rafeek Alfarra 2 Downloaded from شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي
Out Lines Mahmoud Rafeek Alfarra Downloaded from Downloaded from 3 Notes on Declaring and Using Methods Method Call Stack and Activation Records Case Study: Random-Number Generation Case Study: A Game of Chance Method Overloading General Practices about Methods
Notes on Declaring and Using Methods Mahmoud Rafeek Alfarra Downloaded from Downloaded from 4 There are three ways to call a method: 1. Using a method name by itself to call another method of the same class such as maximum( number1, number2, number3 ) 2. Using a variable that contains a reference to an object, followed by a dot (.) and the method name to call a method of the referenced object. maximumFinder.determineMaximum() 3. Using the class name and a dot (.) to call a static method of a classsuch as Math.sqrt( )
Method Call Stack and Activation Records Mahmoud Rafeek Alfarra Downloaded from Downloaded from 5 When a program calls a method, the called method must know how to return to its caller, so the return address of the calling method is pushed onto the program execution stack (sometimes referred to as the method call stack). If a series of method calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that each method can return to its caller.
Method Call Stack and Activation Records Mahmoud Rafeek Alfarra Downloaded from Downloaded from 6 The program execution stack also contains the memory for the local variables used in each invocation of a method during a program's execution. This data, stored as a portion of the program execution stack, is known as the activation record or stack frame of the method call.
Case Study: Random-Number Generation Mahmoud Rafeek Alfarra Downloaded from Downloaded from 7 The element of chance can be introduced in a program via an object of class Random (package java.util) or via the static method random of class Math. Objects of class Random can produce random boolean, byte, float, double, int, long and Gaussian values, whereas Math method random can produce only double values.
Case Study: Random-Number Generation Mahmoud Rafeek Alfarra Downloaded from Downloaded from 8 A new random-number generator object can be created as follows: Random new Random Random randomNumbers = new Random(); Then, to generate a random int values: int randomValue = randomNumbers.nextInt(); int randomValue = randomNumbers.nextInt( 2 ); int randomValue = randomNumbers.nextInt( 10 );
Case Study: A Game of Chance Mahmoud Rafeek Alfarra Downloaded from Downloaded from 9 Write a program to simulate any game which depends on generating random values, you can read the section 6.10 to more information. عَنْ أَبِي مُوسَى الْأَشْعَرِيِّ أَنَّ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ قَالَ " مَنْ لَعِبَ بِالنَّرْدِ ، فَقَدْ عَصَى اللَّهَ وَرَسُولَهُ ” صحيح
In the next Lecture Mahmoud Rafeek Alfarra Downloaded from Downloaded from 10 Mohammed Ahmad Abedallah Taher Khaled AbuDagga Group A Khaled AbuAzra. Abdallah Rezq Fares AbuHashem Group B Abederhman Arga Mohammed Breka Dawood Mnefy Ibrahem Kulab Group C Each group present the case studies of this chapter
Practice 1 Mahmoud Rafeek Alfarra Downloaded from Downloaded from 11 Write A method to Find the maximum even number in array ARRAY which contains random integers from 0 To 100. Then call it in the main Method.
Practice 2 Mahmoud Rafeek Alfarra Downloaded from Downloaded from 12 Write A method to Search in array ARRAY which contains random integers from 0 To 100. Write A method to Search in array ARRAY which contains random integers from 0 To 100. The wanted values must be accepted from user and then passed to method. The wanted values must be accepted from user and then passed to method.
Next Lecture … Mahmoud Rafeek Alfarra Downloaded from Downloaded from 13 Processing string