Classes in the Java Library

  • Recall:   classes are used to:

    1. Group useful methods together

    2. Implement some useful object to help programmers write their code

  • We have just learned how to implement a deck of playing cards as a class.

    • It is very time consuming to implement useful objects as classes

  • That's why:

    • The Java library has a large number of classes to help you write programs

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

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

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

Background information:   measuring time inside a computer

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

    • This internal clock allows a computer to calculate the current time

  • The java.util.Date class is used to measure the passage of time since Jan 1, 1970:

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

  • Examples:

     # millisec
          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() methods in java.util.Date:


import java.util.Date;

public class myProg
{
    public static void main(String[] args)
    {
        Date d1 = new Date();                 // Get the current time
        System.out.println( d1.toString() );  // Will print the current time

        Date d2 = new Date(0);        // Get this date: Midnight Jan 1, 1970 GMT 
        System.out.println( d2 );     // Note: GMT is 5 hrs ahead of EST

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

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

Using the java.util.Data class

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

// getTime( ) returns # millisec since Jan 1, 1970  00:00:00 GMT

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/10-classes/15-date/Demo2.java

Application of the java.util.Data class

We can measure the time needed to run a program by recording the start time and stop time:

import java.util.Date;

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

        Date myDate;

        myDate = new Date();          // Record the time before running the loop
	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 after running the loop
	endTime = myDate.getTime();   // Get end time in msec

	long elapsedTime = endTime - startTime;   // Compute elapsed time
        System.out.println("Program ran for : " + elapsedTime + " msec");
    } 
} 

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

Background information:  how computer generates "random" numbers

  • Computers do not generate truly random numbers

    • Computers only generates pseudo (= fake, but "looks like real") random numbers

  • Pseudo random numbers are generated using:

    • A "pseudo random function"
    • A starting random value x0 called the "seed"

  • The pseudo random numbers are generated by applying a "pseudo random function" repeatedly as follows:

     First  pseudo random number x1 = PseudoRandonFunction( x0 ) 
     Second pseudo random number x2 = PseudoRandonFunction( x1 )  
     ...
    

    I.e.:   the random number xn+1 is computed using xn

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 x0)
    
       Random(long seed) Creates a new random number generator
                         using seed as seed x0.
    
     Methods:
    
       nextInt()    Returns the next pseudo random int typed value
                    between: [Integer.MIN_VALUE .. Integer.MAX_VALUE]
     
       nextInt(n)   Returns the next pseudo random int typed value
                    between: [0 .. n)
    
       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/10-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)+" "); // Generates the same series !!!
    } 
} 

DEMO: demo/10-classes/16-random/Demo2.java