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?
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.
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.
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.
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?
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.
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.
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.
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.
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.
Post by:autobot
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
a bit of art, as a gift, the permaculture playing cards