04MethodsSmall

04MethodsSmall - Lecture Set 4 More About Methods and More...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) Lecture Set 4: More About Methods and More About Operators c Methods c Definitions c Invocations c More arithmetic operators c Operator Side effects c Operator Precedence c Short-circuiting main method public static void main(String args[]){ // statements here } c All projects and examples have defined this method c No explicit call needed c Parts of the line c Name = main c Parameter List = String args[] c Return type = void c Access = public -- more on this later c Modifier = static CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) Other public static methods c A static method is associated with a class c not an individual instance (object) c Must have all of the same parts as the main public static returnType name ( argList ){ body } c For example – defining a method to print a number of stars public static void printStars(int count){ for (int curr = 0; curr < count; curr++){ System.out.print(“*”); } } c For example – defining a method to print a number of stars printStars(3) System.out.println(); printStars(77); CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 3 method information: parameters and arguments c parameter list c type name for each item in the list c e.g. (MyGrid grid, char where) c argument list c expression for each item in the list c e.g. (grid, ‘t’) c Matched between the arguments and the parameters based on position in the list CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 4 Non-main static public methods: defining, invoking and commenting c Defined based on a name and a list of parameters public static void name(parameterlist){ body } c Invoked by stating its name and giving an argument for each element of the parameter list name(argumentlist); c Each method must have a well defined purpose c That information goes into a comment before the method definition c Each parameter’s purpose should be explained
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

04MethodsSmall - Lecture Set 4 More About Methods and More...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online