• Post Reply Bookmark Topic Watch Topic
  • New Topic

importing libraries  RSS feed

 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...I'm taking an online class. The assignment is to use the UNumber Library. I just have to take a program and change it to use this library. Sounds easy enough. However, how do I get my class to use this library? Do I use an import statement?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you'd need to ensure that the code in that library can be found in your classpath. Then, if the classes in the library are in a package (as they should be) then yes, you would have to use import statements to tell the compiler about that package.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use fully qualified class names (but I wouldn't recommend that).
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the UNumber class? You will probably have to download a UNumber.jar, put it somewhere on your computer, and add the CLASSPATH to the instructions to compile or start your app. Look here (old edition) and look for the -cp or -classpath option. You use the same technique for the java tool.
 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, as a follow up to this, but before I continue...let me say thanks for the replies. I really appreciate it. I'm trying to learn as much as I can. Not just for my class, but also moving forward. But I'm stuck. The assignment is to modify my Task1 Class to allow for the use of the UNumber class. So I assume that I have to declare a UNumber object, and change the Task1 Class to use those that class instead of a double. But it's the implementation of it that's hiolding me up. I understand what I need to do, but not how to do it. Would someone be able to point me in the right direction? I just need enough of a pointer to help me do the work.




 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nestor, be sure to UseCodeTags. I've added them to your latest post.
 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will do. Sorry about that.

 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I understand the assignment. I can read into the application "doubles", and then convert those to the UNumber type. It's that part that I'm stuck at.

Do I declare a new UNumber object, and pass those double values to it?
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you know, we have no idea what this UNumber class is. It is probably something that your instructor provides either as a UNumber.java file or something that's inside a .jar file.
 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The UNumber class implements Comparable. I'll copy the first lines of it here. It's a big file. If it helps you all to actually look at it I can attach it here.



 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you tried using code tags, Nestor, but the [code] tag goes before the code, and the [/code] goes after it. I've fixed it for you.
 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

Should I post the entire code? Or should I attach it as a .java file?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already know how to import classes. Here's an example from your code:



Here, as you should know, you import the Scanner class which is in the java.util package. Then you can use the Scanner type anywhere you like in the code, as you have in fact done.

Now suppose you want to use the UNumber class. If it isn't in a package then you don't need to import it at all. Otherwise you do, and you already know how to import a class which is in some package. So just do that.
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nestor Coronado wrote:. . .
I don't like the way you wrote that loop, I am afraid. There are much better ways to test for correct input and incorrect input with a Scanner. You can use a loop and a hasNextXXX method call. I would suggest that rather than using an empty line, you use a control character to terminate the input. Unfortunately the control character is platform dependent. Change your line 19 to this sort of thing:-Then you can use methods like hasNext to continue your loop; ctrl-D or ctrl-Z terminates the input.
There is no need for a new Scanner for each input; you only need one Scanner pointing to System.in, which you should never close. I don't think there is a need to close a Scanner pointing to a String object either. You can use a Scanner with a loop to go through input until you get a valid number: try here for more hints from Prasanna Raman.
 
Nestor Coronado
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's the part of the code that's been done for me. Basically, the code is entirely done. I just need to modify it to use the UNumber library. That's the assignment part of it. But that's also where I'm stuck right now. I take in the input as a double. Then I create a UNumber object and use one of it's methods to convert the double to the appropriate UNumber value for the lesson.

Since I'm behind in my assignments I had to just turn in what I had.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!