methods. overview class/library/static vs. message/non-static return and void parameters and...

45
Methods

Upload: aron-carter

Post on 17-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Methods

Page 2: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Overview• Class/Library/static vs. Message/non-static• return and void• Parameters and arguments

Page 3: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Dot and Parenthesis• x.y means “the y inside x”– Similar to: ’s

• System.out.println means “The println inside the out that is inside System”

• x() means “x is a method and I want to run it”

Page 4: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

What is a class?• A class can be (any mix of) 3 things:– A program• If and only if it has “public static void main( String[] )”

– A library of methods and constants• Examples: Math, JOptionPane

– A description of a kind of object• … We’ll get into this in several weeks• Examples: Scanner, Random

Page 5: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

static and non-static • Consider x.y()• If x is a class, y must be static• If x is an Object, y should not be static

• Math.cos(): static because Math is a class• stdin.nextInt(): non-static because stdin is an object

(in particular, a Scanner).

Page 6: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Method Definition and Invocation• A method is a named piece of code• Defining a method = writing a how-to• Invoking a method = following that how-to

• Pressing “run” just invokes main()

Page 7: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Parameters and Arguments• A formal parameter is what a method writer

assumes an invoker will bring to the table• An actual argument is the thing the method

invoker brings• … people are sloppy with these names …

Page 8: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Parameters and Arguments• How to fry an egg– Crack the egg into a pan– Add heat

• Parameters: what I call things I assume will exist when defining a method

• Fry this ostrich egg• Fry this chicken egg

• Arguments: the values I use for the parameters when invoking a method

Page 9: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Parameters and Argumentspublic class Example { public static void f( int x ) {

System.out.println( x + 2 ); }}

public class ExampleUser { p … id main( String[] args ) { Example.f( 3 ); int y = 17; Example.f( y - 2 ); }}

Page 10: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Return• When I ask you “what’s sqrt(4),” you return

“2” to me• return is how methods give back values• return is also a way to halt a method

Page 11: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments
Page 12: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Carbon Dating• Archeologists can date artifacts by the relative

concentration of carbon-14 in a sample compared to concentration in the air

• The formula isage = ln( relative concentration ) × −8260

Page 13: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Page 14: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Run invokes method main

Dater.main()

String args

Page 15: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Create a variable x in main

Dater.main()

String args

int x

Page 16: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke carbonDate with argument 0.05

Dater.main()

String args

int x

Page 17: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke carbonDate with argument 0.05

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

Page 18: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Create a variable age in carbonDate

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age

Page 19: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.05

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age

Math.log()

double x 0.05

Page 20: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.05

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age

Math.log()

-2.99573…

Page 21: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.05

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age Math.log()

-2.99573…

Page 22: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.05

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age

-2.99573…

Page 23: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Compute the value (24744.748…) and store it in age

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age 24744.74

-2.99573…

Page 24: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Compute the return value (24744)

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.05

double age 24744.74

Page 25: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

return and store the result

Dater.main()

String args

int x24744

Dater.carbonDate()

Page 26: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

return and store the result

Dater.main()

String args24744

Dater.carbonDate()

int x

Page 27: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

return and store the result

Dater.main()

String args24744

int x 24744

Page 28: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

After returning, carbonDate’s variables disappear

Dater.main()

String args

int x 24744

Page 29: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Compute the string to print: "0.05 means 24744 years old"

Dater.main()

String args

int x 24744

Page 30: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke System.out’s println method

Dater.main()

String args

int x 24744

PrintWriter.println()

String thingToPrint

"0.05 means 24744 years old"

Page 31: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke carbonDate with argument 0.10

Dater.main()

String args

int x 24744

Page 32: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke carbonDate with argument 0.10

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

Page 33: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Create a variable age in carbonDate

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

double age

Page 34: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.10

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

double age

Math.log()

double x 0.10

Page 35: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.10

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

double age Math.log()

-2.30259…

Page 36: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke Math’s log method with argument 0.10

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

double age

-2.30259…

Page 37: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Compute the value (19019.352…) and store it in age

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

double age 19019.35

-2.30259…

Page 38: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Compute the return value (19019)

Dater.main()

String args

int x

Dater.carbonDate()

double concentration 0.10

double age 19019.35

Page 39: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

return and use the result

Dater.main()

String args

int x19019

Dater.carbonDate()

Page 40: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

return and use the result

Dater.main()

String args

19019int x 24744

Dater.carbonDate()

Page 41: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

return and use the result

Dater.main()

String args

19019int x 24744

Page 42: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

After returning, carbonDate’s variables disappear

Dater.main()

String args

19019int x 24744

Page 43: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Compute the string to print: "0.10 means 19019 years old"

19019

Dater.main()

String args

int x 24744

Page 44: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

Invoke System.out’s println method

Dater.main()

String args

int x 2474419019

PrintWriter.println()

String thingToPrint

"0.10 means 19019 years old"

Page 45: Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); }

public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }}

After main ends, its variables disappear