1 / 107

Methods basics

Methods basics. Before this week Solve the problem de l’heure by developing a static method main( ) Did use other libraries Class methods Non-void and void methods Methods with and without parameters. Things to learn and understand. Mechanics of method invocation Activation records

mira
Télécharger la présentation

Methods basics

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 basics • Before this week • Solve the problem de l’heure by developing a static method main() • Did use other libraries • Class methods • Non-void and void methods • Methods with and without parameters

  2. Things to learn and understand • Mechanics of method invocation • Activation records • Flow of control • Return statement • Parameter passing • Scope

  3. Methods • What are they • A named piece of code that can take parameters and produce a value • What are they good for • Support modular problem solving • Re-use • Correctness double ratio = Math.cos( 10 ); boolean b = MagicEightBall.shouldI( ”buy it” );

  4. Carbon dating • Archeologists can date a biological artifact by studying its relative concentration of carbon-14 to carbon-12 • Concentration is an indicator of agebecause radioactive carbon-14 decaysto carbon-12 in a very predictable manner • Carbon dating formula • Age = ln( concentration ) * –8260

  5. Sample run Let’s write a method that does the carbon dating and a program that uses the method

  6. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  7. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } • A block is a list of statements nested within braces • A method body is a block • A block can be placed anywhere a statement would be legal

  8. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } What is necessary for this to compile and run?

  9. Dater.java public class Analyze { public static void main(String[] args) { String s = Dater.carbonDate(); String t = Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Does this compile?

  10. Dater.java public class Analyze { public static void main(String[] args) { String s = Dater.carbonDate(); String t = Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Does this compile? No! There is nothing that carbonDating() is supplying for s and t to use.

  11. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Does this compile?

  12. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Does this compile? No! Without static then carbonDate() is a message method that must used with an object to take the message

  13. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } public – other classes can use the method

  14. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } static – library method – performs a service does not send a message to an object

  15. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } void – does not bring back a value; i.e., it does not share anything with the invoker

  16. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Like all programs begins with the running – invocation – of method main()

  17. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Programmers that say the method being invoked has the flow of control – its statements are the ones being currently executed

  18. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Every method invocation begins with the creation in the computer memory of an activation record that stores the values of the variables belonging to the method

  19. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  20. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  21. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } main() uses carbonDate() to create the display

  22. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Because carbonDate() is a class method, invocation starts with the name of its class

  23. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } carbonDate() takes the flow of control and its gets a new activation record

  24. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  25. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Ratio of C-14 to C-12? 0.05

  26. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Ratio of C-14 to C-12? 0.05

  27. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  28. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } A ratio of 0.05 indicates the object is approximately 24744 years old

  29. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  30. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Completion of carbonDate() releases its activation record and main() retakes the flow of control

  31. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } carbonDate() again starts up with a new activation record and the flow of control

  32. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  33. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  34. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Ratio of C-14 to C-12? 0.10

  35. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Ratio of C-14 to C-12? 0.10

  36. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  37. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } A ratio of 0.10 indicates the object is approximately 19019 years old

  38. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } Completion of carbonDate() releases its activation record and main() retakes the flow of control

  39. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } }

  40. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } main() completes and returns control back to the system

  41. Dater.java public class Analyze { public static void main(String[] args) { Dater.carbonDate(); Dater.carbonDate(); } } public class Dater { public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int) (Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n"); } } main() completes and returns control back to the system

  42. Sample run

  43. Dater.java publicstatic void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int)(Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n") }

  44. Modified Dater.java public class Analyze { // main(): program starting point public static void main(String[] args) { Dater.carbonDate(); System.out.println( concentration ); } public static void carbonDate() { ... double concentration = stdin.nextDouble(); } Does this compile and run? Why?

  45. Modified Dater.java public class Analyze { // main(): program starting point public static void main(String[] args) { Dater.carbonDate(); System.out.println( concentration ); } public static void carbonDate() { ... double concentration = stdin.nextDouble(); } Does this compile and run? Why? No. Local variables of other methods are not accessible

  46. Dater.java public static void carbonDate() { Scanner stdin = new Scanner(System.in); System.out.print("Ratio of C-14 to C-12? "); double concentration = stdin.nextDouble(); int ageInYears = (int)(Math.log(concentration) * -8260); System.out.println("A ratio of " + concentration + " indicates the object is approximately " + ageInYears + " years old\n") } • What’s defined in braces stays in braces • Method main() cannot use carbonDate()’s stdin, concentration, or ageInYears • When main() has the flow of control, carbonDate()’s local variables do not exist

  47. Bringing back something • Consider again String t = s.substring(5, 7); double ratio = Math.cos(10); • How about int n1 = StandardInput.promptAndReadInt(); int n2 = StandardInput.promptAndReadInt(); What is happening here? What is happening here?

  48. ReturnDemo.java public class ReturnDemo { // main(): program starting point public static void main(String[] args) { int n1 = StandardInput.promptAndReadInt(); System.out.println(n1 + "*" + n1 + "=" + (n1 * n1)); System.out.println(); int n2 = StandardInput.promptAndReadInt(); System.out.println(n2 + "*" + n2 + "=" + (n2 * n2)); System.out.println(); } } What type of value does promptAndReadInt() need to return?

  49. ReturnDemo.java

  50. StandardInput.java public class StandardInput { // promptAndReadInt(): returns next input as an int public static intpromptAndReadInt() { // get the number Scanner stdin = new Scanner(System.in); System.out.print("Enter an integer value: "); int result = stdin.nextInt(); // return the number return result; } } promptAndReadInt() method in class StandardInput has a return type ofint – a promptAndReadInt() invocation produces anintas its return value

More Related