import javax.swing.*; import java.util.*; /** * This program generates 10 fraction arithmetic problems. User is prompted for a time limit for each question. * Fractions must be multiplied, divided, added, or subtracted. * Scoring is as follows: * Correct Wrong * Under Time 10 3 *Over Time 6 0 * Score is kept and shown at the end of the round. * * The user then has the choice to play again. * * * @author Bill Kukowski */ class FractionArithmetic { /** * Main method. * * @params args */ public static void main(String[] args) { boolean playAgain = true; while (playAgain) //enables user to play again { //stores time limit for problem int seconds = Integer.parseInt(JOptionPane.showInputDialog(null, "How many seconds do you want to complete each problem? (Default 30)", 30)); //generates new problem set Problems problems = new Problems(); //sets object used for input. Scanner scanner = new Scanner(System.in); String guess; float guessF; long startTime, answerTime; boolean correctAns, underTime; //used for scorekeeping. int[] scores = new int[4]; //displays each problem for (int p=0;p<10;p++) { guess = ""; //get current system time startTime = System.currentTimeMillis(); //ouutput problem. System.out.println(problems.getNumerator1(p) + " " + problems.getNumerator2(p)); System.out.println("_" + " " + problems.getOperation(p) + " " + "_"); System.out.println(problems.getDenominator1(p) + " " + problems.getDenominator2(p)); System.out.println("You have " + seconds + " seconds remaining. Enter answer as a fraction (e.g. 4/7)"); //store user's input guess = scanner.next(); // split user's input into two ints and store to array String[] components = guess.split("/"); //divides ints to get float. (needed to compare to float generated by problems.class) guessF = (float) Integer.parseInt(components[0]) / (float) Integer.parseInt(components [1]); //get system time after answer is entered. answerTime = System.currentTimeMillis(); //set booloean for correct answer correctAns = guessF == problems.getAnswer(p); //set boolean for undertime underTime = answerTime < startTime + (seconds * 1000); //add correct score to correct array entry if ( correctAns && underTime) scores[0]++; if ( correctAns && !underTime) scores[1]++; if ( !correctAns && underTime) scores[2]++; if ( !correctAns && !underTime) scores[3]++; //generate separator between problems System.out.println(""); System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); System.out.println(""); } //tally up total score int points = (10 * scores[0]) + (6 * scores[1]) + (3 * scores[2]); //display final score System.out.println(""); System.out.println(""); System.out.println(""); System.out.println("********You're Finished!********"); System.out.println(""); System.out.println(" Under Over"); System.out.println(" Time Limit Time Limit"); System.out.println(""); System.out.println("Correct Answers " + scores[0] + " " + scores[1]); System.out.println("Wrong Answers " + scores[2] + " " + scores[3]); System.out.println("Total Points: " + points + "(" + (scores[0] * 10) + " + " + (scores[1] * 6) + " + " + (scores[2] * 3) + " + 0"); //prompt to play again System.out.println("Do you want to play again? 1 for yes 0 for no."); //check playAgain if (!(scanner.nextInt() == 1)) { playAgain = false; } } } } /** * This class generates 10 Fraction arithmetic problems. Fractons and operations are stored in an array. * * @author Bill Kukowski */ class Problems { private int[][] fractionGroup1 = new int[2][10]; private int[][] fractionGroup2 = new int[2][10]; private int[] operations = new int[10]; private float[] answers = new float[10]; private float tmpNum1 = 0; private float tmpNum2 = 0; public Problems() { //generates first array of fractions for (int r=0;r<2;r++) { for (int c=0;c