Skip to main content

Student Portal > Contests > 

Programming Contest Central

   
Welcome students Resources Competition ProfilesFAQ
  Contest calendar
  Problem of the month
  Past problems
  Games and brainteasers
  Successes
  Tutorials

Packages
Importing Packages
Using Packages
Related Links


Importing Packages

A package is a group of classes which have predefined functions. By using these functions already written for you, tasks like writing to the screen or getting user input become much easier. Packages are categorized according to their functionality and usability and are found in what is called the Java library. In order to use these functions, you must import them at the very beginning of the program, using notation such as "java.aaa.bbb" where "aaa" is the name of the package and "bbb" is the name of the class within the package. (The "bbb" extension is optional if you want to import the entire package.)

For example, by typing "import java.io.*" at the beginning of the program, you would be importing the java.io package, which contains all classes that have functions related to input and output of information. (The ".*" indicates that you're importing all the classes in the directory). Alternatively, if you only need one class, you can specify a single class with the following line: import java.io.BufferedReader;.

Java Packages
Package Name Features
  java.lang
    Automatically imported, you don't need to import it yourself.
    Includes classes that are fundamental to the design of you program.
    Contains the Math class ( functions such as calculating the square root of a given number, or rounding decimals)
    Contains Threads (for when you want to do multiple things at once - and keep them going)
    Also contains basic objects like Strings.
  java.io
    Input and output, to enable the user to read from and write to various places, like the screen, or a text file.
    Includes BufferedReader, InputStream, FileReader, and FileWriter
  java.util
  (as in "utilities")
    The collections framework (an object that represents a group of objects. Objects are instances of classes, and that will be discussed in the Object-Oriented Programming Tutorial)
    The event model (functions that deal with responses from the user, such as clicking a button)
    Other utility classes such as the random-number generator and string tokenizer
  java.awt
  (stands for Abstract
   Window Toolkit)
    Various tools to create a user interface (what the user sees on the screen).
    Objects it includes are Rectangle, Point, Polygon, and more.
    java.awt also includes objects that can hold various other objects and images so that you can output to the screen. Some of these components are Frame, Label, Panel, and Canvas.

All these packages come standard when you download a Java Development Kit (JDK). However, depending on the IDE (Integrated Development Environment), some also comes with a few special packages of their own. These packages may be useful for some users, but not others (e.g. one user likes to create widgets, and therefore chooses the Eclipse IDE, which supports widgets, instead of the Ready to Program IDE, which doesn't). The tutorials on this web site will use packages only from the Java library.

Example
import java.io.*;    //this imports the java.io package

public class title
{
      public static void main(String args[])
      {
            // code goes here
      }
}



Using Packages

Each class contained in a package has methods of its own. Depending on the class, you can either use those methods directly, or create an object and then use those methods.

Example
/* The following is how to make use of classes within a package. The Math class is contained in the Java.lang package, and we therefore don't have to import it. The Math class has a method called sqrt, which calculates the square root of a number. Let's take the square root of i. */

double i = 122;

double answer = Math.sqrt (i);

System.out.println(answer);

System Output
11.045


Example
/* Now, we're going to make an object of type Random, a random number generator . */

import java.util.*;     // if you don't import this package, the code won't compile properly (meaning run properly: that is, you'll get an error)

public class RandomNumbers
{
       public static void main(String args[])
       {
            Random rand = new Random();
            double d;
            //The following line assigns d to a random integer from 0 to 6
            //(inclusive)
            d = rand.nextInt(7);
      }
}


Don't worry about remembering which methods belong to what classes in certain packages. There's a handy reference guide available to you, called Java API documentation. There, you can look up everything you need to know.



For more information on variables and casting, check out some useful links:

Java API Documentation A site detailing all of the different packages and the Classes and methods that they include.
Eclipse API Documentation A guide listing all of the different packages that Eclipse provides.
Widgets A description of Widgets by Addison-Wesley.




Back to top

 



 
Quick links

Student forum

Teacher resources

RSS Feed