Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.

Similar presentations


Presentation on theme: "CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another."— Presentation transcript:

1 CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: http://twiki-edlab.cs.umass.edu/bin/view/Moll121/WebHome Owl due soon; another one up today. Second programming assignment now due, next assignment up today, due next Thursday Read 4.1 - 4.5 for next week MidTerm: Monday, 10/18, 615-715, Thompson 102,106 Office hours - now posted; held in LGRT 220 I will accept late programming assignments on Thursday - but this is it: beginning with #3, late rules will be in effect

2 Random numbers Java has a Random class (in the java.util package) It produces pseudo-random numbers Random r = new Random(); vs Random r = new Random(303); r.nextInt(); vs r.nextInt(1000 r.nextDouble() -> produces a random value between 0 and 1.0

3 import java.util.Random; public class RandomTester{ public static void main(String[] args) { Random r1 = new Random(202); Random r2 = new Random(303); System.out.println(" from r1: " + r1.nextInt()); System.out.println(" from r2: " + r2.nextInt()); } } œ from r1: -1150098092 œ from r2: -1168181290

4 A real use for random numbers (more or less..) We know that √2 = ~ 1.414 This says that if I choose 1000 numbers between 1 and 2, about 414 should fall below √2 -- the others should be above this value. We can actually use this (probabilistic) technique to compute this value (unimportant) and other values (important!) Here’s the code… Note: if r is a Random object, the statement val = r.nextDouble(); copies a random value between 0 and 1 into val. To get a random value between 1 and 2, try (1 + val); for a random value between 13 and 14: (13 + val)

5 import java.util.Random; public class RootTwo{ public static void main(String[] args){ Random r = new Random(); int ct = 0; double val; for(int j = 0; j < 1000; j++){ val = r.nextDouble(); if ((1 + val) * (1 + val) < 2) ct++; } System.out.println("Square root of 2 is~ " + (1 + (double)ct/1000)); System.out.println(“Using Math class: “ + Math.sqrt(2)); } } jGRASP exec: java RootTwo œœßœSquare root of 2 is~ 1.408 Using Math class: 1.4142135623730951

6 An aside: sqrt(num) doesn’t really belong to a particular object -- in a way it’s too basic. Instead, sqrt is a class method or static method. No object is needed - you just associate it with the class: Math.sqrt(someNum); same with Math.max(a,b)

7 The game of Craps The basic game of craps is very simple. On the first roll of two dice (the come-out roll), the shooter wins by rolling either a 7 or 11 (a natural). Rolling craps (2, 3, or 12) loses. Any other number (4, 5, 6, 8, 9, or 10) is called the point. Now to win, the point number must be rolled before a 7. If a 7 is rolled before the point number, the shooter loses; if the point comes up first, the shooter wins.

8 import java.util.*; class CrapsTester { public static void main(String args[]) { public int gameCount = 100000; Craps1 C= new Craps1(); int count = 0; for (int i = 0; i < gameCount; i++) if (C.crapsRun()) count++; System.out.println((float)count / gameCount); } }

9 // The Craps1 class has one attribute - the random // object r. It has three methods: throwDie, throwDice, // and crapsRun import java.util.Random; public class Craps1 { Random r = new Random(); public int throwDie(){ return (1 +r.nextInt(6));} public int throwDice(){ return (throwDie() + throwDie());}

10 public boolean crapsRun() // returns true if player wins { int point = 0; int cur; cur = throwDice(); switch(cur) {{ case 7: case 11: return(true); case 2: case 3: case 12: return(false); default: point = cur; } while (true) { cur = throwDice(); if (cur == point) return(true); else if (cur == 7) return(false);} }

11 public boolean crapsRun2() { int point = 0; int cur; boolean done = false; boolean result= false; cur = throwDice(); switch(cur){ case 7: case 11: {done = true; result = true; break;} case 2: case 3: case 12: {done = true; result = false; break;} default: point = cur; } if (done) return result; else while (true) { cur = throwDice(); if (cur == point) return(true); else if (cur == 7) return(false);}}

12 public class Test{ public static void main(String[] args){ int num = 300000; int splitCount = 0; while (num > 1){ num = num/2; splitCount++; } System.out.println("number of splits: " + splitCount); } } ----jGRASP exec: java Test œœßœnumber of splits: 18

13 import element.*; public class Test{ public static void main(String[] args){ ConsoleWindow c = new ConsoleWindow(); c.out.println("Enter some large integer "); int num = c.input.readInt(); int splitCount = 0; while (num > 1){ c.out.println("number " + num); num = num/2; splitCount++; } c.out.println(); // a blank line in the console c.out.println("number of splits: " + splitCount); } }


Download ppt "CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another."

Similar presentations


Ads by Google