static int |
nextInt(int min,
int max)
@@ -218,7 +224,7 @@ Method Summary
Methods inherited from class java.lang.Object
-equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
+clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
@@ -373,7 +379,7 @@ nextDouble
-
@@ -406,16 +426,15 @@ nextDouble
@@ -427,14 +446,13 @@ nextDouble
diff --git a/src/ConsoleProgram.java b/src/ConsoleProgram.java
index 9cdce7b..c59321a 100644
--- a/src/ConsoleProgram.java
+++ b/src/ConsoleProgram.java
@@ -1,125 +1,226 @@
-import java.util.Scanner;
+import java.util.*;
public class ConsoleProgram{
- private Scanner scanner;
-
- public static void main(String[] args){
- // Assume the class name is passed in as the first argument.
-
- if(args.length == 0){
- System.out.println("Please provide the name of the main class as an argument.");
- return;
- }
-
- String mainClassName = args[0];
-
- try{
- Class mainClass = Class.forName(mainClassName);
- Object obj = mainClass.newInstance();
- ConsoleProgram program = (ConsoleProgram)obj;
- program.run();
- } catch (IllegalAccessException ex) {
- System.out.println("Error in program. Make sure you extend ConsoleProgram");
- } catch (InstantiationException ex) {
- System.out.println("Error in program. Make sure you extend ConsoleProgram");
- } catch (ClassNotFoundException ex) {
- System.out.println("Error in program. Make sure you extend ConsoleProgram");
- }
- }
-
- /**
- * The run method is overwritten by the subclass. This is the main entry point for
- * ConsolePrograms. There is no main method in student programs that extend ConsoleProgram,
- * instead they write their application starting like this:
- *
- * public void run()
- * {
- * // student code here
- * }
- *
- * The main method of ConsoleProgram calls this run method when it begins.
- */
- public void run(){
- /* Overridden by subclass */
- }
-
- /**
- * Constructor for Console program. Create a scanner instance to be used for user input.
- */
- public ConsoleProgram(){
- scanner = new Scanner(System.in);
- }
-
- /**
- * This method reads a line of input from the user, given a prompt.
- *
- * @param prompt A prompt to the user to get input
- * @return A String of the user input.
- */
- public String readLine(String prompt){
- System.out.print(prompt);
- return scanner.nextLine();
- }
-
- /**
- * This method reads a boolean value from the user, asking them for either
- * a true or false value. This makes use of the readLine method.
- *
- * @param prompt A prompt to the user to read a boolean value
- * @return A boolean value read from the user.
- */
- public boolean readBoolean(String prompt){
-
- while(true){
- String input = readLine(prompt);
-
- if(input.equalsIgnoreCase("true")){
- return true;
- }
-
- if(input.equalsIgnoreCase("false")){
- return false;
- }
- }
- }
-
- /**
- * This method reads a double value from the user, given a prompt.
- *
- * @param prompt A prompt to the user to ask for a a double.
- * @return A double value read from the user.
- */
- public double readDouble(String prompt){
-
- while(true){
- String input = readLine(prompt);
- try {
- double n = Double.valueOf(input).doubleValue();
- return n;
- } catch (NumberFormatException e){
-
- }
- }
- }
-
- /**
- * This method reads a int value from the user, given a prompt.
- *
- * @param prompt A prompt to the user to ask for an int
- * @return The int value read from the user.
- */
- public int readInt(String prompt){
-
- while(true){
- String input = readLine(prompt);
- try {
- int n = Integer.parseInt(input);
- return n;
- } catch (NumberFormatException e){
-
- }
- }
- }
-
-}
\ No newline at end of file
+ private Scanner scanner;
+
+ public static void main(String[] args){
+ // Assume the class name is passed in as the first argument.
+
+ if(args.length == 0){
+ System.out.println("Please provide the name of the main class as an argument.");
+ return;
+ }
+
+ String mainClassName = args[0];
+
+ try{
+ Class mainClass = Class.forName(mainClassName);
+ Object obj = mainClass.newInstance();
+ ConsoleProgram program = (ConsoleProgram)obj;
+ program.run();
+ } catch (IllegalAccessException ex) {
+ System.out.println("Error in program. Make sure you extend ConsoleProgram");
+ } catch (InstantiationException ex) {
+ System.out.println("Error in program. Make sure you extend ConsoleProgram");
+ } catch (ClassNotFoundException ex) {
+ System.out.println("Error in program. Make sure you extend ConsoleProgram");
+ }
+ }
+
+ public void run(){
+ /* Overridden by subclass */
+ }
+
+ public ConsoleProgram(){
+ scanner = new Scanner(System.in);
+
+ }
+
+ public String readLine(String prompt){
+ System.out.print(prompt);
+ return scanner.nextLine();
+ }
+
+ public boolean readBoolean(String prompt){
+
+ while(true){
+ String input = readLine(prompt);
+
+ if(input.equalsIgnoreCase("true")){
+ return true;
+ }
+
+ if(input.equalsIgnoreCase("false")){
+ return false;
+ }
+ }
+ }
+
+ public double readDouble(String prompt){
+
+ while(true){
+ String input = readLine(prompt);
+ try {
+ double n = Double.valueOf(input).doubleValue();
+ return n;
+ } catch (NumberFormatException e){
+
+ }
+ }
+ }
+
+ // Allow the user to get an integer.
+ public int readInt(String prompt){
+
+ while(true){
+ String input = readLine(prompt);
+ try {
+ int n = Integer.parseInt(input);
+ return n;
+ } catch (NumberFormatException e){
+
+ }
+ }
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println()
+ */
+ public void println() {
+ System.out.println();
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(String s)
+ */
+ public void println(String s) {
+ System.out.println(s);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(boolean x)
+ */
+ public void println(boolean x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(char x)
+ */
+ public void println(char x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(char[] x)
+ */
+ public void println(char[] x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(int x)
+ */
+ public void println(int x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(long x)
+ */
+ public void println(long x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(float x)
+ */
+ public void println(float x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(double x)
+ */
+ public void println(double x) {
+ System.out.println(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.println(Object o)
+ */
+ public void println(Object o) {
+ System.out.println(o);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print()
+ */
+ public void print() {
+ System.out.print("");
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(String s)
+ */
+ public void print(String s) {
+ System.out.print(s);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(boolean x)
+ */
+ public void print(boolean x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(char x)
+ */
+ public void print(char x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(char[] x)
+ */
+ public void print(char[] x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(int x)
+ */
+ public void print(int x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(long x)
+ */
+ public void print(long x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(float x)
+ */
+ public void print(float x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(double x)
+ */
+ public void print(double x) {
+ System.out.print(x);
+ }
+
+ /**
+ * Allows us to use a shorthand version for System.out.print(Object o)
+ */
+ public void print(Object o) {
+ System.out.print(o);
+ }
+}
diff --git a/src/Randomizer.java b/src/Randomizer.java
index 66b76dc..6491793 100644
--- a/src/Randomizer.java
+++ b/src/Randomizer.java
@@ -2,82 +2,91 @@
public class Randomizer{
- public static Random theInstance = null;
-
- public Randomizer(){
-
- }
-
- public static Random getInstance(){
- if(theInstance == null){
- theInstance = new Random();
- }
- return theInstance;
- }
-
- /**
- * Return a random boolean value.
- * @return True or false value simulating a coin flip.
- */
- public static boolean nextBoolean(){
- return Randomizer.getInstance().nextBoolean();
- }
+ public static Random theInstance = null;
- /**
- * This method simulates a weighted coin flip which will return
- * true with the probability passed as a parameter.
- *
- * @param probability The probability that the method returns true, a value between 0 to 1 inclusive.
- * @return True or false value simulating a weighted coin flip.
- */
- public static boolean nextBoolean(double probability){
- return Randomizer.nextDouble() < probability;
- }
-
- /**
- * This method returns a random integer.
- * @return A random integer.
- */
- public static int nextInt(){
- return Randomizer.getInstance().nextInt();
- }
+ public Randomizer(){
- /**
- * This method returns a random integer between 0 and n, exclusive.
- * @param n The maximum value for the range.
- * @return A random integer between 0 and n, exclusive.
- */
- public static int nextInt(int n){
- return Randomizer.getInstance().nextInt(n);
- }
+ }
- /**
- * Return a number between min and max, inclusive.
- * @param min The minimum integer value of the range, inclusive.
- * @param max The maximum integer value in the range, inclusive.
- * @return A random integer between min and max.
- */
- public static int nextInt(int min, int max){
- return min + Randomizer.nextInt(max - min + 1);
- }
+ public static Random getInstance(){
+ if(theInstance == null){
+ theInstance = new Random();
+ }
+ return theInstance;
+ }
- /**
- * Return a random double between 0 and 1.
- * @return A random double between 0 and 1.
- */
- public static double nextDouble(){
- return Randomizer.getInstance().nextDouble();
- }
+ /**
+ * Return a random boolean value.
+ * @return True or false value simulating a coin flip.
+ */
+ public static boolean nextBoolean(){
+ return Randomizer.getInstance().nextBoolean();
+ }
+
+ /**
+ * This method simulates a weighted coin flip which will return
+ * true with the probability passed as a parameter.
+ *
+ * @param probability The probability that the method returns true, a value between 0 to 1 inclusive.
+ * @return True or false value simulating a weighted coin flip.
+ */
+ public static boolean nextBoolean(double probability){
+ return Randomizer.nextDouble() < probability;
+ }
+
+ /**
+ * This method returns a random integer.
+ * @return A random integer.
+ */
+ public static int nextInt(){
+ return Randomizer.getInstance().nextInt();
+ }
+
+ /**
+ * This method returns a random integer between 0 and n, exclusive.
+ * @param n The maximum value for the range.
+ * @return A random integer between 0 and n, exclusive.
+ */
+ public static int nextInt(int n){
+ return Randomizer.getInstance().nextInt(n);
+ }
+
+ /**
+ * Return a number between min and max, inclusive.
+ * @param min The minimum integer value of the range, inclusive.
+ * @param max The maximum integer value in the range, inclusive.
+ * @return A random integer between min and max.
+ */
+ public static int nextInt(int min, int max){
+ return min + Randomizer.nextInt(max - min + 1);
+ }
+
+ /**
+ * Return a random double between 0 and 1.
+ * @return A random double between 0 and 1.
+ */
+ public static double nextDouble(){
+ return Randomizer.getInstance().nextDouble();
+ }
+
+ /**
+ * Return a random double between min and max.
+ * @param min The minimum double value in the range.
+ * @param max The maximum double value in the rang.
+ * @return A random double between min and max.
+ */
+ public static double nextDouble(double min, double max){
+ return min + (max - min) * Randomizer.nextDouble();
+ }
+
+ /**
+ * Return a random color.
+ * @return A random hex string that represents a color.
+ */
+ public static String nextColor(){
+ String randomNum = Integer.toHexString(Randomizer.nextInt(0, 16777216));
+ return "'#" + randomNum + "'";
+ }
- /**
- * Return a random double between min and max.
- * @param min The minimum double value in the range.
- * @param max The maximum double value in the rang.
- * @return A random double between min and max.
- */
- public static double nextDouble(double min, double max){
- return min + (max - min) * Randomizer.nextDouble();
- }
-
}
|