Methods
description
Transcript of Methods
![Page 1: Methods](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/1.jpg)
Methods
![Page 2: Methods](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/2.jpg)
Overview• Class/Library/static vs. Message/non-static• return and void• Parameters and arguments
![Page 3: Methods](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/11.jpg)
![Page 12: Methods](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/44.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062520/568162ba550346895dd3427a/html5/thumbnails/45.jpg)
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