Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.

Similar presentations


Presentation on theme: "10/25: Methods & templates Return to concepts: methods Math class methods Program of the day."— Presentation transcript:

1 10/25: Methods & templates Return to concepts: methods Math class methods Program of the day

2 Methods recall: a method is an action; something to do. terminology: Java API, Java class library programmer-defined methods instance (global) versus local variables methods are invoked by calling a method. methods return a result. Methods bear a conceptual resemblance to functions in equations and to templates.

3 Methods: what they look like general format of methods: methodname ( argument ) methodname ( arg, arg ) Classname.methodname ( argument ) Classname.methodname ( arg, arg ) Names of methods look similar to names of variables & objects: they don’t start with capital letter or numbers. Methods can have one or more arguments. Multiple arguments are separated by commas. Methods retrieved from libraries have the name of the class that they are associated with in front separated by a period.

4 Method headers: what they look like general format of methods: privacy returntype methodname ( type parameter ) privacy returntype methodname (type prm, type prm) optional: public or private? Can this method be used outside this class or not? Parameters are templates for the arguments that the method will use as input. Parameter types specify the kind of arguments that the method will accept as input. Return types specify the kind of variables that the method will return (or output).

5 Sample Program public class DrawLine extends JApplet { public void paint ( Graphics g ) { int y; for ( int x = 0 ; x < 150 ; x++ ) { y = drawDot ( x ); g.drawString ( ".", x, y ); } int drawDot ( int a ) { int b; b = 2 * a + 10 ; return b; }

6 Math Class Methods Math class methods provide mathematical functions for us to use. A few: abs ( x ) absolute value of x exp ( x ) exponential of x; e x max ( x, y ) returns larger value ( x or y ) min ( x, y ) returns smaller value pow ( x, y ) x y, x to the y th power sqrt ( x ) square root of x

7 Sample Program public class CallMe extends JApplet { public void paint ( Graphics g ) { g.drawString ( " My phone number is ", 25, 25 ); for ( int x = 0 ; x < 8 ; x++ ) { if ( x == 3 ) { g.drawString ( " - ", 50 + x * 7, 40 ); continue; } g.drawString ( " " + randomNumber( 9 ), 50 + x*7, 40 ); } public int randomNumber ( int a ) { int b = 0; b = (int)( Math.random() * a + 1 ); return b; }

8 Program of the day Modify DrawLine to allow user input for the slope (coefficient of x) and the y-intercept. y = 2x + 10 Modify DrawLine to draw a curved line instead of a straight line (think of parabolas and x 2 functions).


Download ppt "10/25: Methods & templates Return to concepts: methods Math class methods Program of the day."

Similar presentations


Ads by Google