Results 1 to 6 of 6

Thread: qu

  1. #1
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default qu

    public class peiramus {
    public void main (String [] args) { /*inside here i write the sequence of the method i want right? or its not necessary? (can i leave it empty? is it gonna follow the flow of the text (reading 'word by word'?)? */
    method1 (); //this is OK
    method2 (); //but here it doesnt accept them (its obvious it has to do with the fact that i 'put things' inside the parentheses) so what i do here?
    method3 ();

    }
    private void method1 () {
    blah blah
    }
    private void method2 (String [] Array1){ //taken from method1
    blah blah
    }
    private void method3 (String [] DictionaryArray, int method4){ //taken form method 2 and method 4)
    blah blah
    private int method4 (blah blah){
    if.. blah blah
    blah blah.. return -1; //send the result to method3 }
    Last edited by Gilgamesh; 11-29-2007 at 01:13 AM.

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    private void method2 (String [] Array1){ ... }
    That method accepts an Array of Strings in its argument. So if you make an array you can put it in there.

    edit:
    Well all those methods are void, so doing:
    Java Code:
    method1 ();
    method2 ();
    is okay, since they dont return anything.

    But method3() returns an integer, so i am assuming that you want to do something with it.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.util.*;
    
    public class ExpTest {
        public static void main(String[] args) {
            ExpTest test = new ExpTest();
            String[] s = test.method1();
            System.out.printf("s    = %s%n", Arrays.toString(s));
            String[] caps = test.method2(s);
            System.out.printf("caps = %s%n", Arrays.toString(caps));
            int n = test.method4(s);
            System.out.println("n = " + n);
            test.method3(caps, n);
        }
    
        private String[] method1() {
            return new String[] { "hello", "world" };
        }
    
        //taken from method1
        private String[] method2(String[] array1) {
            String[] returnVal = new String[array1.length];
            for(int j = 0; j < array1.length; j++) {
                String s = "";
                for(int k = 0; k < array1[j].length(); k++) {
                    s += String.valueOf(array1[j].charAt(k)).toUpperCase();
                }
                returnVal[j] = s;
            }
            return returnVal;
        }
    
        //taken form method 2 and method 4
        private void method3(String[] array, int method4) {
            System.out.println("Selected index = " + method4 +
                               "  value = " + array[method4]);
        }
    
        //send the result to method3
        private int method4(String[] array) {
            int index= 0;
            if(array.length % 2 == 0)
                index = array.length-1;
            return index;
        }
    }

  4. #4
    Gilgamesh is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    :) well my question was focusing on the red comments (i.e. how we organize the main class). I have finished my coding. My problem is how to refer methods that they have arguments in the main method. I can put easily methods with no arguments, like
    Java Code:
    method1();
    in the main method, but I cant refer the methods that have arguments inside the main method.

    Java Code:
    public void main (String [] args) { 
    method1 (); //here ok, no problem.
    method2 (arguments* here e.g. String string, String [] array etc); //[COLOR="Red"]if I do this java starts yelling at me[/COLOR]
    }
    *I mean the same arguments thar are necessary to write the method2. Do we write them as arguments when we refer method2 at the main method? OR we are supposed to write inside the parentheses, the 'result' of the method? OR nothing (but anyway if I just type method2(); it doesnt accept it)?
    Last edited by Gilgamesh; 11-29-2007 at 01:14 AM.

  5. #5
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    You do it exactly the way hardwired showed you
    Java Code:
    ExpTest test = new ExpTest();
    The main method in any java program is static. That means that you cant call non-static methods from it (ie: method1()). You have to first create an instance of that class (the code above) then execute the methods:
    Java Code:
    test.method();
    //taken from method1
    How do you mean? method1 is void, it doesn't return anything.

    Read over hardwired more carefully.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    method2(arguments* here e.g. String string, String [] array etc)
    Java Code:
    String s = "hello world";
    // use "s" as an argument to the [i]takesAString[/i] method:
    takesAString(s);
    // To call a method that takes a Rectangle argument
    // make a Rectangle
    Rectangle square = new Rectangle(100,100,200,200);
    // and pass it to the method
    takesRectangle(rect);
    ...
    void takesAString(String str) {
        // do something with "str"
    }
    
    void takesRectangle(Rectangle r) {
        // do something with "r"
    }

Posting Permissions

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