Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Location
    Campbell, CA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    Hi,

    My name is Diana Mecum, and I am trying to learn Java. I have a master's degree in Math, and thought that working math problems might be interesting. The whole Java thing is a little overwhelming.

    I have installed NetBeans on my Mac, and understand how to create projects with it. I understand how to import the commons-math3-3.6.1.jar flle into a newly created library for a new project.

    I am trying to write a Java file to do simple math calculations, using the commons-math library files. I don't know how to import the library file within the Java file.

    What I would like is a program to print output with a println command. I would then set up integers, doubles, etc, and perform various calculations with them. Can someone help me with a simple program to get started?

    Diana M.

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    Hi,

    NetBeans has a cool "Fix Imports" feature that I think you'll find very helpful.

    double angle = Math.toRadians(90); // Convert degrees to radians	
    Cos cos = new Cos(); // Compile error on this line: "Can't find symbol: class Cos"
    At this point, NetBeans will show a compile error on the 2nd line. That's because it doesn't know which "Cos" class you're talking about (there could be many). Right-click an empty area of the text editor and select "Fix Imports" (or Ctrl + Shift + I). If the jar file is properly added to the project as a library (on the project's classpath), it should find the Cos class in the commons-math2-2.6.1.jar file and automatically add the import statement for you at the top of your class file:

    import org.apache.commons.math3.analysis.function.Cos;

    And now the compile error should be gone. If you've done any C/C++ coding before, these import statements are similar to "using namespace"; not to be confused with #include.

    This will get the cosine of 90 degrees (in radians) and print it out:
    double angle = Math.toRadians(90);		
    Cos cos = new Cos();
    double cosine = cos.value(angle);
     
    System.out.println("Cosine of angle " + angle + " is " + cosine);
    Hope that helps!

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Location
    Campbell, CA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    BinaryDigit09, thanks for your reply. Would it be possible to paste the entire file into the thread? I am not sure where to place the import line. I'll look at this tonight, and get back to you in 24 hours with a response as to whether I was able to get it working. Thanks again, Diana

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    Don't worry about where to place the import statement. NetBeans will do that for you when you tell it to "Fix Imports." But for your reference, it will go after the package statement and before the beginning of the class definition:

    package myproject;
     
    import org.apache.commons.math3.analysis.function.Cos; // <-- It's here!
     
    public class Main {
    	public static void main(String[] args) throws Exception {		
    		double angle = Math.toRadians(90);
    		Cos cos = new Cos();
    		double cosine = cos.value(angle);
    		System.out.println("Cosine of angle "+angle+" is " + cosine);		
    	}	
    }
    I never manually type import statements. I always let NetBeans do it for me.

  5. #5
    Junior Member
    Join Date
    Sep 2017
    Location
    Campbell, CA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    BinaryDigit09,

    Thanks so much! I'll test it tomorrow morning, and get back to you with my results. Much appreciated. You have saved me a lot of time.

    Diana M.

  6. #6
    Junior Member
    Join Date
    Sep 2017
    Location
    Campbell, CA
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    This worked, thanks a bunch!

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: simple netbeans project for math calculations with commons-math commons-math3-3.6.1.jar

    Great!

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2017, 12:35 PM
  2. How to solve user defined from apache commons math
    By The_Legend in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 18th, 2013, 02:55 PM
  3. User Input/Output for Math Calculations - Java
    By Infinite in forum Java Theory & Questions
    Replies: 7
    Last Post: July 9th, 2013, 03:45 AM
  4. commons-fileupload-1.2.1.jar Problems
    By sengreen in forum Java Servlet
    Replies: 0
    Last Post: April 30th, 2013, 08:42 AM
  5. Math calculations
    By mikem1034 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 16th, 2013, 09:13 AM

Tags for this Thread