Import packages to Java

Discussion in 'Mac Programming' started by kulimer, Mar 4, 2012.

  1. macrumors 6502

    Joined:
    Aug 30, 2011
    #1
  2. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    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.
     
  3. kulimer, Mar 5, 2012
    Last edited: Mar 5, 2012

    thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #3
    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?
     
  4. macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    Nope. Lion has Java 6...
     
  5. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #5
    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?
     
  6. thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #6
    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.
     

    Attached Files:

  7. thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #7
    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".
     
  8. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #8
    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.
     
  9. thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #9
    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.
     

    Attached Files:

  10. naples98, Mar 5, 2012
    Last edited: Mar 5, 2012

    macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #10
    "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

    Code:
    theta = g.calculateTheta(2.5, 2);
    which means you are calling the calculateTheta method on the instance of Gamma called "g".
     
  11. kulimer, Mar 5, 2012
    Last edited: Mar 5, 2012

    thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #11
    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"
     

    Attached Files:

  12. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #12
    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:
     
  13. thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #13
    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,
    Code:
    theta = g.r(2.5, 3.0); 
    Also, should I declare
    Code:
    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.
     
  14. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #14
    The class Gamma does NOT have a method called "r" so
    Code:
    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).
     
  15. thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #15

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

    So, it should be
    Code:
    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!
     

    Attached Files:

Share This Page