| CSCI A201/A597Lecture Notes 28 Spring 2000 |
FOUR: THROWING DARTS
Here's the basic program:
import element.*;
public class Darts {
public static void main(String[] args) {
Circle c = new Circle(100, 100, 100);
int hits = 0;
int attempts = 10000;
for (int i = 0; i < attempts; i++) {
int x = (int) (Math.random() * 200);
int y = (int) (Math.random() * 200);
Pt m = new Pt(x, y);
if (c.contains(m)) {
hits = hits + 1;
}
}
System.out.println("Throwing " + attempts +
" darts makes PI seem like: " +
4.0 * hits / attempts);
}
}
Here it is visualizing what it's doing:
import element.*;
import java.awt.Color;
public class GraphDarts {
public static void main(String[] args) {
DrawingWindow d = new DrawingWindow();
Circle c = new Circle(100, 100, 100);
d.draw(c);
int hits = 0;
int misses = 0;
int attempts = Integer.parseInt(args[0]);
for (int i = 0; i < attempts; i++) {
int x = (int)(Math.random() * 200);
int y = (int)(Math.random() * 200);
Pt m = new Pt(x, y);
if (c.contains(m)) {
d.setForeground(Color.blue);
hits = hits + 1;
} else {
misses = misses + 1;
d.setForeground(Color.red);
}
d.draw(m);
}
System.out.println("Throwing " + attempts +
" darts makes PI seem like: " +
4.0 * hits / attempts);
System.out.println("Experiment PI. Attempts: " + attempts);
System.out.println(" Hits: " + hits);
System.out.println(" Misses: " + misses);
System.out.println(" Check attempts: " + (hits + misses));
System.out.println("End of experiment");
}
}
SEVEN: REVERSING ARRAYS Here's a program developed by Ben Hui.
import element.*;
import java.util.Random;
public class Ben {
public static void main(String [] args){
Random gen = new Random ();
int size = Integer.parseInt(args [0]);
int numbers [] = new int [size];
for (int i = 0; i < numbers.length; i++)
numbers[i] = Library.Random (0,200,gen);
for ( int i = 0; i
He calls his function inverse other than that
it's like we did it in class on Tuesday. ONE: SORTING RANDOM ARRAYS
Here's the basic part of it, without the drawing window interface.
import element.*;
import java.util.Random;
public class DNM{
public static void main(String [] args){
int size = Integer.parseInt(args [0]);
int[] alpha = Library.createArray(size);
Library.show(alpha);
Library.sort(alpha);
Library.show(alpha);
}
}
class Library{
public static int Random(int low, int high, Random gen){
int x = Math.abs(gen.nextInt())%(high-low+1);
x = x + low;
return (x);
}
public static void show(int[] numbers) {
for (int i = 0; i < numbers.length; i++)
System.out.print(numbers [i] +" ");
System.out.println();
}
public static void sort(int[] numbers) {
for (int i = 0 ; i <= numbers.length-1; i++)
for (int j = 0; j < numbers.length; j++){
if (numbers[i]<numbers[j]){
int temp;
temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
}
}
public static int[] createArray(int size) {
Random gen = new Random();
int numbers [] = new int [size];
for (int i = 0; i <numbers.length; i++)
numbers [i] = Library.Random(0,200,gen);
return numbers;
}
}
Adding the loop, controlled through a drawing window, is immediate. SIXTEEN: CHECKERBOARD
Here's a solution developed with Peter James.
import element.*;
public class CB {
public static void main(String[] args) {
DrawingWindow d = new DrawingWindow();
Rect[][] g = new Rect[8][8];
int xL = 10, yL = 10, w = 10, h = 10;
for (int i = 0; i < g.length; i++) {
for (int j = 0; j < g[i].length; j++) {
g[i][j] = new Rect(xL + j * w, yL + i * h, w, h);
if ((i + j) % 2 == 0) {
d.fill(g[i][j]);
} else {
d.draw(g[i][j]);
}
}
}
while (true) {
Pt m = d.awaitMouseClick();
for (int i = 0; i < g.length; i++) {
for (int j = 0; j < g[i].length; j++) {
if (g[i][j].contains(m)) {
System.out.println(i + ", " + j);
}
}
}
}
}
}
How do we color the squares?