Understanding Java Application Programming Interface (API)
Exploring the rich collection of predefined classes in the Java API for input/output operations, database interactions, mathematical calculations, file processing, and more. Learn about static methods, method overloading, and the modularization benefits methods offer in programming.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
6001104-3 Structured Programming L. Rafika Maaroufi 1
Chapter3: Methods: a deeper look 2
Outline Java API Methods Static method Method with multiple parameters Method overloading 3
Java Application Programming Interface Java Application Programming Interface (also referred to as the Java API or Java class library) The Java API provides a rich collection of predefined classes that contain: Input/output operations, Methods for performing Database operations, Common mathematical Networking operations, calculations, File processing, String manipulations, Error checking and many Character manipulations, other useful tasks. 4
Java Application Programming Interface Java API Packages (See page 209) 5
Methods Methods (called functions or procedures in some languages) help you modularize a program by separating its tasks into self-contained units. The statements in the method bodies are written only once, are hidden from other methods and can be reused from several locations in a program. 6
Static Methods A static method or a class method A method applies to the class in which it s declared as a whole To declare a method as static, place the keyword static before the return type in the method s declaration. 7
Static Methods For any class imported into your program, you can call the class s static methods by specifying the name of the class in which the method is declared, followed by a dot (.) and the method name ClassName.methodName( arguments ) For example, For example, We use various Math class methods to present the concept of static methods: We can calculate the square root of 900.0 with the static method call Math.sqrt( 900.0 ) 8
Declaring Methods with Multiple Parameters Methods often require more than one piece of information to perform their tasks How to write your own methods with multiple parameters? 9
Example Use a method called maximum to determine and return the largest of three double values. In main, prompt the user to enter three double values, then read them from the user; calls method maximum to determine the largest of the three values it receives as arguments. When method maximum returns the result, the program assigns maximum s return value to local variable result. Then outputs the maximum value 10
s F Output: 12
Notes on Declaring and Using Methods 1. 1. Using a method name by itself to call another method of the same class such as maximum(number1, number2, number3) in line 21 of Fig. 6.3. 2. 2. Using a variable that contains a reference to an object, followed by a dot (.) and the method name to call a non-static method of the referenced object such as the method call in line 13 of Fig. 5.10, myGradeBook.displayMessage(), which calls a method of class GradeBook from the main method of GradeBookTest. 3 3. . Using the class name and a dot (.) to call a static method of a class such as Math.sqrt(900.0). 13
Method Overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters (determined by the number, types and order of the parameters) Is commonly used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments 14
Method Overloading For example, Math methods abs, min and max are overloaded with four versions each: 1 1. . One with two double parameters. 2 2. . One with two float parameters. 3 3. . One with two int parameters. 4 4. . One with two long parameters. 15
Distinguishing Between Overloaded Methods The compiler distinguishes overloaded methods by their signature a combination of the method s name and the number, types and order of its parameters. The order of the parameter types is important 18
Exercise 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: double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ) 19
Solution 20
References Java: How to Program, 9e, Dietel and Dietel, Pearson 0273759760 21