1 / 15

Writing methods

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;

miette
Download Presentation

Writing 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. 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 Can give the method information also (in parameters) public void myMethod( ) { } Can return values also (int, double, etc) 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 xy(radius3 here) return volume; }

  6. 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; }

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

  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

More Related