Daniel Slaughter

Welcome to my WebQuest area

 

Resume

Teaching

Interests

Webmasterme

 

Just another caffeinated program

Programming using Java 2: learning methods

 

I.            Introduction

The key factor of standardized programming is organization. What can be done in 10 lines of code can something be better done in 20. It may seem more at first but when a program becomes complex then the satisfaction of the simplicity is outstanding.

 

In this WebQuest (Lesson) we will be going over the basics of creating a method in Java, and how to use it!

 

II.         Task

We will start by creating a new class that initializes in the main method (this is where the program starts). It will then incorporate different methods of that class into it. Making the passing of requests easier for the programmer (because we’re lazy). In the end we will have a finished project that is able to display a simple line of text that is called from another method using the System.out.println() class/method.

 

III.      Process

·         The teacher will go over the installation instructions prior to the assignment; the links to the subject’s matter can be found under the resource area.

·         Open up the Java compiler.

·         Create a new project.

·         Create a new class.

·         Inside the class declare the main method (previous lesson): public static void main(String[] args) { }

                                                          i.      inside this method’s bracket’s indlude the following line:

1.       System.out.println(displayInformation());

a.       This tells the compiler to jump down to that method and ask it for what it wants to return. In this case it will use that information to print it’s value.

·         Bellow the end bracket (}) of the main method declare a new method:

                                                          i.      public String displayInformation() {}

1.       public: this means that the method can be called anywhere within the program/application.

2.       String: this means that it will return some value when called (if no value is desired then it will be known as “void”).

3.       displayInformation: this is the name of the method; it is what will be referenced to.

4.       (): inside here can be set Parameters. Such as “int pValue”; this would give the method a value to use. Typically it will be used to judge what information will be sent back to the calling method/class. (we will go over this in another lesson).

                                                        ii.      Inside this method’s brackets (displayInformation) include the following line:

1.       return “I Love Methods!”;

a.       return: this will tell the compiler that the information proceeding the return will be sent back to the method/class that is calling this current method (displayInformation).

b.       “I Love Methods!”: since we are returning a “String” (specified in the declaration of the method) we will need to return a string value. The “”’s tell us that we are returning a String value.

·         Now just simply run and compile the code… It should print “I Love Methods!” and then exit.

 

IV.       Resources

http://sun.com this is where the java sdk can be found. (it’s used to compile the code.)

http://bluej.org this is where a good compiler called BlueJ can be found. It uses the sdk to compile the code.

 

V.          Evaluation

                        Teacher to student help will be given upon request.

                        Print out of the final project’s code will be collected.

 

VI.       Conclusion

This unit was used to help me understand and incorporate methods into future assignments. I can understand where the benefits can be used in larger projects and where it causes too much useless code in smaller ones.

 

 

[ Return ]