1.08k likes | 1.3k Views
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
E N D
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 • Flow of control • Return statement • Parameter passing • Scope
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” );
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
Sample run Let’s write a method that does the carbon dating and a program that uses the method
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"); } }
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
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?
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?
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.
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?
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
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
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
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
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()
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
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
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"); } }
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"); } }
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
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
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
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"); } }
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
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
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"); } }
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
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"); } }
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
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
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"); } }
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"); } }
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
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
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"); } }
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
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
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"); } }
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
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
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") }
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?
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
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
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?
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?
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