www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: help with my simple method :( please

  1. #1
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665

    help with my simple method :( please

    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.

    PHP Code:

    <?php

    function bueza_foo($value) {
       do 
    stuff here

        
    return $value;
    }

    $value $_GET['value'];
    $output bueza_foo($value);
    echo 
    $output;

    ?>
    How would I do the equivelent for Java??

    1) Make function
    2) Make it available to do certain things and then return a value??

    "Everything in a web browser."

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    What have you tried so far?

    You can't expect us to just hand you your homework. We will help you with it, but not do it for you.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    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.

    "Everything in a web browser."

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    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.

  5. #5
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    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.

    "Everything in a web browser."

  6. #6
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    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.

  7. #7
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    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.

  8. #8
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Yer, thats right. The static method can to executed without declaring an instance of the object its within.

    Regards.

  9. #9
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    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);
    }
    How do I use the method??


    just...

    getAverage(); ??

    "Everything in a web browser."

  10. #10
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    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.
    Code:
    System.out.println(Integer.toString(
        getAverage(1, 2, 3, 4, 5, 5)));
    Regards.

  11. #11
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    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.

  12. #12
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    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)

  13. #13
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Okay. I've coded something up like this.

    Code:
    // 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.

    "Everything in a web browser."

  14. #14
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    Oops, how do you return more than one value???

    "Everything in a web browser."

  15. #15
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles