[] [][] [][][] [][][][]
for (int i = 1; i <= n; i++) { // Make triangle row }
for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n";
/** This class describes triangle objects that can be displayed as shapes like this: [] [][] [][][] */ public class Triangle { private int width; /** Constructs a triangle. @param aWidth the number of [] in the last row of the triangle. */ public Triangle(int aWidth) { width = aWidth; } /** Computes a string representing the triangle. @return a string consisting of [] and newline characters */ public String toString() { String r = ""; for (int i = 1; i <= width; i++) { // Make triangle row for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n"; } return r; } }
/** This program prints two triangles. */ public class TriangleRunner { public static void main(String[] args) { Triangle small = new Triangle(3); System.out.println(small.toString()); Triangle large = new Triangle(15); System.out.println(large.toString()); } }
Program Run:
[] [][] [][][] [] [][] [][][] [][][][] [][][][][] [][][][][][] [][][][][][][] [][][][][][][][] [][][][][][][][][] [][][][][][][][][][] [][][][][][][][][][][] [][][][][][][][][][][][] [][][][][][][][][][][][][] [][][][][][][][][][][][][][] [][][][][][][][][][][][][][][]
for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) if (i + j <= 10) Print " " else Print "*" System.out.println(); }
int n = 0; for (int i = 1; i <= 5; i++) for (int j = 0; j < i; j++) n = n + j;
Random generator = new Random(); int n = generator.nextInt(a); // 0 < = n < a double x = generator.nextDouble(); // 0 <= x < 1
int d = 1 + generator.nextInt(6);
import java.util.Random; /** This class models a die that, when cast, lands on a random face. */ public class Die { private Random generator; private int sides; /** Constructs a die with a given number of sides. @param s the number of sides, e.g. 6 for a normal die */ public Die(int s) { sides = s; generator = new Random(); } /** Simulates a throw of the die @return the face of the die */ public int cast() { return 1 + generator.nextInt(sides); } }
/** This program simulates casting a die ten times. */ public class DieSimulator { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); System.out.print(n + " "); } System.out.println(); } }
Program Run:
6 5 6 3 2 6 3 4 4 1
Second Run:
3 2 2 1 6 5 3 4 1 2
Which of these can be used to generate a random letter A, B, C, or D?
"ABCD".substring(generator.nextInt(4), generator.nextInt(4) +
1)
"A" + generator.nextInt(4)
'A' + (int)(4 * generator.nextDouble())
import java.util.Random; /** This class simulates a needle in the Buffon needle experiment. */ public class Needle { private Random generator; private int hits; private int tries; /** Constructs a needle. */ public Needle() { hits = 0; tries = 0; generator = new Random(); } /** Drops the needle on the grid of lines and remembers whether the needle hit a line. */ public void drop() { double ylow = 2 * generator.nextDouble(); double angle = 180 * generator.nextDouble(); // Computes high point of needle double yhigh = ylow + Math.sin(Math.toRadians(angle)); if (yhigh >= 2) hits++; tries++; } /** Gets the number of times the needle hit a line. @return the hit count */ public int getHits() { return hits; } /** Gets the total number of times the needle was dropped. @return the try count */ public int getTries() { return tries; } }
/** This program simulates the Buffon needle experiment and prints the resulting approximations of pi. */ public class NeedleSimulator { public static void main(String[] args) { Needle n = new Needle(); final int TRIES1 = 10000; final int TRIES2 = 1000000; for (int i = 1; i <= TRIES1; i++) n.drop(); System.out.printf("Tries = %d, Tries / Hits = %8.5f\n", TRIES1, (double) n.getTries() / n.getHits()); for (int i = TRIES1 + 1; i <= TRIES2; i++) n.drop(); System.out.printf("Tries = %d, Tries / Hits = %8.5f\n", TRIES2, (double) n.getTries() / n.getHits()); } }
Program Run:
Tries = 10000, Tries / Hits = 3.08928 Tries = 1000000, Tries / Hits = 3.14204