Using Classes from the Java Library

Paul Ngugi - Jun 3 - - Dev Community

The Java API contains a rich set of classes for developing Java programs. This section gives some examples of the classes in the Java library.

The Date Class

Java provides a system-independent encapsulation of date and time in the java.util.Date class, as shown in Figure below.

Image description

You can use the no-arg constructor in the Date class to create an instance for the current date and time, the getTime() method to return the elapsed time since January 1, 1970, GMT, and the toString() method to return the date and time as a string. For example, the following code

java.util.Date date = new java.util.Date();
System.out.println("The elapsed time since Jan 1, 1970 is " +
date.getTime() + " milliseconds");
System.out.println(date.toString());

displays the output like this:

The elapsed time since Jan 1, 1970 is 1324903419651 milliseconds
Mon Dec 26 07:43:39 EST 2011

The Date class has another constructor, Date(long elapseTime), which can be used to construct a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT.

The Random Class

Another way to generate random numbers is to use the java.util.Random
class, as shown in Figure below, which can generate a random int, long, double, float, and boolean value.

Image description

When you create a Random object, you have to specify a seed or use the default seed. A seed is a number used to initialize a random number generator. The no-arg constructor creates a Random object using the current elapsed time as its seed. If two Random objects have the same seed, they will generate identical sequences of numbers. For example, the following code creates two Random objects with the same seed, 3.

Random random1 = new Random(3);
System.out.print("From random1: ");
for (int i = 0; i < 10; i++)
System.out.print(random1.nextInt(1000) + " ");

Random random2 = new Random(3);
System.out.print("\nFrom random2: ");
for (int i = 0; i < 10; i++)
System.out.print(random2.nextInt(1000) + " ");

The code generates the same sequence of random int values:

From random1: 734 660 210 581 128 202 549 564 459 961
From random2: 734 660 210 581 128 202 549 564 459 961

The ability to generate the same sequence of random values is useful in software testing and many other applications. In software testing, often you need to reproduce the test cases from a fixed sequence of random numbers.

The Point2D Class

Java API has a conveninent Point2D class in the javafx.geometry package for representing a point in a two-dimensional plane. The UML diagram for the class is shown in Figure below.

Image description

You can create a Point2D object for a point with the specified x- and y-coordinates, use the distance method to compute the distance from this point to another point, and use the toString() method to return a string representation of the point. Below program gives an example of using this class.

package demo;
import java.util.Scanner;
import javafx.geometry.Point2D;

public class TestPoint2D {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter point1's x-, y-coordinates: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        System.out.print("Enter point2's x-, y-coordinates: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        Point2D p1 = new Point2D(x1, y1);
        Point2D p2 = new Point2D(x2, y2);
        System.out.println("p1 is " + p1.toString());
        System.out.println("p2 is " + p2.toString());
        System.out.println("The distance between p1 and p2 is " + p1.distance(p2));
    }

}
Enter fullscreen mode Exit fullscreen mode

Enter point1's x-, y-coordinates: 1.5 5.5
Enter point2's x-, y-coordinates: -5.3 -4.4
p1 is Point2D [x = 1.5, y = 5.5]
p2 is Point2D [x = -5.3, y = -4.4]
The distance between p1 and p2 is 12.010412149464313

This program creates two objects of the Point2D class (lines 17–18). The toString() method returns a string that describes the object (lines 19–20). Invoking p1.distance(p2) returns the distance between the two points (line 21).

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player