public methods

In the below MaxMinDifference class, near the top i try to call the print method. The print method is located in another class called ArrayShuffle. The ArrayShuffle class is located in another folder (not the same directory as MaxMinDifference).

The problem is i get errors pointing to the print method when i try to compile the MinMaxDifference class. Does anyone know what the problem is?

class MaxMinDifference

{

publicstaticvoid main(String[] args)

{

int[] nums ={34,76,22,99,4,66,12};

int[] nums2 ={50,30,150,80,120,70};

ArrayShuffle.print(nums);

ArrayShuffle.print(nums2);

System.out.print("The difference between the largest and smallest number ");

System.out.println("in the array is "+range(nums) );

System.out.print("The difference between the largest and smallest number ");

System.out.println("in the array is "+range(nums2) );

}

publicstaticint range(int[] x){

int min = x[0], max = x[0];

for(int i = 0; i < x.length; i++)

if(x[i] < min) min = x[i];

elseif(x[i] > max) max = x[i];

return max - min;

}

}

the print method in ArrayShuffle.

publicstaticvoid print(int[] a){

for(int i = 0; i < a.length; i++){

System.out.print(a[i]+" ");

}

System.out.println();

System.out.println();

}

MaxMinDifference.java:11: cannot resolve symbol

symbol : variable ArrayShuffle

location: class MaxMinDifference

ArrayShuffle.print(nums);

MaxMinDifference.java:12: cannot resolve symbol

symbol : variable ArrayShuffle

location: class MaxMinDifference

ArrayShuffle.print(nums2);

^

2 errors

[3003 byte] By [mark_8206a] at [2007-11-26 21:55:05]
# 1
By "another folder" I guess you mean that ArrayShuffle is in a different package. You need to import it so the compiler knows where to find it. http://java.sun.com/docs/books/tutorial/java/package/packages.html
jverda at 2007-7-10 3:50:35 > top of Java-index,Java Essentials,New To Java...
# 2

And, in anticipation of your next question....

[url=http://wiki.java.net/bin/view/Javapedia/ClassPath]Javapedia: Classpath[/url]

[url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html]How Classes are Found[/url]

[url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html]Setting the class path (Windows)[/url]

[url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html]Setting the class path (Solaris/Linux)[/url]

[url=http://www-106.ibm.com/developerworks/edu/j-dw-javaclass-i.html]Understanding the Java ClassLoader[/url]

java -cp .;<any other directories or jars> YourClassName

You get a NoClassDefFoundError message because the JVM (Java Virtual Machine) can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

javac -classpath .;<any additional jar files or directories> YourClassName.java

You get a "cannot resolve symbol" message because the compiler can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

jverda at 2007-7-10 3:50:35 > top of Java-index,Java Essentials,New To Java...
# 3
the problem is that it's in another folder. you need to learn about packages, and where to put things. you can't just throw a whole load of files in random places at the compiler and hope it sorts them out!
georgemca at 2007-7-10 3:50:35 > top of Java-index,Java Essentials,New To Java...
# 4
no, it's not in another package, it's located in another folder.i would have put it in a package but i had big problems trying to get the program to run.
mark_8206a at 2007-7-10 3:50:35 > top of Java-index,Java Essentials,New To Java...
# 5
> no, it's not in another package, it's located in> another folder.Then that folder has to be on your classpath.
jverda at 2007-7-10 3:50:35 > top of Java-index,Java Essentials,New To Java...
# 6

> the problem is that it's in another folder. you need

> to learn about packages, and where to put things. you

> can't just throw a whole load of files in random

> places at the compiler and hope it sorts them out!

if i make packages, can i run the programs in them with the jdk commander? or do i need to run them with the dos window?

mark_8206a at 2007-7-10 3:50:35 > top of Java-index,Java Essentials,New To Java...
# 7

> > the problem is that it's in another folder. you

> need

> > to learn about packages, and where to put things.

> you

> > can't just throw a whole load of files in random

> > places at the compiler and hope it sorts them out!

>

> if i make packages, can i run the programs in them

> with the jdk commander? or do i need to run them with

> the dos window?

stop guessing at things and read some documentation. it will save you hours of frustration

georgemca at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 8

You [url=http://forum.java.sun.com/thread.jspa?threadID=5148882&messageID=9556527#9556527]just[/url] [url=http://forum.java.sun.com/thread.jspa?threadID=5148790&messageID=9556060#9556060]don't[/url] [url=http://forum.java.sun.com/thread.jspa?threadID=5148707&messageID=9555601#9555601]get[/url] packages, and you never will if you don't start using the freakin' command prompt. I have tried to help you, and you have such a dread of the command line that I have given up.

We don't support JDK Commander, whatever the hell it is.

kevjavaa at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 9
Does anyone want to muddy the waters more by pointing out that code in different folders can be, in fact, in the same package, and this is sometimes a useful thing?
DrLaszloJamfa at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 10

> Does anyone want to muddy the waters more by pointing

> out that code in

> different folders can be, in fact, in the same

> package, and this is sometimes

> a useful thing?

yes. you do :-)

one big problem is that people confuse package structures with filesystem structures. packages appear to map directly to directory structures, because it was a sensible implementation choice for storing classes on disk, but a folder is not a package, and a package is not a folder

georgemca at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 11

> and you never will if you don't start using the

> freakin' command prompt. I have tried to help you,

> and you have such a dread of the command line that I

> have given up.

>

> We don't support JDK Commander,

i can get most of the my programs to work when i use the command prompt, but when i put them into a package, for some reason they don't run.

mark_8206a at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 12

> > and you never will if you don't start using the

> > freakin' command prompt. I have tried to help

> you,

> > and you have such a dread of the command line that

> I

> > have given up.

> >

> > We don't support JDK Commander,

>

> i can get most of the my programs to work when i use

> the command prompt, but when i put them into a

> package, for some reason they don't run.

the reason being, you don't understand packages properly. seriously, trying to sort this exact mess out is a waste of time, you'll only be back in the same frustrating position next time. do yourself a favour and take some time off coding to read a [url=http://www.jarticles.com/package/package_eng.html]tutorial[/url]

georgemca at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 13

> i can get most of the my programs to work when i use

> the command prompt, but when i put them into a

> package, for some reason they don't run.

Read this.

http://java.sun.com/docs/books/tutorial/java/package/packages.html

Especially:

http://java.sun.com/docs/books/tutorial/java/package/managingfiles.html

Grok it. Become one with it.

kevjavaa at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...
# 14

> i can get most of the my programs to work when i use

> the command prompt, but when i put them into a

> package, for some reason they don't run.

Then read the link I provided on packages and the ones I provided on classpath.

Start simple: Put everything in a package--the same package--with only one level, say package pack;

. Create two classes in two different directories, say, C:\dir1\pack and C:\dir2\pack. Have one of the classes use the other.

Set your classpath correctly to get that to work. If you can't get it to work, provide all the details about what's where, what your code looks like, what command you ran.

Then, create another class in another package, say, pack.otherpack, and have a pack class use that one.

***BUT DO READ THE LINKS FIRST.***

jverda at 2007-7-10 3:50:36 > top of Java-index,Java Essentials,New To Java...