160 likes | 393 Views
MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE. Computer Programming 2. Lecture 3: Methods in Advanced & Practices. 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 3: Methods in Advanced & Practices Prepared & Presented by: Mahmoud Rafeek Alfarra
و من يتقِ الله ... كلمة التقـوى: (إِذْ جَعَلَ الَّذِينَ كَفَرُوا فِي قُلُوبِهِمُ الْحَمِيَّةَ حَمِيَّةَ الْجَاهِلِيَّةِ فَأَنْزَلَ اللَّهُ سَكِينَتَهُ عَلَى رَسُولِهِ وَعَلَى الْمُؤْمِنِينَ وَأَلْزَمَهُمْ كَلِمَةَ التَّقْوَى وَكَانُوا أَحَقَّ بِهَا وَأَهْلَهَا وَكَانَ اللَّهُ بِكُلِّ شَيْءٍ عَلِيماً) (الفتح:26) شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي Downloaded from http://staff.cst.ps/mfarra
Out Lines 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 Downloaded from http://staff.cst.ps/mfarra
Notes on Declaring and Using Methods There are three ways to call a method: Using a method name by itself to call another method of the same class such as maximum( number1, number2, number3 ) 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() Using the class name and a dot (.) to call a static method of a classsuch as Math.sqrt( 900.0 ) Downloaded from http://staff.cst.ps/mfarra
Method Call Stack and Activation Records 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. Downloaded from http://staff.cst.ps/mfarra
Method Call Stack and Activation Records 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. Downloaded from http://staff.cst.ps/mfarra
Case Study: Random-Number Generation 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. Downloaded from http://staff.cst.ps/mfarra
Case Study: Random-Number Generation A new random-number generator object can be created as follows: RandomrandomNumbers = newRandom(); Then, to generate a random int values: intrandomValue = randomNumbers.nextInt(); int randomValue = randomNumbers.nextInt( 2 ); int randomValue = randomNumbers.nextInt( 10 ); Downloaded from http://staff.cst.ps/mfarra
Case Study: A Game of Chance Write a program to simulate any game which depends on generating random values, you can read the section 6.10 to more information. عَنْ أَبِي مُوسَى الْأَشْعَرِيِّ أَنَّ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ قَالَ " مَنْ لَعِبَ بِالنَّرْدِ ، فَقَدْ عَصَى اللَّهَ وَرَسُولَهُ ” صحيح Downloaded from http://staff.cst.ps/mfarra
In the next Lecture Mohammed Ahmad AbedallahTaher KhaledAbuDagga Group A • AbederhmanArga • Mohammed Breka • DawoodMnefy • IbrahemKulab • Group C • KhaledAbuAzra. • AbdallahRezq • Fares AbuHashem • Group B Each group present the case studies of this chapter Downloaded from http://staff.cst.ps/mfarra
Practice 1 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. Downloaded from http://staff.cst.ps/mfarra
Practice 2 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. Downloaded from http://staff.cst.ps/mfarra
Next Lecture … Processing string Downloaded from http://staff.cst.ps/mfarra