Lucky Cat Tutoring Services


October 13, 2009, 4:07 pm
Filed under: Uncategorized | Tags: , , , ,

Methods:Stand alone units, or blocks of code that do a specific task.

–EXAMPLE: public static void main(String []args) <—a main method
–the "public" means it's public, "void" returns nothing, "(String[]args)" returns the string

Step-line Refinement: Creating routines and methods to break the code down into more manageable and efficent parts.

MAKING A METHOD
1) Call the method inside the main method: any method that is called inside the main must also be STATIC!

Example: public static void printStar()
{
}
KEY POINT:METHODS CAN NEVER BE INSIDE ANOTHER METHOD! It’s inside the class, but outside any other method!

2) next put the required method code inside the {} to make the method, making sure that the block of code stands alone.

3)INVOKING THE METHOD: put the method in the code by putting the method name and ()

printStar();

Example of full mini method program

/* This is a program that displays a star
*
*
*
*making methods
*/

public class StarProgram
{
public static void printStar()
{
System.out.println(” * “);
System.out.println(” *** “);
System.out.println(” ***** “);
System.out.println(” ******* “);
System.out.println(” ***** “);
System.out.println(” *** “);
System.out.println(” * “);
}
public static void main(String [] args)
{
//System.out.println(“This is my star” + “\nI call this star George\n”);
System.out.println(“This is my star\nI call this star Fred\n”);

printStar();//calling the method
}

}

The output looks like:

This is my star
I call this star Fred

*
***
*****
*******
*****
***
*

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: