Results 1 to 6 of 6
Thread: qu
- 11-28-2007, 10:51 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
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 12:13 AM.
- 11-28-2007, 11:23 PM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
That method accepts an Array of Strings in its argument. So if you make an array you can put it in there.Java Code:private void method2 (String [] Array1){ ... }
edit:
Well all those methods are void, so doing:
is okay, since they dont return anything.Java Code:method1 (); method2 ();
But method3() returns an integer, so i am assuming that you want to do something with it.
- 11-28-2007, 11:31 PM #3
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; } }
- 11-29-2007, 12:05 AM #4
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
:) 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
in the main method, but I cant refer the methods that have arguments inside the main method.Java Code:method1();
*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)?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] }Last edited by Gilgamesh; 11-29-2007 at 12:14 AM.
- 11-29-2007, 12:15 AM #5
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
You do it exactly the way hardwired showed you
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:ExpTest test = new ExpTest();
//taken from method1Java Code:test.method();
How do you mean? method1 is void, it doesn't return anything.
Read over hardwired more carefully.
- 11-29-2007, 01:15 AM #6
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" }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks