22
10/25: Methods & templates • Return to concepts: methods • Math class methods •1 st Program of the day • About DrawLine.java modifications • Method definitions • Math.random()

10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Embed Size (px)

Citation preview

Page 1: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

10/25: Methods & templates

• Return to concepts: methods

• Math class methods

• 1st Program of the day

• About DrawLine.java modifications

• Method definitions

• Math.random()

Page 2: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Methods

• recall: a method is an action; something to do.

• terminology:Java API, Java class libraryprogrammer-defined methodsinstance (global) versus local variablesmethods are invoked by calling a method.methods return a result.

• Methods bear a conceptual resemblance to functions in equations and to templates.

Page 3: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

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.

Page 4: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

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).

Page 5: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Sample Programpublic 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; }}

Page 6: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

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; ex

• max ( x , y ) returns larger value ( x or y )

• min ( x , y ) returns smaller value • pow ( x , y ) xy, x to the yth power• sqrt ( x ) square root of x

Page 7: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Sample Programpublic 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; }}

Page 8: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

1st 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 x2 functions).

Page 9: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Math.random, more methods

• About DrawLine.java modifications– allow user input– draw a curve

• Method definitions

• Math.random()

Page 10: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Allow User Input (1 of 2)• Add in an init method for two input dialog boxes.

double coeff; //note that I declared thesedouble yInt; //two variables as global.

public void init () {

cText = JOptionPane.showInputDialog(“Coefficient, please.”);

yText = JOptionPane.showInputDialog (“Y-intercept, please.”);

coeff = Double.parseDouble ( cText );yInt = Double.parseDouble ( yText );

}

Page 11: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Allow User Input (2 of 2)

• Use coeff & yInt in the drawDot method

int drawDot ( int a ) { int b; //must cast to int below

b = (int) (coeff * a + yInt) ; return b;

}

Page 12: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Draw a Curve• Change the formula in the drawDot method from:

int drawDot ( int a ) {

int b;b = 2 * a + 10 ;

return b; }

• to: int drawDot ( int a )

{ int b;

b = 2 * ( a * a ) + 10 ; return b; }

Page 13: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Method Definitions

• in SquareInt.java (simple version below), we see a method called square:public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

Page 14: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

the method square is called in this line. Because it is in parentheses, x is the value to use as the input.

Page 15: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

x fits the requirement that it must be an integer.

Page 16: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

using x as the input, the method calculates a return value.

Page 17: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Method Definitions

public void paint ( Graphics g )

{

g.drawString(square(x) + “ “,25,25);}

public int square ( int y ){

return y * y ;}

the returned value is what replaces the square(x). This value is of type int.

x * x

Page 18: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Example: Maximum.java (p. 212)public void init (){ (stuff left out…)double max = maximum ( num1, num2, num3 );outputArea.setText ( “Max is “ + max );

}

public double maximum ( double x, double y, double z )

{return Math.max ( x, Math.max ( y, z ) );

}

Page 19: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Important: Coercion of Arguments• Java will automatically promote variables to

larger types when necessary; EX: int to double.

• In Math.pow, the requested types of input are double. However, Java will create a double version of an int variable to use in that method automatically.

• go to Java class index for Math.pow• read p. 213-214 carefully

Page 20: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Math.random()• generates a random double number in the range

0.0 <= x < 1.0

• To utilize this function, we use scaling & shifting.

• scaling: multiplying the function by a number to get the range we want.

• shifting: adding a number to the function to get the beginning point of the range that we want.

Page 21: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

Math.random()• EX: Math.random ()• shift it by 1: Math.random() + 1 makes 1 to <2• scale it by 6: Math.random()* 6 makes 0 to <5

• to simulate rolling a die, we want 6 possibilities (or a range of 6), from 1 to 6. They have to be integers.

(int) ( Math.random() * 6 + 1 );

Page 22: 10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions

2nd Program of the day

• pg. 220 RollDie.java

• Modify the program to allow the user to input how many times to roll the die.