🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

01_Handout_1(123).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

IT1908 DESIGNING METHODS The method name should follow the naming convention rules...

IT1908 DESIGNING METHODS The method name should follow the naming convention rules below: Parts of a Method Declaration o Should be a verb in lowercase (ex. run); or A method declaration specifies all the information needed to o A multi-word name that begins with a verb in lowercase, call a method. followed by adjectives, nouns, etc., that starts with Example of a method declaration: capital letters (ex. runFast) public final void nap(int minutes) throws Exception { The parameter list is a comma-delimited list of input //statements parameters, preceded by their data types and enclosed by } parentheses. Empty parentheses are allowed if there are no The parts of a method declaration are as follows: parameters specified. Element Value in nap() Required? The optional exception list indicates that the method might example throw one of the listed exception types. Access modifier public No The method body is everything that is enclosed between Optional specifier final No braces. It contains the method’s code including the Return type void Yes declaration of local variables. Method name nap Yes Parameter list (int minutes) Yes, but can be Access Modifiers empty ( ) Two (2) levels of access control: Optional throws Exception No o Top level – public or package-private exception list o Member level – public, private, protected, or Method body { Yes, but can be package-private //statements empty { } The public modifier allows the method to be visible to all } classes everywhere. To call the nap() method, just type its name followed by a The private modifier specifies that the method can only be single int value in parentheses. Ex. nap(15); accessed in its own class. An access modifier specifies whether other classes can use The protected modifier specifies that the member can only a particular field or invoke a particular method. The access be accessed within its own package and by a subclass of its modifiers in Java are public, protected, package-private class in another package. (no modifier), and private. The package-private modifier (no modifier) specifies that The most common optional specifiers are the following: the member can only be accessed within its own package. o static is used for class methods. o abstract is used when a method does not have a References: body. Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning Java programming: The object-oriented approach. Indiana: John Wiley & Sons, Inc. o final is used when a method is not allowed to be Farrell, J. (2014). Java programming (7th ed.). Boston: Course Technology, Cengage overridden by a subclass. Learning. Return type is the data type of the value returned by the Oracle Docs (n.d.). Citing sources. Retrieved from method, or void if the method does not return a value. https://docs.oracle.com/javase/tutorial/java/javaOO/index.html 01 Handout 1 *Property of STI  [email protected] Page 1 of 1

Tags

java programming method declaration software design
Use Quizgecko on...
Browser
Browser