Classes in the Java Library

  • The Java library has a large number of classes (written by people working for Oracle)

  • You can find the documentation for all the classes at:

  • In this set of slides, we will learn how to read the documentation and use some of the classes:

      • The java.util.Date class
      • The java.util.Random class

Prerequsite: measuring time inside a computer

  • Computers have an internal clock that measures the passing of time in millisecs

  • The java.util.Date class represents a date as:

      Date = # millisecs since Jan 1, 1970 00:00:00 GMT
    
      Jan 1, 1970 00:00:00 GMT                           Date 
             |                                            |
             +-------------------------------------------->
                                # millisec  

  • Examples:

      0     --->  Jan 1, 1970 00:00:00 GMT
      1000  --->  Jan 1, 1970 00:00:01 GMT

The java.util.Data class   Documentation: click here

  • Some methods in documentation:

     Constructors:
    
       Date()    Allocates (= create) a Date object and 
                 initializes it so that it represents 
    	     the time at which it was allocated (= created)
    
       Date(long msec)   Allocates a Date object and 
                         initializes it to represent the specified 
    		     number of milliseconds since 
                         January 1, 1970, 00:00:00 GMT.
    
     Methods:
    
       toString()  Converts this Date object to a String
    
       getTime()   Returns the number of milliseconds since 
                   January 1, 1970, 00:00:00 GMT represented 
    	       by this Date object.  

Using the java.util.Data class

Example using the constructors and the toString() method in java.util.Date:

import java.util.Date;

public class myProg
{
    public static void main(String[] args)
    {
        Date d1 = new Date();  
        System.out.println( d1.toString() ); 

        Date d2 = new Date(0);    // EST is 4 hours behind GMT
        System.out.println( d2 ); // Same effect as d2.toString() !

        Date d3 = new Date(1000); // 1000 msec = 1 sec
        System.out.println( d3 ); // d3 is 1 sec later than d2
    } 
} 

DEMO: demo/03-classes/15-date/Demo.java

Using the java.util.Data class

Example using the constructors and the getTime() method in java.util.Date:

import java.util.Date;

public class myProg
{
    public static void main(String[] args)
    {
        long  result;

        Date myDate = new Date(0);  
	result = myDate.getTime();
        System.out.println( result ); // 0

        myDate = new Date(1000);  
	result = myDate.getTime();
        System.out.println( result ); // 1000

        myDate = new Date();  
	result = myDate.getTime();
        System.out.println( result ); // # msec since Jan 1, 1970
    } 
} 

DEMO: demo/03-classes/15-date/Demo2.java

Using the java.util.Data class

Measure the time needed to run a program:

import java.util.Date;

public class myProg
{
    public static void main(String[] args)
    {
        long  startTime, endTime;

        Date myDate;

        myDate = new Date();          // Record the time NOW
	startTime = myDate.getTime(); // Get start time in msec

	for ( long i = 0; i < 10000000; i++ ) ;   // Loop to pass time... 

        myDate = new Date();          // Record the time NOW
	endTime = myDate.getTime();   // Get end time in msec

        System.out.println("Program ran for : " 
                            + (endTime-startTime) + " msec");
    } 
} 

DEMO: demo/03-classes/15-date/Demo3.java

Prerequsite: how computer generates "random" numbers

  • Computers do not generate truly random numbers

      • Computers only generates pseudo ("looks like random") random numbers

  • Pseudo random numbers are generated using a "pseudo random function":

          xn+1 = PseudoRandonFunction( xn )  
      
                 with some initial value x0  called the seed

    I.e.:

      The first pseudo random number x1 is computed using x0
      The second pseudo random number x2 is computed using x1
      And so on...

The java.util.Random class   Documentation: click here

  • Some methods in documentation:

     Constructors:
    
       Random()          Creates a new random number generator.
                         (Using Date.getTime() as seed)
    
       Random(long seed) Creates a new random number generator
                         using "seed" as seed.
    
     Methods:
    
       nextInt()    Returns the next pseudo random int typed value
     
       nextInt(n)   Returns the next pseudo random int typed value
                    that is between: [0 .. n)
    
       nextLong()   Returns the next pseudo random long typed value
    
       nextDouble() Returns the next pseudo random double value
                    between [0.0 .. 1.0)

Using the java.util.Random class

Making 3 random number generators for 3 different streams of random numbers:

import java.util.Random;

public class myProg
{
    public static void main(String[] args)
    {
        Random rg1 = new Random(); // Random Num Gen 1
        Random rg2 = new Random(); // Random Num Gen 2
        Random rg3 = new Random(); // Random Num Gen 3

        for ( int i = 0; i < 10; i++ )
	    System.out.print( rg1.nextInt()+" ");    // Randon series of int
        System.out.println();

        for ( int i = 0; i < 10; i++ )
	    System.out.print( rg2.nextInt(100)+" "); // Randon series in [0..100)
        System.out.println();

        for ( int i = 0; i < 10; i++ )
	    System.out.print( rg3.nextDouble()+" "); // Randon series in [0..1)
    } 
} 

DEMO: demo/03-classes/16-random/Demo.java

Using the java.util.Random class

Example that shows the pseudo random numbers are not truly random:

import java.util.Random;

public class myProg
{
    public static void main(String[] args)
    {
        Random rg1 = new Random(4); // Random Num Gen 1 with seed = 4
        Random rg2 = new Random(4); // Random Num Gen 2 with seed = 4

        for ( int i = 0; i < 10; i++ )
	    System.out.print( rg1.nextInt(100)+" "); // "Random" series 1
        System.out.println();

        for ( int i = 0; i < 10; i++ )
	    System.out.print( rg2.nextInt(100)+" "); // Same series !!!
    } 
}