Writing a method [beginner]

K

KyoGaSuki

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?
 
M

Mark Vismonte

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

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

Mark Vismonte

or say you wanted something to print out

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

KyoGaSuki

or say you wanted something to print out

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

}

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){

}
 
A

Abdullah

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.
 
K

KyoGaSuki

No, it means:

public static getCoords(String param) {

}



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).
 
C

Cameron

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
 
K

KyoGaSuki

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

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!
 
C

Cameron

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 )
 
K

KyoGaSuki

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.
 
L

Lew

Cameron said:
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.
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 );
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top