Steps
to creating a program with methods
Step 1: Design
- Analyze - Determine the parts that repeat or that you want to separate
- Pick a name for your class
- Pick a name for each part (method)
Step 2: Create the class
- Open BlueJ
- Take menu option Project / Open Project / click on your
project
- click "New Class"
- Name the class the name you picked in Step 1
- Delete everything after public class (your name)
Step 3: Create the structure
- Enter Open close brackets under the class {
}
- Enter One line for the main method: public static void main ()
- Enter One line for each method: public static void yourMethodName
()
- Enter Open close brackets under each method {
}
- Compile and see no errors
Step 4: Write comments
- Type a comment between /* */ on the top of the class to describe
what the class does.
- Type a comment between /* */ right on top of the method
to describe what the method does.
- Type a comment on top of each method to describe the test data that would
be good and the desired result of each test.
- Compile and see no errors
Step 5: Set up the input variables
- Inside the method parentheses, put the type and name of each parameter
the method will take in (ex: int x, char ab)
- Compile and see no errors
Step 6: Set up the output variables
- If the method does not return anything, skip this whole step.
- Change the word void to the type being returned. ex: double instead of
void (Do not name the variable being returned in the method header. )
- Right before the methods ending curly brace, type return and an example
of the type. (Ex: return 3.0;)
- Compile and see no errors
Step 7: Code the parts (methods)
Inside the method brackets (the ones underneath the method)
type the statements needed to create the part. (This part is different
for every method. It depends upon what the method wants to accomplish.)
Change the return value to the real variable you want to return. (ex:
return abc);
Compile and see no errors.
You can also right click on the class in the BlueJ window and
execute the individual methods to be sure they work on their own.
Step 8: Call the parts (methods)
Inside the main method brackets (the ones underneath
the main method):
- Inside the main method brackets (the ones underneath the main
method):
- type the name of the first part you want the computer to do.
(You can find the name of the part by looking after "public static
void".)
- type ();
- repeat for all the parts you want the computer to do, in the
order you want them done.
- Compile and see no errors