PDA

View Full Version : Import packages to Java




kulimer
Mar 4, 2012, 11:50 PM
Hi all,
I have trouble importing any .jar packages, like this one
http://www.icsa.inf.ed.ac.uk/research/groups/hase/simjava/distributions/

I've tried putting it under src and reference library, with command

import eduni.*;
or
import java.eduni.*;

What is going on? Has anyone imported successfully? How did you do it? thanks!



wrldwzrd89
Mar 5, 2012, 06:58 AM
Hi all,
I have trouble importing any .jar packages, like this one
http://www.icsa.inf.ed.ac.uk/research/groups/hase/simjava/distributions/

I've tried putting it under src and reference library, with command

import eduni.*;
or
import java.eduni.*;

What is going on? Has anyone imported successfully? How did you do it? thanks!
Remember, a star import does NOT also import subpackages of the package you're importing. It merely imports all classes in that package. Since the eduni package doesn't have any classes in it, this will fail.

kulimer
Mar 5, 2012, 11:35 AM
Remember, a star import does NOT also import subpackages of the package you're importing. It merely imports all classes in that package. Since the eduni package doesn't have any classes in it, this will fail.

I could be doing something wrong there, but I've also tried, which also didn't work:
1. SSJ from University of Montreal
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html

2. The JSC library
http://www.jsc.nildram.co.uk/downloads/download.html

On another onte, I was told by someone else "JRE requirements are different for [different] packages".

while someone else said, "It ought to work on any version newer than Java5", which I think the Lion has?

wrldwzrd89
Mar 5, 2012, 11:56 AM
I could be doing something wrong there, but I've also tried, which also didn't work:
1. SSJ from University of Montreal
http://www.iro.umontreal.ca/~simardr/ssj/indexe.html

2. The JSC library
http://www.jsc.nildram.co.uk/downloads/download.html

On another onte, I was told by someone else "JRE requirements are different for [different] packages".

while someone else said, "It ought to work on any version newer than Java5", which I think the Lion has?
Nope. Lion has Java 6...

naples98
Mar 5, 2012, 12:31 PM
1. Can you post some code to show how you are using the import statements and describe what errors you are getting?

2. What IDE are you using? Have you added the jars to your classpath?

kulimer
Mar 5, 2012, 01:35 PM
1. Can you post some code to show how you are using the import statements and describe what errors you are getting?

2. What IDE are you using? Have you added the jars to your classpath?

Would the screenshot below help?

I am using Eclipse (Ver. Helios Service Release 2)

I right clicked GibbsSampling > Java Build Path > Add External JARs, does that sound like the normal procedure?

I've also tried, import eduni.*;
which the screenshot doesn't show.

kulimer
Mar 5, 2012, 01:55 PM
I think I figured it out, the files should be located under "/SRC" NOT under "/Referenced Libraries".

And then, add External Jars. Won't work if it's "/Referenced Libraries".

naples98
Mar 5, 2012, 02:08 PM
I think I figured it out, the files should be located under "/SRC" NOT under "/Referenced Libraries".

And then, add External Jars. Won't work if it's "/Referenced Libraries".

I usually put external jars in a "lib" folder and then add them to the build path. The "/Referenced Libraries" you refer to is not the location of the jars but just that your project is using them.

kulimer
Mar 5, 2012, 02:25 PM
I usually put external jars in a "lib" folder and then add them to the build path. The "/Referenced Libraries" you refer to is not the location of the jars but just that your project is using them.

Now I have them all in, still having some trouble using it.
API for the JSC package:
http://www.jsc.nildram.co.uk/api/jsc/distributions/Gamma.html

I want to generate numbers from gamma(shape=2.5, scale=0.5).
I have this:

double theta;
theta = rand.Gamma(2.5, 3);

I get "rand cannot be resolved", how should I use it? Unlike R, there are no reproducible examples.

Screenshot has the codes.

naples98
Mar 5, 2012, 02:29 PM
"rand" is a variable name that you have never declared. You need to declare it, just like "theta" before you can call methods on it.

Edit: I don't think you want to use "rand" rather "g" on line 15.

Edit #2: I keep looking at this and thinking about ways to explain it without just giving you the answer but...

"g" is of type Gamma and the class Gamma has methods. I don't know what methods Gamma has but you are probably looking for something like

theta = g.calculateTheta(2.5, 2);

which means you are calling the calculateTheta method on the instance of Gamma called "g".

kulimer
Mar 5, 2012, 02:52 PM
"rand" is a variable name that you have never declared. You need to declare it, just like "theta" before you can call methods on it.

Edit: I don't think you want to use "rand" rather "g" on line 15.

I don't know how to use Rand along with gamma, any clues? (API link in the last post)

So, I wrote:
Gamma g = new Gamma();
Random r = new Random();
theta = r.g(2.5, 3);

error: in the last line, under g
"The method g(double, int) is undefined for the type Random"

naples98
Mar 5, 2012, 03:32 PM
When you have an instance of a class stored in a variable, in this case "g", and you want to use methods for that class, you use "g.methodName()" where methodName is the method you want to use.

In the case of the Gamma class, it has 1 method called "sample" which returns a random number so you would call "g.sample()" to use that method.

I don't know why you think you have to use the Random class. :confused:

kulimer
Mar 5, 2012, 03:37 PM
theta = g.calculateTheta(2.5, 2);

which means you are calling the calculateTheta method on the instance of Gamma called "g".
yeah, what you are saying makes sense. But something is not right.

First, call Gamma, so, I have g. Followed by, dot r, where r means random. I have,
theta = g.r(2.5, 3.0);
Also, should I declare
Rand r = new Rand();
//or
Random r = new Random();
I'm going with the first one, it's the jsc method, but I get red underline under Rand.

naples98
Mar 5, 2012, 03:51 PM
First, call Gamma, so, I have g. Followed by, dot r, where r means random. I have,
theta = g.r(2.5, 3.0);

The class Gamma does NOT have a method called "r" so theta = g.r(2.5, 3.0); is not going to work. Also, you don't "call Gamma", you call a "method from the class Gamma".

Also, should I declare
Rand r = new Rand();
//or
Random r = new Random();
I'm going with the first one, it's the jsc method, but I get red underline under Rand.

The first one may give you an instance of Rand if it has a no-arg constructor but I don't know what the class Rand is or where you are getting it from. The second will correctly give you an instance of Random although I don't see anywhere in your code that requires it. Exactly what are you looking at that makes you think you need to use Random (or Rand).

kulimer
Mar 5, 2012, 05:03 PM
The class Gamma does NOT have a method called "r" so theta = g.r(2.5, 3.0); is not going to work. Also, you don't "call Gamma", you call a "method from the class Gamma".

The first one may give you an instance of Rand if it has a no-arg constructor but I don't know what the class Rand is or where you are getting it from. The second will correctly give you an instance of Random although I don't see anywhere in your code that requires it. Exactly what are you looking at that makes you think you need to use Random (or Rand).


exactly, like you said! To generate random I should use
g.random()
And I don't need to declare random like how I do in import java.util.Random;

So, it should be
double alpha=2.5;
double beta=r.nextInt(10)*1.0;
Gamma g = new Gamma(alpha,beta);
System.out.println("Random value is "+g.random());

Also, you have to type
import jsc.distributions.Gamma;
import jsc.*; won't call Gamma out.

I don't know why, but I worked my way through. A screenshot for anyone else ever Googles here. This should help you!