I'm really new to Java, please excuse my basic question.
In my class we briefly discussed the API and I have downloaded it to my computer but am having a very hard time understanding how to use it. Say for example I want to find how to print an interger as a rounded number where would that be located in the API?
Don't panic - part of the process of learning any language is discovering where in the language's API a particular piece of functionality might be. If we take your example: you wouldn't ever need to round an integer, since integral values are always whole numbers. But for general mathematic manipulation you have the java.lang.Math class.
The Java API tells you what classes you have at your disposal, and services the classes offer.
I want to find how to print an interger as a rounded number where would that be located in the API?
First you locate the class, that might contain the method that we are looking for:
First try: We will use the Integer class*.
What do you see? - There is a description of the class. We learn, that an object of type Integer contains a single field whose type is int
- Then there's the field summary, which tells you which public variables you can use. Here we see, that we can get the maximum(minimum) integer value by using Integer.MAX_VALUE (or Integer.MIN_VALUE).
- On to the methods (and your question): What we do is we take a look at the methods, and the description. We (sadly) find, that the interger class doesn't have a "rounding" method!
Suddenly you remember, that there's a class called Math! let's take a look at it:
We find, that the class Math indeed have two methods called round. Sadly the first take a double as an argument, and the other takes a float.
A final question: Are you sure, your looking for a method that can round and integer? if you wish to get such a method, you have to make your own as (to my knowleadge) there arn't any such methods in the J2SE API.
Had your question been: How do I round a double or float and print the value, my answer would have been:
Hope it helped - else ask again
*If you have a more "exotic" class, that is in a package you can search for it by presseing the "all classes" link in the upper left corner, and then do a "find and search" in the left frame containing the classes.
Joined: Mar 13, 2005
Thanks for much for the clear example. I'm sorry I actually used the wrong wording and ment to type "double" instead of "interger". Bascially it's for a application that calculates sales tax and the problem I had was computing sales tax. The answer kept showing too many numbers bwyond the decimal point.
I started learning Java during a college course. Our textbook was Core Java(TM) 2, Volume I--Fundamentals which is in its 7th edition now. In each chapter it has good discussion of the related classes from the API. I usually find tutorials such as these a good place to start. Later when I need to perform a specific task, I at least have a vague idea where to look to figure out how to accomplish it.
Since you are in a college course, I assume you have a textbook of some kind. Hopefully it provides a decent description of the common classes from the API. If you are interested in learning more on your own, I suggest that you use Sun's Java Tutorial. It has several "trails" about specific parts of the API. There are also other online tutorials that can give you an introduction to groups of classes from the API. Unfortunately, I don't have any bookmarked at the moment, but you can probably find some suggestions that have been given here previously by using the Big Moose Saloon search tool. Finally, you can come here to ask questions. If you need help solving a particular problem, some of us may know of relevant classes in the API, so if we mention the name, you can at least look up the methods the classes provide.
p.s. I'm glad to see that someone knows about the API docs. We were having a discussion in another thread about the apparent lack of knowledge by some posters here. [ March 14, 2005: Message edited by: Layne Lund ]