Writing a method [beginner]

Discussion in 'Java' started by KyoGaSuki, Mar 20, 2008.

  1. KyoGaSuki

    KyoGaSuki Guest

    So we have a test tomorrow on writing methods, and I thought I
    understood it...until I looked at some examples and realized....I
    don't know how to actually write one.

    So far I know:

    public static void methodName( // Don't know what goes here and
    why // ) {
    statements here;
    }

    Can anyone help T.T?
    KyoGaSuki, Mar 20, 2008
    #1
    1. Advertising

  2. those would be the parameters say if you wanted a method called addOne

    public static int addOne(int firstNumber)
    {
    return firstNumber + 1;
    }

    On Mar 19, 11:02 pm, KyoGaSuki <>
    wrote:
    > So we have a test tomorrow on writing methods, and I thought I
    > understood it...until I looked at some examples and realized....I
    > don't know how to actually write one.
    >
    > So far I know:
    >
    > public static void methodName( // Don't know what goes here and
    > why // ) {
    >           statements here;
    >
    > }
    >
    > Can anyone help T.T?
    Mark Vismonte, Mar 20, 2008
    #2
    1. Advertising

  3. or say you wanted something to print out

    public void printThis(String z)
    {
    System.out.println(z);
    }


    On Mar 20, 12:31 am, Mark Vismonte <> wrote:
    > those would be the parameters say if you wanted a method called addOne
    >
    > public static int addOne(int firstNumber)
    > {
    > return firstNumber + 1;
    >
    > }
    >
    > On Mar 19, 11:02 pm, KyoGaSuki <>
    > wrote:
    >
    > > So we have a test tomorrow on writing methods, and I thought I
    > > understood it...until I looked at some examples and realized....I
    > > don't know how to actually write one.

    >
    > > So far I know:

    >
    > > public static void methodName( // Don't know what goes here and
    > > why // ) {
    > >           statements here;

    >
    > > }

    >
    > > Can anyone help T.T?
    Mark Vismonte, Mar 20, 2008
    #3
  4. KyoGaSuki

    KyoGaSuki Guest

    On Mar 20, 3:35 am, Mark Vismonte <> wrote:
    > or say you wanted something to print out
    >
    > public void printThis(String z)
    > {
    > System.out.println(z);
    >
    > }
    >
    > On Mar 20, 12:31 am, Mark Vismonte <> wrote:
    >
    > > those would be the parameters say if you wanted a method called addOne

    >
    > > public static int addOne(int firstNumber)
    > > {
    > > return firstNumber + 1;

    >
    > > }

    >
    > > On Mar 19, 11:02 pm, KyoGaSuki <>
    > > wrote:

    >
    > > > So we have a test tomorrow on writing methods, and I thought I
    > > > understood it...until I looked at some examples and realized....I
    > > > don't know how to actually write one.

    >
    > > > So far I know:

    >
    > > > public static void methodName( // Don't know what goes here and
    > > > why // ) {
    > > > statements here;

    >
    > > > }

    >
    > > > Can anyone help T.T?


    So when it says "the method getCoord takes one string parameter, does
    that mean is should be:

    public static string getCoords

    and then it says "prompt for and performs input of one floating point
    value representing a coordinate of a point" does it mean that it would
    be?:

    public static string getCoords(float coord){

    }
    KyoGaSuki, Mar 20, 2008
    #4
  5. KyoGaSuki

    Abdullah Guest

    On Mar 20, 1:23 pm, KyoGaSuki <> wrote:
    > On Mar 20, 3:35 am, Mark Vismonte <> wrote:
    >
    >
    >
    > > or say you wanted something to print out

    >
    > > public void printThis(String z)
    > > {
    > > System.out.println(z);

    >
    > > }

    >
    > > On Mar 20, 12:31 am, Mark Vismonte <> wrote:

    >
    > > > those would be the parameters say if you wanted a method called addOne

    >
    > > > public static int addOne(int firstNumber)
    > > > {
    > > > return firstNumber + 1;

    >
    > > > }

    >
    > > > On Mar 19, 11:02 pm, KyoGaSuki <>
    > > > wrote:

    >
    > > > > So we have a test tomorrow on writing methods, and I thought I
    > > > > understood it...until I looked at some examples and realized....I
    > > > > don't know how to actually write one.

    >
    > > > > So far I know:

    >
    > > > > public static void methodName( // Don't know what goes here and
    > > > > why // ) {
    > > > > statements here;

    >
    > > > > }

    >
    > > > > Can anyone help T.T?

    >
    > So when it says "the method getCoord takes one string parameter, does
    > that mean is should be:
    >
    > public static string getCoords
    >


    No, it means:

    public static getCoords(String param) {

    }

    > and then it says "prompt for and performs input of one floating point
    > value representing a coordinate of a point" does it mean that it would
    > be?:
    >
    > public static string getCoords(float coord){
    >
    > }


    This one is wrong too. You'll need to use something to /ask/ the user
    for input. Use JOptionPane.showInputDialog() for a GUI input dialog
    box, or look up the Scanner class if you want something on the command
    line.
    Abdullah, Mar 20, 2008
    #5
  6. KyoGaSuki

    KyoGaSuki Guest

    On Mar 20, 5:29 am, Abdullah <> wrote:
    > On Mar 20, 1:23 pm, KyoGaSuki <> wrote:
    >
    >
    >
    > > On Mar 20, 3:35 am, Mark Vismonte <> wrote:

    >
    > > > or say you wanted something to print out

    >
    > > > public void printThis(String z)
    > > > {
    > > > System.out.println(z);

    >
    > > > }

    >
    > > > On Mar 20, 12:31 am, Mark Vismonte <> wrote:

    >
    > > > > those would be the parameters say if you wanted a method called addOne

    >
    > > > > public static int addOne(int firstNumber)
    > > > > {
    > > > > return firstNumber + 1;

    >
    > > > > }

    >
    > > > > On Mar 19, 11:02 pm, KyoGaSuki <>
    > > > > wrote:

    >
    > > > > > So we have a test tomorrow on writing methods, and I thought I
    > > > > > understood it...until I looked at some examples and realized....I
    > > > > > don't know how to actually write one.

    >
    > > > > > So far I know:

    >
    > > > > > public static void methodName( // Don't know what goes here and
    > > > > > why // ) {
    > > > > > statements here;

    >
    > > > > > }

    >
    > > > > > Can anyone help T.T?

    >
    > > So when it says "the method getCoord takes one string parameter, does
    > > that mean is should be:

    >
    > > public static string getCoords

    >
    > No, it means:
    >
    > public static getCoords(String param) {
    >
    > }
    > > and then it says "prompt for and performs input of one floating point
    > > value representing a coordinate of a point" does it mean that it would
    > > be?:

    >
    > > public static string getCoords(float coord){

    >
    > > }

    >
    > This one is wrong too. You'll need to use something to /ask/ the user
    > for input. Use JOptionPane.showInputDialog() for a GUI input dialog
    > box, or look up the Scanner class if you want something on the command
    > line.


    So, other than forgetting to include the input prompt in that last
    one, is it correct? Is it possible to use the print command in this
    instance? Other than the opening of the method, the only other thing
    that really confuses me is the variables inside. In a lot of examples
    they have things like:

    public static string getCoords(float coord) {
    getCoords = newCoord;
    }

    The variables just seem to change randomly (at least to me).
    KyoGaSuki, Mar 20, 2008
    #6
  7. KyoGaSuki

    Cameron Guest

    Hope this makes sense... It just takes the coords and prints em out
    via another method.
    as long as u reference the variables properly in the new method, you
    should have no problem


    /*takes x and y coords and prints them out*/
    public class coord{

    public static void main(String[] args){

    //set global var
    double xCoord = 2.231;
    double yCoord = 56.21;

    //call method to print out coords
    printCoords(xCoord, yCoord);

    }//end of main

    /*in the constructor u can call xCoord or yCoord anything you like
    *as long as u reference it properly in the method
    */
    public static void printCoords(double xCoord, double yCoord){

    //print out coord values
    System.out.println(xCoord+" "+yCoord);

    }//end of method

    }//end of class




    On Mar 20, 1:13 pm, KyoGaSuki <> wrote:
    > On Mar 20, 5:29 am, Abdullah <> wrote:
    >
    >
    >
    > > On Mar 20, 1:23 pm, KyoGaSuki <> wrote:

    >
    > > > On Mar 20, 3:35 am, Mark Vismonte <> wrote:

    >
    > > > > or say you wanted something to print out

    >
    > > > > public void printThis(String z)
    > > > > {
    > > > > System.out.println(z);

    >
    > > > > }

    >
    > > > > On Mar 20, 12:31 am, Mark Vismonte <> wrote:

    >
    > > > > > those would be the parameters say if you wanted a method called addOne

    >
    > > > > > public static int addOne(int firstNumber)
    > > > > > {
    > > > > > return firstNumber + 1;

    >
    > > > > > }

    >
    > > > > > On Mar 19, 11:02 pm, KyoGaSuki <>
    > > > > > wrote:

    >
    > > > > > > So we have a test tomorrow on writing methods, and I thought I
    > > > > > > understood it...until I looked at some examples and realized....I
    > > > > > > don't know how to actually write one.

    >
    > > > > > > So far I know:

    >
    > > > > > > public static void methodName( // Don't know what goes here and
    > > > > > > why // ) {
    > > > > > > statements here;

    >
    > > > > > > }

    >
    > > > > > > Can anyone help T.T?

    >
    > > > So when it says "the method getCoord takes one string parameter, does
    > > > that mean is should be:

    >
    > > > public static string getCoords

    >
    > > No, it means:

    >
    > > public static getCoords(String param) {

    >
    > > }
    > > > and then it says "prompt for and performs input of one floating point
    > > > value representing a coordinate of a point" does it mean that it would
    > > > be?:

    >
    > > > public static string getCoords(float coord){

    >
    > > > }

    >
    > > This one is wrong too. You'll need to use something to /ask/ the user
    > > for input. Use JOptionPane.showInputDialog() for a GUI input dialog
    > > box, or look up the Scanner class if you want something on the command
    > > line.

    >
    > So, other than forgetting to include the input prompt in that last
    > one, is it correct? Is it possible to use the print command in this
    > instance? Other than the opening of the method, the only other thing
    > that really confuses me is the variables inside. In a lot of examples
    > they have things like:
    >
    > public static string getCoords(float coord) {
    > getCoords = newCoord;
    >
    > }
    >
    > The variables just seem to change randomly (at least to me).
    Cameron, Mar 20, 2008
    #7
  8. KyoGaSuki

    KyoGaSuki Guest

    On Mar 20, 11:12 am, Cameron <> wrote:
    > Hope this makes sense... It just takes the coords and prints em out
    > via another method.
    > as long as u reference the variables properly in the new method, you
    > should have no problem
    >
    > /*takes x and y coords and prints them out*/
    > public class coord{
    >
    > public static void main(String[] args){
    >
    > //set global var
    > double xCoord = 2.231;
    > double yCoord = 56.21;
    >
    > //call method to print out coords
    > printCoords(xCoord, yCoord);
    >
    > }//end of main
    >
    > /*in the constructor u can call xCoord or yCoord anything you like
    > *as long as u reference it properly in the method
    > */
    > public static void printCoords(double xCoord, double yCoord){
    >
    > //print out coord values
    > System.out.println(xCoord+" "+yCoord);
    >
    > }//end of method
    >
    > }//end of class
    >
    > On Mar 20, 1:13 pm, KyoGaSuki <> wrote:
    >
    > > On Mar 20, 5:29 am, Abdullah <> wrote:

    >
    > > > On Mar 20, 1:23 pm, KyoGaSuki <> wrote:

    >
    > > > > On Mar 20, 3:35 am, Mark Vismonte <> wrote:

    >
    > > > > > or say you wanted something to print out

    >
    > > > > > public void printThis(String z)
    > > > > > {
    > > > > > System.out.println(z);

    >
    > > > > > }

    >
    > > > > > On Mar 20, 12:31 am, Mark Vismonte <> wrote:

    >
    > > > > > > those would be the parameters say if you wanted a method called addOne

    >
    > > > > > > public static int addOne(int firstNumber)
    > > > > > > {
    > > > > > > return firstNumber + 1;

    >
    > > > > > > }

    >
    > > > > > > On Mar 19, 11:02 pm, KyoGaSuki <>
    > > > > > > wrote:

    >
    > > > > > > > So we have a test tomorrow on writing methods, and I thought I
    > > > > > > > understood it...until I looked at some examples and realized....I
    > > > > > > > don't know how to actually write one.

    >
    > > > > > > > So far I know:

    >
    > > > > > > > public static void methodName( // Don't know what goes here and
    > > > > > > > why // ) {
    > > > > > > > statements here;

    >
    > > > > > > > }

    >
    > > > > > > > Can anyone help T.T?

    >
    > > > > So when it says "the method getCoord takes one string parameter, does
    > > > > that mean is should be:

    >
    > > > > public static string getCoords

    >
    > > > No, it means:

    >
    > > > public static getCoords(String param) {

    >
    > > > }
    > > > > and then it says "prompt for and performs input of one floating point
    > > > > value representing a coordinate of a point" does it mean that it would
    > > > > be?:

    >
    > > > > public static string getCoords(float coord){

    >
    > > > > }

    >
    > > > This one is wrong too. You'll need to use something to /ask/ the user
    > > > for input. Use JOptionPane.showInputDialog() for a GUI input dialog
    > > > box, or look up the Scanner class if you want something on the command
    > > > line.

    >
    > > So, other than forgetting to include the input prompt in that last
    > > one, is it correct? Is it possible to use the print command in this
    > > instance? Other than the opening of the method, the only other thing
    > > that really confuses me is the variables inside. In a lot of examples
    > > they have things like:

    >
    > > public static string getCoords(float coord) {
    > > getCoords = newCoord;

    >
    > > }

    >
    > > The variables just seem to change randomly (at least to me).


    You...are amazing! Thank you SO much!
    KyoGaSuki, Mar 20, 2008
    #8
  9. KyoGaSuki

    Cameron Guest

    forget about the global variable comment i put in ..


    public static void main(String[] args){

    //set global var
    double xCoord = 2.231;
    double yCoord = 56.21;

    global variables go inbetween the "public class myClass" and the main
    constructor. (global vars come in quite handy when you move onto
    swing )
    Cameron, Mar 20, 2008
    #9
  10. KyoGaSuki

    KyoGaSuki Guest

    On Mar 20, 11:51 am, Cameron <> wrote:
    > forget about the global variable comment i put in ..
    >
    > public static void main(String[] args){
    >
    > //set global var
    > double xCoord = 2.231;
    > double yCoord = 56.21;
    >
    > global variables go inbetween the "public class myClass" and the main
    > constructor. (global vars come in quite handy when you move onto
    > swing )


    You have all been so helpful, thank you n.n. Just one more question,
    though. Can someone give me a basic (like...the most beginner-form)
    example of calling a method? I think I have got down the basics of
    writing a method, but I need a couple of examples of calling that
    method.
    KyoGaSuki, Mar 20, 2008
    #10
  11. KyoGaSuki

    Lew Guest

    Cameron wrote:
    >> forget about the global variable comment i put in ..
    >>
    >> public static void main(String[] args){
    >>
    >> //set global var
    >> double xCoord = 2.231;
    >> double yCoord = 56.21;
    >>
    >> global variables go inbetween the "public class myClass" and the main
    >> constructor. (global vars come in quite handy when you move onto
    >> swing )


    These aren't global variables. As you explain them, they become instance
    variables. Anyway, global variables are a Bad Thing.

    And that's "Swing".

    And class names are supposed to start with an upper-case letter.

    KyoGaSuki wrote:
    > You have all been so helpful, thank you n.n. Just one more question,
    > though. Can someone give me a basic (like...the most beginner-form)
    > example of calling a method? I think I have got down the basics of
    > writing a method, but I need a couple of examples of calling that
    > method.


    There are two ways, depending on whether it's a static method or not.

    If it's a static method, you call it with the class name, a dot. the method
    name, and parentheses enclosing any arguments. If it's an instance method,
    you call it with an object reference. a dot, then the method name and
    parentheses, with arguments if any. Here's a sample class with a static and a
    non-static (instance) method, declared 'public' so the client can use them:

    public class Methodist
    {
    private double value;

    /** Static method that takes an double and returns a value.
    * @param xp <code>double</code> argument.
    * @return double the result.
    */
    public static double square( double xp )
    {
    return xp * xp;
    }

    /** Instance method that takes no arguments and returns the internal value.
    * @return double the internal value.
    */
    public double getValue()
    {
    return value;
    }

    /** Instance method that stores the argument in the internal value
    * and returns nothing.
    * @param val <code>double</code> to store in the internal value.
    */
    public void setValue( double val )
    {
    this.value = val;
    }

    }

    public class TestMethodist
    {
    /** Static method that process an array of <code>String</code>s.
    * @param args <code>String []</code> arguments to process.
    */
    public static void main( String [] args )
    {
    double sq = Methodist.square( Math.PI ); // invoke a static method

    Methodist holder = new Methodist(); // an instance is required

    holder.setValue( sq ); // invoke an instance method
    sq = holder.getValue(); // invoke an instance method

    System.out.println( "Internal value is "+ sq );
    }
    }

    --
    Lew
    Lew, Mar 21, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    571
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    964
    Mabden
    Sep 2, 2004
  3. WX
    Replies:
    4
    Views:
    2,615
    Serge Orlov
    Feb 13, 2005
  4. KyoGaSuki

    Writing a method [beginner]

    KyoGaSuki, Mar 20, 2008, in forum: Java
    Replies:
    0
    Views:
    362
    KyoGaSuki
    Mar 20, 2008
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    212
    7stud --
    Apr 12, 2011
Loading...

Share This Page