1 / 22

Introduction To Programming Information Technology , 1’ st Semester

MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE. Using Java. Introduction To Programming Information Technology , 1’ st Semester. Lecture 17 Methods in Java Practices. Teacher: Mahmoud Rafeek Alfarra. Practice 1.

Télécharger la présentation

Introduction To Programming Information Technology , 1’ st Semester

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Using Java Introduction To ProgrammingInformation Technology , 1’st Semester Lecture 17 Methods in Java Practices Teacher: Mahmoud Rafeek Alfarra

  2. Practice 1 • For the class Craps in Methods_Practice1 document , state the scope of each of the following entities: • The variable randomNumbers. • The variable die1. • The methodrollDice. • The methodplay. • The variable sumOfDice Presented & Prepared by: Mahmoud R. Alfarra

  3. Practice 2 • Give the method header for each of the following methods: • Method hypotenuse, which takes two double-precision, floating-point arguments side1 and side2 and returns a double-precision, floating-point result. • Method smallest, which takes three integers x, y and z and returns an integer. Presented & Prepared by: Mahmoud R. Alfarra

  4. Practice 2 Presented & Prepared by: Mahmoud R. Alfarra Give the method header for each of the following methods: Method instructions, which does not take any arguments and does not return a value. Method intToFloat, which takes an integer argument number and returns a floating-point result. 4

  5. Practice 3 • Find the error in each of the following program segments. Explain how to correct the error. Presented & Prepared by: Mahmoud R. Alfarra

  6. Practice 3 Presented & Prepared by: Mahmoud R. Alfarra Find the error in each of the following program segments. Explain how to correct the error. 6

  7. Practice 3 Presented & Prepared by: Mahmoud R. Alfarra Find the error in each of the following program segments. Explain how to correct the error. 7

  8. Practice 3 Presented & Prepared by: Mahmoud R. Alfarra Find the error in each of the following program segments. Explain how to correct the error. 8

  9. Practice 4 • Using Methods, Write a complete Java application to prompt the user for the double radius of a sphere, and call method sphereVolume to calculate and display the volume of the sphere. Use the following statement to calculate the volume: doublevolume = (4.0/ 3.0) * Math.PI * Math.pow( radius,3 ( Presented & Prepared by: Mahmoud R. Alfarra

  10. Practice 5 What is the value of x after each of the following statements is executed? Presented & Prepared by: Mahmoud R. Alfarra

  11. Practice 6 • Write a method multiple that determines, for a pair of integers, whether the second integer is a multiple of the first. • The method should take two integer arguments and return TRue if the second is a multiple of the first and false otherwise. • Incorporate this method into an application that inputs a series of pairs of integers (one pair at a time) and determines whether the second value in each pair is a multiple of the first. Presented & Prepared by: Mahmoud R. Alfarra

  12. Practice 7 Write a method isEven that uses the remainder operator (%) to determine whether an integer is even. The method should take an integer argument and return true if the integer is even and false otherwise. Incorporate this method into an application that inputs a sequence of integers (one at a time) and determines whether each is even or odd. Presented & Prepared by: Mahmoud R. Alfarra

  13. Practice 8 Write a method squareOfAsterisks that displays a solid square (the same number of rows and columns) of asterisks whose side is specified in integer parameter side. For example, if side is 4, the method should display a square (4*4) . Incorporate this method into an application that reads an integer value for side from the user and outputs the asterisks with the squareOfAsterisks method. Presented & Prepared by: Mahmoud R. Alfarra

  14. Practice 9 Modify the method created in Practice 8 to form the square out of whatever character is contained in character parameter fillCharacter. Thus, if side is 5 and fillCharacter is "#" ##### ##### ##### ##### ##### Presented & Prepared by: Mahmoud R. Alfarra

  15. Practice 10 Trace the following program: • Calculate the integer part of the quotient when integer a is divided by integer b. • Calculate the integer remainder when integer a is divided by integer b. • Use the program pieces developed in parts (a) and (b) to write a method displayDigits that receives an integer between 1 and 99999 and displays it as a sequence of digits, separating each pair of digits by two spaces. For example, the integer 4562 should appear as Presented & Prepared by: Mahmoud R. Alfarra

  16. Practice 10 Presented & Prepared by: Mahmoud R. Alfarra Trace the following program: • Use the program pieces developed in parts (a) and (b) to write a method displayDigits that receives an integer between 1 and 99999 and displays it as a sequence of digits, separating each pair of digits by two spaces. For example, the integer 4562 should appear as 4 5 6 2 16

  17. Practice 10 Presented & Prepared by: Mahmoud R. Alfarra Trace the following program: • Incorporate the method developed in part (c) into an application that inputs an integer and calls displayDigits by passing the method the integer entered. Display the results. 17

  18. Practice 11 Presented & Prepared by: Mahmoud R. Alfarra An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write a method that determines whether a number is prime. 18

  19. Practice 12 Presented & Prepared by: Mahmoud R. Alfarra Using methods, write application to sort the arrays, the method of sorting will receive the array and then sort it. 19

  20. More Practices • More practices in the text book, chapters 6 & 7 Presented & Prepared by: Mahmoud R. Alfarra

  21. Emank X Mezank كان النبي صلى الله عليه وسلم : يستغفر الله في المجلس الواحد أكثر من 70 مرة و يختم الصالحات بالاستغفار Presented & Prepared by: Mahmoud R. Alfarra

  22. Next… Practices Presented & Prepared by: Mahmoud R. Alfarra

More Related