mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Convert 20_Buzzword to Java
The original version was a straight forward monolithic BASIC-to-Java conversion. Updated to use common Java coding conventions. - Split the single static main method into classes. The static part only contains the bootstrap code for the game. - Split the word list into three arrays so that there is no need to use error-prone calculations when choosing the random words. - Placed the Scanner in a try-with-resources block to ensure that the scanner gets closed when it is no longer needed.
This commit is contained in:
@@ -1,41 +1,17 @@
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import static java.lang.System.out;
|
|
||||||
|
|
||||||
// This is very close to the original BASIC. Changes:
|
|
||||||
// 1) the array indexing is adjusted by 1
|
|
||||||
// 2) the user can enter a lower case "y"
|
|
||||||
// 3) moved the word list to the top 8~)
|
|
||||||
public class Buzzword {
|
public class Buzzword {
|
||||||
private static final String[] A = {
|
|
||||||
"ABILITY","BASAL","BEHAVIORAL","CHILD-CENTERED",
|
|
||||||
"DIFFERENTIATED","DISCOVERY","FLEXIBLE","HETEROGENEOUS",
|
|
||||||
"HOMOGENEOUS","MANIPULATIVE","MODULAR","TAVISTOCK",
|
|
||||||
"INDIVIDUALIZED","LEARNING","EVALUATIVE","OBJECTIVE",
|
|
||||||
"COGNITIVE","ENRICHMENT","SCHEDULING","HUMANISTIC",
|
|
||||||
"INTEGRATED","NON-GRADED","TRAINING","VERTICAL AGE",
|
|
||||||
"MOTIVATIONAL","CREATIVE","GROUPING","MODIFICATION",
|
|
||||||
"ACCOUNTABILITY","PROCESS","CORE CURRICULUM","ALGORITHM",
|
|
||||||
"PERFORMANCE","REINFORCEMENT","OPEN CLASSROOM","RESOURCE",
|
|
||||||
"STRUCTURE","FACILITY","ENVIRONMENT"
|
|
||||||
};
|
|
||||||
private static Scanner scanner = new Scanner( System.in );
|
|
||||||
|
|
||||||
public static void main( final String [] args ) {
|
public static void main(final String[] args) {
|
||||||
out.println( " BUZZWORD GENERATOR" );
|
try (
|
||||||
out.println( " CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" );
|
// Scanner is a Closeable so it must be closed
|
||||||
out.println();out.println();out.println();
|
// before the program ends.
|
||||||
out.println( "THIS PROGRAM PRINTS HIGHLY ACCEPTABLE PHRASES IN" );
|
final Scanner scanner = new Scanner(System.in);
|
||||||
out.println( "'EDUCATOR-SPEAK' THAT YOU CAN WORK INTO REPORTS" );
|
) {
|
||||||
out.println( "AND SPEECHES. WHENEVER A QUESTION MARK IS PRINTED," );
|
final BuzzwordSupplier buzzwords = new BuzzwordSupplier();
|
||||||
out.println( "TYPE A 'Y' FOR ANOTHER PHRASE OR 'N' TO QUIT." );
|
final UserInterface userInterface = new UserInterface(
|
||||||
out.println();out.println();out.println( "HERE'S THE FIRST PHRASE:" );
|
scanner, buzzwords);
|
||||||
do {
|
userInterface.run();
|
||||||
out.print( A[ (int)( 13 * Math.random() ) ] + " " );
|
|
||||||
out.print( A[ (int)( 13 * Math.random() + 13 ) ] + " " );
|
|
||||||
out.print( A[ (int)( 13 * Math.random() + 26 ) ] ); out.println();
|
|
||||||
out.print( "?" );
|
|
||||||
}
|
}
|
||||||
while ( "Y".equals( scanner.nextLine().toUpperCase() ) );
|
|
||||||
out.println( "COME BACK WHEN YOU NEED HELP WITH ANOTHER REPORT!" );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
39
20_Buzzword/java/src/BuzzwordSupplier.java
Normal file
39
20_Buzzword/java/src/BuzzwordSupplier.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import java.util.Random;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A string supplier that provides an endless stream of random buzzwords.
|
||||||
|
*/
|
||||||
|
public class BuzzwordSupplier implements Supplier<String> {
|
||||||
|
|
||||||
|
private static final String[] SET_1 = {
|
||||||
|
"ABILITY","BASAL","BEHAVIORAL","CHILD-CENTERED",
|
||||||
|
"DIFFERENTIATED","DISCOVERY","FLEXIBLE","HETEROGENEOUS",
|
||||||
|
"HOMOGENEOUS","MANIPULATIVE","MODULAR","TAVISTOCK",
|
||||||
|
"INDIVIDUALIZED" };
|
||||||
|
|
||||||
|
private static final String[] SET_2 = {
|
||||||
|
"LEARNING","EVALUATIVE","OBJECTIVE",
|
||||||
|
"COGNITIVE","ENRICHMENT","SCHEDULING","HUMANISTIC",
|
||||||
|
"INTEGRATED","NON-GRADED","TRAINING","VERTICAL AGE",
|
||||||
|
"MOTIVATIONAL","CREATIVE" };
|
||||||
|
|
||||||
|
private static final String[] SET_3 = {
|
||||||
|
"GROUPING","MODIFICATION", "ACCOUNTABILITY","PROCESS",
|
||||||
|
"CORE CURRICULUM","ALGORITHM", "PERFORMANCE",
|
||||||
|
"REINFORCEMENT","OPEN CLASSROOM","RESOURCE", "STRUCTURE",
|
||||||
|
"FACILITY","ENVIRONMENT" };
|
||||||
|
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a buzzword by concatenating a random word from each of the
|
||||||
|
* three word sets.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
return SET_1[random.nextInt(SET_1.length)] + ' ' +
|
||||||
|
SET_2[random.nextInt(SET_2.length)] + ' ' +
|
||||||
|
SET_3[random.nextInt(SET_3.length)];
|
||||||
|
}
|
||||||
|
}
|
||||||
57
20_Buzzword/java/src/UserInterface.java
Normal file
57
20_Buzzword/java/src/UserInterface.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import static java.lang.System.out;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A command line user interface that outputs a buzzword every
|
||||||
|
* time the user requests a new one.
|
||||||
|
*/
|
||||||
|
public class UserInterface implements Runnable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input from the user.
|
||||||
|
*/
|
||||||
|
private final Scanner input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The buzzword generator.
|
||||||
|
*/
|
||||||
|
private final Supplier<String> buzzwords;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new user interface.
|
||||||
|
*
|
||||||
|
* @param input The input scanner with which the user gives commands.
|
||||||
|
* @param buzzwords The buzzword supplier.
|
||||||
|
*/
|
||||||
|
public UserInterface(final Scanner input,
|
||||||
|
final Supplier<String> buzzwords) {
|
||||||
|
this.input = input;
|
||||||
|
this.buzzwords = buzzwords;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
out.println(" BUZZWORD GENERATOR");
|
||||||
|
out.println(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||||
|
out.println();
|
||||||
|
out.println();
|
||||||
|
out.println();
|
||||||
|
out.println("THIS PROGRAM PRINTS HIGHLY ACCEPTABLE PHRASES IN");
|
||||||
|
out.println("'EDUCATOR-SPEAK' THAT YOU CAN WORK INTO REPORTS");
|
||||||
|
out.println("AND SPEECHES. WHENEVER A QUESTION MARK IS PRINTED,");
|
||||||
|
out.println("TYPE A 'Y' FOR ANOTHER PHRASE OR 'N' TO QUIT.");
|
||||||
|
out.println();
|
||||||
|
out.println();
|
||||||
|
out.println("HERE'S THE FIRST PHRASE:");
|
||||||
|
|
||||||
|
do {
|
||||||
|
out.println(buzzwords.get());
|
||||||
|
out.println();
|
||||||
|
out.print("?");
|
||||||
|
} while ("Y".equals(input.nextLine().toUpperCase()));
|
||||||
|
|
||||||
|
out.println("COME BACK WHEN YOU NEED HELP WITH ANOTHER REPORT!");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user