Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A very simple program

Posted on 2004-10-30
22
Medium Priority
?
238 Views
Last Modified: 2010-03-31
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()?
0
Comment
Question by:daveask
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 7
  • 3
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453375
public static void main
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 600 total points
ID: 12453379

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
}
0
 
LVL 13

Assisted Solution

by:petmagdy
petmagdy earned 200 total points
ID: 12453390
ok,

try this class:

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

/*
 * Created on 30/10/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class MyClass {

      public Hashtable addressBook;
      
      public MyClass()
      {
            addressBook = new Hashtable();
      }
      
      public static void main(String[] args) {
            MyClass myClass = new MyClass();
            myClass.addAddressBook("John", "6765765756");
            myClass.displayAddressBook();
      }
      
      public void addAddressBook(String name, String telephone )
      {
            addressBook.put(name, telephone);
      }
      
      public void displayAddressBook()
      {
             Set set=addressBook.keySet();
              Iterator iterator=set.iterator();
              while(iterator.hasNext())
              {
                  Object key=iterator.next();
                  System.err.println("Name : "+key);
                  System.err.println("Telephone :"+addressBook.get(key));
              }
            
      }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daveask
ID: 12453392
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?
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12453426
like this:

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


0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12453430
daveask, u can also use my class skeleton
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453433
>>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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453441
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
      }
}      
      
      
0
 

Author Comment

by:daveask
ID: 12453594
CEHJ,
Thank you.
I might need to set many values to myArray in a loop! How can I initialise values like what you said?
0
 

Author Comment

by:daveask
ID: 12453606
petmagdy,

As you can see, I know only little in Java....why your class skeleton is so complex?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453615
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;
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453619
That would give you a 10 X 10 array of random numbers below 100
0
 

Author Comment

by:daveask
ID: 12453640
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];
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453652
>>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'
0
 

Author Comment

by:daveask
ID: 12453725
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)
     {  
     }
}    
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453732
You must save the source file as MyClass.java, and it's case-sensitive
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453737
If you're in the same directory

javac MyClass.java

java MyClass
0
 

Author Comment

by:daveask
ID: 12453765
Yes, you are right, case-sensitive!

Now it said
Exception in thread "main" Java.lang.NoClassDefFoundError: c:\...MyClass
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453779
java -classpath . MyClass
0
 

Author Comment

by:daveask
ID: 12453886
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.....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453912
Please post your command line and say in what directory your class and source are
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12453980
8-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question