Aroma 2: The Die Class - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Aroma 2: The Die Class

Description:

Department of Mathematics and Computer Science. The University of Akron. 9/1/09 ... When rolled, each face is equally likely to appear on top (a fair Die) ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 8
Provided by: timma87
Category:
Tags: aroma | class | die

less

Transcript and Presenter's Notes

Title: Aroma 2: The Die Class


1
Aroma 2 The Die Class
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
Dice Rolling
  • A Die commonly has 6 faces
  • Faces are numbered 1 through 6
  • When rolled, each face is equally likely to
    appear on top (a fair Die)
  • We want to simulate a fair Die with an arbitrary
    number of faces
  • We will number the faces 1 through n

3
java.util.Random
  • class Random
  • Used to create Random objects
  • Each Random object is able to produce a stream of
    pseudorandom numbers
  • Seed?
  • new Random() vs new Random(x)
  • Methods nextInt(), nextLong(), etc.

4
A Class to Die For
  • import java.util.Random
  • public class Die
  • static private long nextSeed
  • new Random().nextLong()
  • private int sides
  • private Random theDie
  • ...

5
Die Constructor, Die!
  • Die(int sides)
  • if (sideslt1) sides6
  • this.sidessides
  • theDienew Random(nextSeed)
  • nextSeedtheDie.nextLong()

6
Roll Die! Roll!
  • public int roll()
  • int rand Math.abs(theDie.nextInt())
  • return rand sides 1

7
Die Test Class
  • import Die
  • public class DieTest
  • public static void main(String args)
  • Die d1 new Die(6)
  • Die d2 new Die(6)
  • for (int i1 ilt20 )
  • System.out.println("d1 "
  • d1.roll()" d2"d2.roll())
Write a Comment
User Comments (0)
About PowerShow.com