Page 188 - Introduction to Programming with Java: A Problem Solving Approach
P. 188
154 Chapter 5 Using Pre-Built Methods
into three window panes. The top-left pane displays a list of all of Java’s packages. The bottom-left pane displays a list of all of Java’s classes. The right pane displays a variety of different content, where the type of content depends on what the user specifies.
The Web site provides several ways to look things up:
1. If you hope that the API library contains a method or class that might help with your current program- ming project, but you’re unsure, you’ll have to do some browsing. Start by making sure the Overview link at the top of the Web site is selected. When the Overview link is selected, the right window pane displays a list of all the packages and a brief description of each package. If you find a package that looks promising, click the package’s name. That causes the right pane to display all the classes within the selected package and a brief description of each class. If you find a class that looks promising, click the class’s name. That causes the right pane to display all the methods within the selected class and a brief description of each method. If you find a method that looks promising, click the method’s name. That causes the right pane to display the method’s complete details.
2. If you know the name of a particular class that you want details on, click anywhere in the bottom-left window pane and press Ctrlf (hold the control key down and tap the f key). The f stands for “find” and pressing Ctrlf causes a Find dialog box to appear. Enter the name of the class in the Find dialog box and click the Find Next button. That should cause the class to be found and highlighted in the bottom- left window pane. Click the class and that causes the right window pane to display the class’s details.
3. If you know the name of a particular method that you want details on, click the Index link at the top
of the window. That causes the right window pane to display the letters of the alphabet. Click the letter
that matches the first letter of the method you’re interested in. That causes the right window pane to
display methods (and other entities, like classes) that begin with the clicked letter. Find the method
Apago PDF Enhancer
you’re interested in and click it to display all its details.
Using Sun’s Java API Web site is like surfing the net, but you’re not surfing the whole world. You’re just surf- ing the Java class library. You can do it, and we encourage you to give it a try whenever you’re curious.
Using the API Class Library
To use an API class in your program, you must first import it (i.e., load it) into your program. For example, to use the Scanner class, you must include this statement at the top of your program:
import java.util.Scanner;
Note the java.util part of java.util.Scanner. The java.util part is the name of a package. The “util” stands for “utility,” and the java.util package contains general-purpose utility classes. The only java.util class you’ll need right now is the Scanner class. But there are many other useful classes in the java.util package. Examples are:
• TheRandomclass,forhelpingyouworkwithrandomnumbers—discussedinanoptionalsectionatthe end of this chapter.
• The Calendar class, for helping you work with times and dates—discussed in an optional section at the end of Chapter 8.
• The Arrays, ArrayList, LinkedList, and Collections classes, for helping you work with lists or collections of similar data—ArrayLists are discussed in Chapter 10.
If you have a program that needs to use more than one of the classes in a particular package, like two or more of the util package classes just mentioned, you can import them all at once using a statement like this:
import java.util.*;