Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Writing a method [beginner]

Thread view: 
KyoGaSuki - 20 Mar 2008 07:02 GMT
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 - 20 Mar 2008 08:31 GMT
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 <jrockgadaisukida...@yahoo.co.jp>
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
[quoted text clipped - 9 lines]
>
> Can anyone help T.T?
Mark Vismonte - 20 Mar 2008 08:35 GMT
or say you wanted something to print out

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

> those would be the parameters say if you wanted a method called addOne
>
[quoted text clipped - 20 lines]
>
> > Can anyone help T.T?
KyoGaSuki - 20 Mar 2008 09:23 GMT
> or say you wanted something to print out
>
[quoted text clipped - 28 lines]
>
> > > 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){

}
Abdullah - 20 Mar 2008 10:29 GMT
> > or say you wanted something to print out
>
[quoted text clipped - 33 lines]
>
> 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
[quoted text clipped - 3 lines]
>
> }

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.
KyoGaSuki - 20 Mar 2008 14:13 GMT
> > > or say you wanted something to print out
>
[quoted text clipped - 51 lines]
> 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 - 20 Mar 2008 16:12 GMT
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

> > > > or say you wanted something to print out
>
[quoted text clipped - 64 lines]
>
> The variables just seem to change randomly (at least to me).
KyoGaSuki - 20 Mar 2008 16:19 GMT
> Hope this makes sense...  It just takes the coords and prints em out
> via another method.
[quoted text clipped - 95 lines]
>
> > The variables just seem to change randomly (at least to me).

You...are amazing! Thank you SO much!
Cameron - 20 Mar 2008 16:51 GMT
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 )
KyoGaSuki - 20 Mar 2008 21:04 GMT
> forget about the global variable comment i put in ..
>
[quoted text clipped - 7 lines]
> 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.
Lew - 21 Mar 2008 03:47 GMT
Cameron wrote:
>> forget about the global variable comment i put in ..
>>
[quoted text clipped - 7 lines]
>> 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.

> 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 );
 }
}

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.