150 likes | 274 Views
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, 3)); // prints 8 2 and 3 are called parameters Can use variables also x = 2; y = 3;
E N D
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
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
Writing Methods Can give the method information also (in parameters) public void myMethod( ) { } Can return values also (int, double, etc) Make all methods public for now
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 xy(radius3 here) return volume; }
return type double Local variable and constant stuff inside curly braces is method body Java statements inside body, e.g., single = assignment return statement Class Details: Methods first line signature or header public double calcVolume(double radius ) { double volume; volume = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3); return volume; }
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
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
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.
more on main main must always be public name of method public static void main(String[ ] args) { } one array parameter. We’ll figure this out later 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
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
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
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.
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); }
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