Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,

Similar presentations


Presentation on theme: "Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,"— Presentation transcript:

1 Writing methods

2 Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2, 3)); // prints 8 2 and 3 are called parameters Can use variables also x = 2; y = 3; System.out.println(Math.pow(x, y)); // prints 8 System.out.println( ) is a method call

3 Program Design Putting all of our code in main will make main too large Want to be able to split code up functionally Use methods

4 Writing Methods public void myMethod( ) { } Can return values also (int, double, etc) Can give the method information also (in parameters) Make all methods public for now

5 Writing our own method: calcVolume public double calcVolume(double radius) { double volume; // or george. call it anything volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3); // pow(x, y) is method call to calculate x y (radius 3 here) return volume; }

6 Class Details: Methods public double calcVolume(double radius ) { double volume; volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3); return volume; } Java statements inside body, e.g., single = assignment return type double first line signature or header return statement Local variable and constant stuff inside curly braces is method body

7 return statement public double calcVolume( ) { // NO private in local variables; final for constants final double PI = 3.14159; double volume; volume = 4 / 3 * PI * radius * radius * radius; return volume; } type of method is return type to return a value, use ‘return value’; can be calculation

8 Calling methods that you wrote someMethod( ) { double r; // can call this anything r = 2.0; double v; // can call this anything v = calcVolume( r ); System.out.println(“Volume is “ + v ); } called radius in calcVolume. Doesn’t matter Called Volume in calcVolume. Doesn’t matter

9 Notes on methods main is a method Java statements must be within a method Methods can not be embedded: public void somemethod( ) { public void someothermethod( ) { } } To call a method, use ( )s, NO TYPES: answer = calcVolume(r) NOT answer = double calcVolume(double r); calling methods we write from main does not work as you might expect.

10 more on main public static void main(String[ ] args) { } main must always be public main must also be static. we’ll discuss this later in the semester. Our methods will not be static. static is why method calls in main do not work as we expect main always returns a void name of method one array parameter. We’ll figure this out later

11 How to make method calls work Because main is static, you cannot call methods from it. Instead, create a new class that you can call methods from

12 class DriverClass // NO Public { // put your method definitions here public void start() { // put all method calls here } } // end of the DriverClass public class HelloWorld { public static void main(String args[]) { DriverClass driver = new DriverClass(); driver.start(); } // end main } // end HelloWorld Question 6 }

13 Practice Write a method from the practice methods on the 150 review page: Given a test score return a letter which corresponds to the grade. E.g. 88 returns a B. Ignore +s and -s.

14 public class TestScoreConversion { public static void main(String args[]) { DriverClass driver = new DriverClass(); driver.start(); } class DriverClass { public void start( ) { char letter = letter_grade(88); System.out.println("An 88 is a " + letter); }

15 public char letter_grade(int score) { if (score >= 90) return 'A'; if (score >= 80) return 'B'; if (score >= 70) return 'C'; if (score >= 60) return 'D'; return 'F'; } } // DriverClass


Download ppt "Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,"

Similar presentations


Ads by Google