Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Java Code
|
Posted: Mar 9, 2002 12:13 PM
|
|
/* ProblemGenerator.java * @author: Charles Bell * @version: Mar 9, 2002 * email: charbell@bellsouth.net * */
import java.io.*; import java.util.*;
/** ProblemGenerator */ public class ProblemGenerator{
Random random = new Random();
/** */ public static void main(String[] args){
ProblemGenerator problemgenerator = new ProblemGenerator(); problemgenerator.generate();
}
/** Queries the user to enter the number of problems to * generate, generates them, poses the problem, and prompts * for an answer, displays feedback and the correct answer, * if missed, and the cumulative score. */ public void generate (){
showMessage("How many problems would you like?"); int problems = getPositiveInteger(); showMessage("Generating " + String.valueOf(problems) + " problems."); int score = 0; int correctresponses = 0; for (int i = 0; i < problems;i++){ int first = getRandomInt(1,12); int second = getRandomInt(1,12); String question = String.valueOf(first) + " x " + String.valueOf(second); showMessage("Question: " + question + "?"); int useranswer = getPositiveInteger(); int correctanswer = first * second; if (useranswer == correctanswer){ showMessage("Correct!"); correctresponses++; }else{ showMessage("No, " + question + " is " + String.valueOf(correctanswer)); } showMessage(score(i+1,correctresponses)); } showMessage("Your final score was " + score(problems,correctresponses));
}
/** Returns a random integer between low and high, inclusive. * Random.nextInt(int n) returns next random between 0 * inclusive and n exclusive, so must adjust for low and high. */ public int getRandomInt(int low, int high){ int highinclusive = high + 1; int n = random.nextInt(highinclusive - low); return low + n; }
/** Returns a string corresponding to the percentage * of a correct answers a out of n problems. */ public String score(int n, int a){ float ratio = 0f; if (n > 0) { ratio = (float) a / (float) n; } float percent = ratio * 100f; return String.valueOf(percent + "%"); }
/** Displays message string to System.out. */ public void showMessage(String message){ System.out.println(message); }
/** Loops until the user enters a positive integer. */ public int getPositiveInteger(){ int n = -1; while (n < 0){ String input = getUserInput(); if (isInteger(input)){ n = toInt(input); } } return n; }
/** Obtains a line of input from the user using * InputStream System.in. */ public String getUserInput(){ String lineread = ""; try{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); lineread = br.readLine(); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } return lineread; }
/** Converts the input string to an integer * or returns -1 if unsucessful. */ public int toInt(String s){ int n = -1; try{ n = Integer.parseInt(s); }catch(NumberFormatException nfe){ System.err.println("NumberFormatException: " + nfe.getMessage()); System.err.println(s + " can not be converted to an integer."); } return n; }
/** Returns true if the input string can be converted * to an integer. False otherwise. */ public boolean isInteger(String s){ boolean status = false; try{ int n = Integer.parseInt(s); //program execution will not reach next line //unless parse is sucessful. status = true; }catch(NumberFormatException nfe){ status = false; } return status; } }
|
|