1 / 12

Writing Methods

Writing Methods. Create the method. A Method is a tool to do something It contains the code that performs the job Methods have two parts. Create the method. The first part is called the Header modifier return name public void message( ) { open & closing braces }.

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. Create the method • A Method is a tool to do something • It contains the code that performs the job • Methods have two parts

  3. Create the method • The first part is called the Header modifierreturnname public void message( ) { open & closing braces }

  4. Methods have two parts • The second part is called the Body • The bodycontains the lines of code that instruct the computer what to do • You can have as many lines of code in the body as you want

  5. Methods have two parts • ThisBody has two lines of code • It tells the computer to print to the screen the text that is inside the set of doublequotes after skipping a blank line public void message( ) { System.out.println(); System.out.println(“java is not kawfy”); }

  6. Create a Class • To use the method we need to create a simple class • The class is like a tool bag • It can hold one or many tools (methods) • The class also has a Header and a Body

  7. Create a Class The first part is called the Header modifierstructurename public class MyMethods { open & closing braces }

  8. Create a Class The second part is called the Body The body contains one or more methods public class MyMethods { public void message() { System.out.println(); System.out.println(“java is not kawfy”); } }

  9. Create an Application • An Application is an executable program • It uses the methods of different classes to execute the program • Some methods are from the Java Class Library • Other methods are from the Classes that We wrote

  10. Create an Application • We will create an object (instance) of the class • We will call (use) a method of the class • The Application class also has a Header and a Body • The Application has one method named main

  11. Create an Application • Syntax: public class RunMyMethods { public static void main(String [] args) { MyMethods mm = new MyMethods(); mm.message(); //class object calls method } }

  12. Compile and Run • Compile … correct any syntax errors • Run … check for correct output • If necessary make any corrections • Then recompile and rerun.

More Related