Okay, I've been doing php for a long time. I love it. I know about how to make functions that return things. However, I'm doing Java in school now...and I need help.
lol, in the php code above, it practically explains what I want to convert to java.
is it with public static? private static?
I don't know how to start to create a method.
In PHP, I would just do function NameOfFunction (parameters), etc.
My thread isn't about handing me my homework. My question was simple. How do I make a method?
Edit: Jeff, you can consider your reply above this one as "spam" because you didn't even bother to state resources for me look at (websites that have tutorials). My thread isn't spam because I can't even do a search for my question. This board is completely new.
Sorry, but by you saying "we're not going to hand you your homework" to me; that just proves to me that you didn't read my thread correctly.
Edit again: Alright Jeff, it helps you out THAT much to answer my simple question.
Code:
import cs1.Keyboard;
public class MyFirstMethod
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Took like 10 seconds. I'm pissed off at people who try to answer my question with another question. Seriously.
Last edited by BuezaWebDev; 10-16-2004 at 08:52 PM.
public ReturnType functionName(DataType varName)
{
// code
return value;
}
// Example
public int getSum(int x, int y)
{
return (x + y);
}
public - the method can be access from anywhere in your application.
private - can only be accessed from methods in the same class object.
protected - can be access from any object in the same package. left blank - similar to public. You can look up the difference.
If you make your method static, it will be executed the moment the object is created. Only use static when it is necessary.
Originally posted by buntine The basic syntax for a Java method is as follows
Code:
public ReturnType functionName(DataType varName)
{
// code
return value;
}
// Example
public int getSum(int x, int y)
{
return (x + y);
}
public - the method can be access from anywhere in your application.
private - can only be accessed from methods in the same class object.
protected - can be access from any object in the same package. left blank - similar to public. You can look up the difference.
If you make your method static, it will be executed the moment the object is created. Only use static when it is necessary.
Regards.
Perfect solution. Thank you Buntine.
To Jeff: Sorry for exploding on you--but believe it or not, I know how to ask a question and I knew that posting the code that had prior to Buntine's solution would be pointless because every java program starts with that.
I interpretted your response as a blatant attack against my intelligence. "We aren't going to hand you your homework"--Well, duh.
Again, I apologize.
EDIT
I have a question...with
Code:
public int getAverage(int a, int b, int c, int d, int e, int f)
{
int average = ((a+b+c+d+e) / f);
return average;
}
output = getAverage(1,2,3,4,5,5);
System.out.println(output);
//the output should be the average of 1+2+3+4+5 / 5 = 3
// The output will be 3.
Is that true? or am I totally doing this wrong?
Last edited by BuezaWebDev; 10-16-2004 at 09:43 PM.
Looks fine to me. You could shorted your method to one line if you want.
It will provide no real performance gain, its just a matter of personal preferance.
Code:
public int getAverage(int a, int b, int c, int d, int e, int f)
{
return ((a+b+c+d+e) / f);
}
Originally posted by buntine If you make your method static, it will be executed the moment the object is created. Only use static when it is necessary.
I don't think so. If you have an anonymous static block then *it* will be executed when the class is loaded but a static method (a class method) has to be called like any other function in order to be executed. We agree on the point that one should have a good reason for declaring anything static.
Originally posted by buntine Looks fine to me. You could shorted your method to one line if you want.
It will provide no real performance gain, its just a matter of personal preferance.
Code:
public int getAverage(int a, int b, int c, int d, int e, int f)
{
return ((a+b+c+d+e) / f);
}
Regards.
Code:
public int getAverage(int a, int b, int c, int d, int e, int f)
{
return ((a+b+c+d+e) / f);
}
No, use it in the same way you demonstrated before. There is just no need to create a variable in the method if its not going to be used ot altered at a later point.
Jaime, I know you aren't coming up with those examples since you're taking a class but they are absolutely horrible if taken in the context of real world Java programming. They're just C. E.g. a method to get an average only makes sense as part of some kind of collection like a collection of temperature readings. A decent Java programmer would never create a stand alone average calculator.
Originally posted by buntine Yer, thats right. The static method can to executed without declaring an instance of the object its within.
The latest version of our development tool has gotten even pickier than that. If you have a static method then it will complain if you call it relative to the object rather than the class. "not called in a static way" it whines. 8)
// Program will ask user to input a word
// Program will ask user to input a number of times to repeat the word
import cs1.Keyboard;
public class booya
{
public String RepeatWord(String iWord, int iRepeat)
{
if (iRepeat < 1) {
System.out.println("I cannot repeat the word less than 1 time. That's physically impossible.");
} else {
for (int count=1; iRepeat <= count; count++) {
do {
count++;
System.out.println (iWord);
} while (iRepeat <= count);
}
}
}
public static void main(String[] args)
{
System.out.println("Please enter the word that you wish to display: ");
String iWord = Keyboard.readString();
System.out.println("Please enter how many times you wish to repeat it: ");
int iRepeat = Keyboard.readInt();
System.out.println (RepeatWord(iWord, iRepeat));
}
}
However...it's spitting out one error.
System.out.println (RepeatWord(iWord, iRepeat));
Any suggestions?
Last edited by BuezaWebDev; 10-17-2004 at 12:07 AM.
There are a couple of ways to return more than one value.
If the values are all the same type, it can be done by putting them in an array. If they're not, you can still put them in an array of type Object (if you are going to put primatives like byte, short, int, long, float, double, char, boolean, and probably some more I am forgetting, you will have to wrap them in an Object to place them in the array [see http://java.sun.com/j2se/1.4.2/docs/...e-summary.html for some of the pre-defined wrapper classes for primatives]), but this approach can get tricky, since once they're in there, to make any use of them, you will have to re-cast the references to whatever they were before, which may be a pain to figure out.
There is also a growable array-like structure in the java.util package called Vector, into which you can insert java.lang.Object. I can't off the top of my head remember if the Vector is a linked-list, or reallocating array, or what.
Other than that, you can make a custom object class with appropriate member variables to wrap the multiple return values in. Example: say you know you are always returning an int and a String. you would make a class
Code:
class MyWrapper {
int theInt;
String theString;
}
To use, you could instantiate the class, set the values, and return the object.
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
Bookmarks