DOC PREVIEW
Duke CPS 006 - Using Random Numbers

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 68.1Using Random Numbersÿ Why?þ Gamesþ Serious simulationsþ Visual effectsþ Obfuscationÿ There are two standard ways to get random numberþ Use random() method of Math class.þ Use Random classþ Both use same underlying arithmetic.þ Pseudo-random Numbersÿ Using Math.random()þ Returns double in range 0 <= n < 1.0o Does not include 1.0!þ Simulate Die Rollint roll=1+(int) (Math.random() * 6;CompSci 68.2Using Random Numbersÿ Why use Random Class?þ Provides many methods that are convenient to use.þ Serious simulationsþ Visual effectsþ Obfuscationÿ Twenty-sided DieRandom gen = new Random();int roll=1+gen.nextInt(20);ÿSome Other Methods Providedþ int nextDouble()þ long nextLong()þ int nextInt()þ boolean nextBoolean()ÿvoid setSeed(long n) Why ?CompSci 68.3Using Random Numbersÿ Think of ways to use random numbers in Imageprocessing1. ?2. ?3. ?4. ?5. ?6. ?7. ?CompSci 68.4String Methodsÿ There are many useful methodsþ The one used most frequently are summarized belowþ Make sure you understand the notations and what is means!int length()boolean equals(String s)int compareTo(String s) // -1, 0, +1 : <, ==, >String substring(int begin, int end)String substring(int begin)int indexOf(String key) // -1 if not foundchar charAt(int position)þ Example. What are the values?String demo = ”How are things?”;demo.substring(8, 12)demo.indexOf(”wa”)demo.indexOf(”w a”)demo.charAt(7);demo.comparetTo(”how are things?”)CompSci 68.5Lab Today - Animationÿ CanvasCompSci 68.6Canvas Detailsÿ What are the dimensions of the Canvas?þ java.awt.Dimensionÿ Creating the Canvasfinal java.awt.Dimension SIZE =new java.awt.Dimension(600,600);Canvas display = new Canvas(SIZE);ÿ Accessing the dimensionsint height = SIZE.height;int width = SIZE.width;CompSci 68.7Paint a circleÿ Center of the screenþ Point (x, y)þ java.awt.pointPoint center =new Point(?, ?);ÿ Penþ Set the colorJava.awt.Color.REDþ Fill the ovalCompSci 68.8Paint a Circle (cont)ÿ Canvas has a special methodþ paintComponentþ ( must spell it exactly this way)þ This method is automatically invoked when an eventoccurso When main sets isVisible to trueo When a window is resizedCompSci 68.9Lab todayÿ Modify the Canvas class to get the circle to moveÿ Modify the Canvas class to get the circle to bounceinside the canvasÿ Add a new class Bouncer to create a circle thatbouncesÿ Create two bouncing


View Full Document

Duke CPS 006 - Using Random Numbers

Download Using Random Numbers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Using Random Numbers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Using Random Numbers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?