1 / 45

Methods

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”

august
Download Presentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods

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

  3. 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”

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

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

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

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

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

  9. Parameters and Arguments public class Example { public static void f( int x ) { System.out.println( x + 2 ); } } public classExampleUser { p … id main( String[] args ) { Example.f( 3 ); int y = 17; Example.f( y - 2 ); } }

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

  11. Carbon Dating • Archeologists can date artifacts by the relative concentration of carbon-14 in a sample compared to concentration in the air • The formula is age = ln( relative concentration ) × −8260

  12. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } }

  13. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Run invokes method main Dater.main() String args

  14. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Create a variable x in main Dater.main() String args intx

  15. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke carbonDate with argument 0.05 Dater.main() String args intx

  16. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke carbonDate with argument 0.05 Dater.main() Dater.carbonDate() String args double concentration 0.05 int x

  17. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Create a variable age in carbonDate Dater.main() Dater.carbonDate() String args double concentration 0.05 int x double age

  18. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.05 Dater.main() Dater.carbonDate() Math.log() String args double concentration 0.05 double x 0.05 int x double age

  19. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.05 Dater.main() Dater.carbonDate() Math.log() String args double concentration 0.05 int x double age -2.99573…

  20. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.05 Dater.main() Dater.carbonDate() String args double concentration 0.05 int x double age Math.log() -2.99573…

  21. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.05 Dater.main() Dater.carbonDate() String args double concentration 0.05 int x double age -2.99573…

  22. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Compute the value (24744.748…) and store it in age Dater.main() Dater.carbonDate() String args double concentration 0.05 int x double age 24744.74 -2.99573…

  23. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Compute the return value (24744) Dater.main() Dater.carbonDate() String args double concentration 0.05 int x double age 24744.74

  24. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } return and store the result Dater.main() Dater.carbonDate() String args int x 24744

  25. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } return and store the result Dater.main() Dater.carbonDate() String args 24744 intx

  26. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } return and store the result Dater.main() 24744 String args intx 24744

  27. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } After returning, carbonDate’s variables disappear Dater.main() String args intx 24744

  28. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( 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 intx 24744

  29. "0.05 means 24744 years old" publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke System.out’sprintln method Dater.main() PrintWriter.println() String args String thingToPrint intx 24744

  30. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke carbonDate with argument 0.10 Dater.main() String args intx 24744

  31. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke carbonDate with argument 0.10 Dater.main() Dater.carbonDate() String args double concentration 0.10 int x

  32. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Create a variable age in carbonDate Dater.main() Dater.carbonDate() String args double concentration 0.10 int x double age

  33. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.10 Dater.main() Dater.carbonDate() Math.log() String args double concentration 0.10 double x 0.10 int x double age

  34. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.10 Dater.main() Dater.carbonDate() String args double concentration 0.10 int x double age Math.log() -2.30259…

  35. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke Math’s log method with argument 0.10 Dater.main() Dater.carbonDate() String args double concentration 0.10 int x double age -2.30259…

  36. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Compute the value (19019.352…) and store it in age Dater.main() Dater.carbonDate() String args double concentration 0.10 int x double age 19019.35 -2.30259…

  37. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Compute the return value (19019) Dater.main() Dater.carbonDate() String args double concentration 0.10 int x double age 19019.35

  38. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } return and use the result Dater.main() Dater.carbonDate() String args int x 19019

  39. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } return and use the result Dater.main() String args Dater.carbonDate() intx 24744 19019

  40. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } return and use the result Dater.main() String args intx 24744 19019

  41. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } After returning, carbonDate’s variables disappear Dater.main() String args intx 24744 19019

  42. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Compute the string to print: "0.10 means 19019 years old" Dater.main() String args intx 24744 19019

  43. "0.10 means 19019 years old" publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } Invoke System.out’sprintln method Dater.main() PrintWriter.println() String args String thingToPrint intx 24744 19019

  44. publicclass Dater { publicstaticvoid 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" ); } publicstaticintcarbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } } After main ends, its variables disappear

More Related