Link to home
Start Free TrialLog in
Avatar of daveask
daveask

asked on

A very simple program

Hi Experts,

I am downloading J2sdk and eclipse-sdk to write and run Java code. In the same time, can you tell me how to write a very simple program something like:

void main(String args[])
{   double[][] myArray;
    //set some values to myArray
    call MySubroutine(myArray);
    system.out.print myArray;
{
void MySubroutine(myArray)
{   //calculat and change something in myArray
}

And do I need to use Class above void main() and public static before void main()?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

public static void main
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daveask
daveask

ASKER

CEHJ,

Thank you for so prompt response!

So, you said I don't need to use Class here, why?
Why I need put static before void?
Notice, I used a two dimesional array double[][], how can I initialise like what you suggested?
like this:

            double[][] myArray = { {1.0, 2.0} , {3.0, 4.0} };


daveask, u can also use my class skeleton
>>So, you said I don't need to use Class here, why?

No you do - i assumed you knew that.

>>Why I need put static before void?

Because a Java application is started by this method before any instance is present. This is necessary
public class X
{
      public static void main(String args[])
      {  double[][] myArray = { YOU MUST INITIALISE HERE ! };
         //set some values to myArray
         MySubroutine(myArray);
         for(int i = 0;i < myArray.length;i++)
         {
            System.out.println(myArray[i]);

         }

      }
      static void MySubroutine(double[][] myArray)
      {   //calculate and change something in myArray
      }
}      
      
      
Avatar of daveask

ASKER

CEHJ,
Thank you.
I might need to set many values to myArray in a loop! How can I initialise values like what you said?
Avatar of daveask

ASKER

petmagdy,

As you can see, I know only little in Java....why your class skeleton is so complex?
Yes, you could do something like:

double[][] myArray = new double[10][10];
for(int i = 0;i < myArray.length;i++) {
      for(int j = 0;j < myArray[i].length;j++) {
            myArray[i][j] = Math.random() * 100;
      }
}
That would give you a 10 X 10 array of random numbers below 100
Avatar of daveask

ASKER

CEHJ,

Thank you. That is great!
Can you also tell me how to set the array size dynamically:

double[][] myArray;
int mySize = 10;
myArray = new double[mySize][mySize];
>>Can you also tell me how to set the array size dynamically:

Your own code example does just that, and since the code in my loops is *not* hard-coded, it will work with any valid value that you give to 'mySize'
Avatar of daveask

ASKER

OK, I can run my program now!
However, I got error:
cannot read: MyClass.java
1 error
I did nothing in my program:
public class MyClass
{
     public static void main(String args[])
     {  double[][] myArray = new double[10][10];
        MySubroutine(myArray);
        for(int i = 0;i < myArray.length;i++)
        {
        }
     }
     static void MySubroutine(double[][] myArray)
     {  
     }
}    
You must save the source file as MyClass.java, and it's case-sensitive
If you're in the same directory

javac MyClass.java

java MyClass
Avatar of daveask

ASKER

Yes, you are right, case-sensitive!

Now it said
Exception in thread "main" Java.lang.NoClassDefFoundError: c:\...MyClass
java -classpath . MyClass
Avatar of daveask

ASKER

I tried every thing, but still got the same exception......I tried to put MyClass.class in the bin\ and tried to set path and classpath as the other expert suggested but none of them works.....
Please post your command line and say in what directory your class and source are
8-)