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()?
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()?
public static void main
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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} };
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
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
}
}
{
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
}
}
static void MySubroutine(double[][] myArray)
{ //calculate and change something in myArray
}
}
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?
Thank you.
I might need to set many values to myArray in a loop! How can I initialise values like what you said?
ASKER
petmagdy,
As you can see, I know only little in Java....why your class skeleton is so complex?
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;
}
}
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
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];
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'
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'
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)
{
}
}
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
javac MyClass.java
java MyClass
ASKER
Yes, you are right, case-sensitive!
Now it said
Exception in thread "main" Java.lang.NoClassDefFoundE rror: c:\...MyClass
Now it said
Exception in thread "main" Java.lang.NoClassDefFoundE
java -classpath . MyClass
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-)