SlideServe
Menu
Préférences
© SlideServe · v2026

Creating Methods

naif avatar
8 diapositives · 242 Views · Jul 30, 2014

Creating Methods . v oid and return method . Methods . Object oriented programming concepts Classes objects properties (field data) method Why create methods: Reuse code main reason Nothing in main method can be reused in another class.

Creating Methods - slide 1
01 / 8Creating Methods
1 / 17

Télécharger la présentation

Creating Methods
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Creating Methods void and return method

  2. Methods Object oriented programming concepts • Classes • objects properties (field data) method Why create methods: Reuse code main reason Nothing in main method can be reused in another class. Class methods can be reused by creating an object of that class to call its methods or if it is a static method using the Class name to call its methods.

  3. Components of a method • Modifiers—such as public, private, • The return type—the data type of the value returned by the method, or void if the method does not return a value. • The method name—the rules for field names apply to method names as well, but the convention is a little different. • The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses. • The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

  4. Two types of methods • Void method: doesn’t return anything. It just completes a task or a command //create method to first and last index characters Visibility return type methodname parameter info public void findFirstAndLast(String s) { } String first = s.substring(0,1); String last = s.substring(s.length()-1); System.out.println(“first “ + first + “ last “ + last);

  5. Two types of methods return method • The method will return information from a particular data type //create method to find first and last char Visibility return type methodnameparam. info public StringfindFirstAndLastChar(String s) { } String first = s.substring(0,1); String last = s.substring(s.length()-1); return “first “ + first + “ last “ + last;

  6. Printing with methods void method will not return any information. Just perform task You will need to either create a return method that will return the information or put a system.out.print statement inside the method. Return method will return information from data type. When called it must be placed in a System.out.print statement to print information.

  7. Object of class call its methods public static void main(String[]args) { StringExamplese = new StringExample(); se.findFirstAndLast( "Luella High School“) ; System.out.println(se.findFirstAndLastChar("Luella High School“)); }

  8. Object of class call its methods with dot operator Class: StringExample Methods in class: void findFirstAndLast(String s) String findFirstAndLastChar(String s) Formal parameter public static void main(String[]args) { StringExamplese = new StringExample(); se.findFirstAndLast("Luella High School"); System.out.println(se.findFirstAndLastChar("Luella High School")); } actual parameter