Lecture 9 – September 23, 2008

 

Talk about lab 8 –

l          comments on Lab8

l          show what happens if directions not followed in order

 

Talk about lab 9

l          Homework from over weekend was:  study MetricDemo.java  and the UML diagrams in the chapter and determine the meaning of the + and – in the UML diagram.

l          Die class may be used by other programs

 

Talk about constructors

l          belongs to class

l          do not have a visibility modifier

l          same name as class

l          may have no parameters (no-arg constructor)

l          may have one or more parameters (explicit value constructor)

l          class may have no explicit constructor

l          class may have more than one constructor

l          doesn't return values

 

Programs discussed and modified in class

LongWinded - using Math.random( )

QuickWays -  using Math.random( )

using Random class

 

Talk about Math and specifically Math.random

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html

random

public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

Returns:

a pseudorandom double greater than or equal to 0.0 and less than 1.0.

 

 







Talk about Random

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html

 

public class Random

extends Object

implements Serializable

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods. The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the random method in class Math simpler to use.

Constructor Summary

Random()
          Creates a new random number generator.

 

Random(long seed)
          Creates a new random number generator using a single long seed:

 

 

 

 

 int

nextInt()
          Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

 int

nextInt(int n)
          Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

 double

nextDouble()
          Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

 float

nextFloat()
          Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

 void

setSeed(long seed)
          Sets the seed of this random number generator using a single long seed.

ÏÏ

 

Talk about lab 7 -  Sample Desired output

 Ï«Ï ----jGRASP exec: java EnumTest
ÏϧÏ
ÏÏ§Ï Here are the four suits in this game
ÏϧÏCLUBS
ÏϧÏDIAMONDS
ÏϧÏHEARTS
ÏϧÏSPADES
ÏϧÏHEARTS is greater than CLUBS
ÏϧÏSPADES is greater than HEARTS
ÏϧÏCLUBS is less than HEARTS
ÏÏ§Ï Rank      pointValue    abbreviation
ÏϧÏTWO 0 2
ÏϧÏTHREE 0 3
ÏϧÏFOUR 0 4
ÏϧÏFIVE 0 5
ÏϧÏSIX 0 6
ÏϧÏSEVEN 0 7
ÏϧÏEIGHT 0 8
ÏϧÏNINE 0 9
ÏϧÏTEN 0 T
ÏϧÏJACK 1 J
ÏϧÏQUEEN 2 Q
ÏϧÏKING 3 K
ÏϧÏACE 4 A
ÏÏ§Ï The point value of the THREE of HEARTS is 0
ÏÏ§Ï The point value of the ACE of CLUBS is 4
ÏÏ§Ï This program has ended normally
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.
¼¼