Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cant get Methods
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
LOL123




PostPosted: Wed Apr 14, 2010 8:19 pm   Post subject: Cant get Methods

Hi,
I need to make a program that calculates the area of a circle. I have to get the input then use a method to calculate the area. I dont really understand methods I have read wtd's tutorial but still a bit unclear on how to create the method in my program and make it do what I need it to.
This is my code so far:

code:

import java.io.*;
class method
{
  public static void main (String args [])
    throws java.io.IOException
  {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
   
    int radius = 0;
    String input = "";
    radius = Integer.parseInt (input);
    System.out.println ("Please enter a number from the radius of a circle.");
    input = br.readLine();
   }
}
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Wed Apr 14, 2010 8:41 pm   Post subject: Re: Cant get Methods

You know that
code:

public static void main(String[] args)
{
stuff
}

That you put in every program?

That's a method. Creating a custom method works exactly the same way as writing main.

code:

(public OR private OR protected) [static] (return type) methodName([parameters])
{
    code;
}


Where static and parameters are optional, and return type is any data type/class or void (no return.) You can name the method whatever you want (even if there's already a method with that name, so long as they have different parameters. Methods are generally public.

If you want to call a method directly from main, it must be declared static (since main is always static). When you call a method, any code inside that method is run, after which the program will pick up where it left off.

As an example, here's a method that will accept an integer, multiply it by 2, and return the new value.

Java:

class MyClass
{
    public static void main(String[] args)
    {
         int x = doubleNum(3); //program will go find the "doubleNum" method in this class, and run it
                                         //with '3' as the parameter. assign this value to the variable x.
         //The program then returns back to this point, and continues. Let's output our new number.
         System.out.println(x);
    }

    /*This Creates:
       a method that is static (so we can call it from main, since main is also static)
       a method that returns an integer (int doubleNum)
       a method that accepts one integer (which is referred to inside the method by the name "number")
    */

    public static int doubleNum(int number)
    {
         //calculate our value
         int temp = 2 * number;

         //return tells Java that the method is done, and to go back to where the main program left off, and to give the
         //specified value to the program. in this case, 2 times whatever number the method was called with.
         return temp;
     }
}
slider203




PostPosted: Wed Apr 14, 2010 10:17 pm   Post subject: Re: Cant get Methods

Also if you want your method stored in a diffrent file you can use code like this:
code:

class <classname>
{
    variables used

public static int area()
{

  steps in method

}
}


You will have to use an object to access the method in your program though.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: