/** @Author: Dave O'Brien(Mostly me because I own) & Zach Booth Date: Mar 01, 2005 Teacher: Mr. Dissinger */ import apcslib.*; // formatting / Drawing tools import chn.util.*; // Input Console - ConsoleIO input = new ConsoleIO(); import java.util.*; //Random number; array lists public class TestQuestions { static ArrayList myQuestion = new ArrayList(); static ArrayList wrongAnswers = new ArrayList(); static Random num = new Random(); static int numProblems = 0, numCorrect = 0, countR = 0, testNumber; static Questions currentQuestion; static String quesAns; public static void main(String args[]) { ConsoleIO input = new ConsoleIO(); System.out.print("Enter user name:"); String userName = input.readLine(); System.out.println("Enter Test Number, 1 or 2:"); testNumber = input.readInt(); while((testNumber != 1) && (testNumber != 2)) { System.out.print("Not a valid test number, choice again:"); testNumber = input.readInt(); } String test = null; switch(testNumber){ case 1: test = "questionsandanswers.txt"; break; case 2: test = "answersandquestions.txt"; break; } System.out.print("How many questions do you want (more than 10 = 10)?"); int numOfQuestions = input.readInt(); readInQuestions(test); System.out.println(); while(countR != numOfQuestions && countR <10){ //System.out.println(myQuestion.size()); System.out.println("\n**********************************************\n"); choiceNextProblem(); System.out.println(currentQuestion.getQuestion()); System.out.println(currentQuestion.mixThemUp()); //System.out.println(currentQuestion.getAnswer()); char answer = (char)(input.readToken().charAt(0)); switch(answer){ case 'a': answer = 'A'; break; case 'b': answer = 'B'; break; case 'c': answer = 'C'; break; case 'd': answer = 'D'; break; case 'e': answer = 'E'; break; default: break; } while(answer > 'F'){ System.out.print("Invalid answer, reenter."); answer = (char)(input.readToken().charAt(0)); switch(answer){ case 'a': answer = 'A'; break; case 'b': answer = 'B'; break; case 'c': answer = 'C'; break; case 'd': answer = 'D'; break; case 'e': answer = 'E'; break; default: break; } } if(currentQuestion.getAnswer() == answer) numCorrect++; else wrongAnswers.add(new Integer(currentQuestion.getId())); numProblems++; countR++; } //System.out.println(currentQuestion.getAnswer() + " " + answer); Calendar cal = new GregorianCalendar(); int hour = cal.get(Calendar.HOUR_OF_DAY); int min = cal.get(Calendar.MINUTE); System.out.println(numCorrect + "/" + numProblems); System.out.println(((double)numCorrect / numProblems) * 100 + "%"); writeInfo(userName, numCorrect, numProblems, hour, min, wrongAnswers, test); reportWrong(wrongAnswers); } public static void readInQuestions(String file){ FileInput inFile; String fileName = file; inFile = new FileInput(fileName); String ans, aAns, bAns, cAns, dAns, eAns; String questString = ""; int x = 0; boolean questionTest; while(inFile.hasMoreLines()){ questionTest = true; questString = ""; if(((char)(inFile.readLine()).charAt(0)) == '$'){ questString += inFile.readLine(); while (questionTest){ String tempReadIn = inFile.readLine(); if (((char)tempReadIn.charAt(0))!= '@') { questString += "\n" + tempReadIn; questionTest = true; } else { questionTest = false; } } } int id = inFile.readInt(); ans = inFile.readLine(); aAns = ans; bAns = inFile.readLine(); cAns = inFile.readLine(); dAns = inFile.readLine(); eAns = inFile.readLine(); myQuestion.add(new Questions(id, questString, ans, aAns, bAns, cAns, dAns, eAns)); } } public static void choiceNextProblem(){ int nextNumber = num.nextInt(myQuestion.size()); currentQuestion = (Questions)myQuestion.get(nextNumber); myQuestion.remove(nextNumber); } public static void writeInfo(String name, int correct, int probs, int hour, int min, ArrayList wronged, String test){ String fileName = "userInfo.txt"; FileOutput outFile = new FileOutput(fileName,"append"); outFile.println("Name: " + name); outFile.println("Test Name: " + test); outFile.println("Questions wronged: "+ wronged); outFile.println("Number Correct: " + correct); outFile.println("Number of Problems: " + probs); outFile.println("Time: " + hour + ":" + min); //outFile.println(report); outFile.println(); outFile.close(); } public static void reportWrong(ArrayList wrongAnswers){ FileInput myFile = new FileInput("testResults.txt"); int[] wrongAnswersArray = new int[10]; String [] holder = new String[10]; switch(testNumber){ case 1:myFile.readLine(); for(int a = 0; a < wrongAnswersArray.length; a++){ String nextLine = myFile.readLine(); int dotLocation = nextLine.indexOf(":"); int value = Integer.parseInt(nextLine.substring(dotLocation+1)); wrongAnswersArray[a] = value; //System.out.println(value); } for(int b = 0; b < wrongAnswers.size(); b++){ Integer wrapper = (Integer)wrongAnswers.get(b); wrongAnswersArray[wrapper.intValue()-1]++; } myFile.readLine(); //myFile.readLine(); for(int c = 0; c < 10; c++){ String nextLine = myFile.readLine(); holder[c] = nextLine; //System.out.println("test"); } myFile.close(); FileOutput myFileOut = new FileOutput("testResults.txt"); myFileOut.println("questionsandanswers.txt"); for(int d = 0; d < wrongAnswersArray.length; d++){ myFileOut.println(d+1 + ":" + wrongAnswersArray[d]); } myFileOut.println("answersandquestions.txt"); myFileOut.println(""); for(int e = 0; e < 10; e++){ myFileOut.println(holder[e]); } break; case 2: myFile.readLine(); for(int c = 0; c < 10; c++){ String nextLine = myFile.readLine(); holder[c] = nextLine; } myFile.readLine(); myFile.readLine(); for(int a = 0; a < wrongAnswersArray.length; a++){ String nextLine = myFile.readLine(); int dotLocation = nextLine.indexOf(":"); int value = Integer.parseInt(nextLine.substring(dotLocation+1)); wrongAnswersArray[a] = value; } for(int b = 0; b < wrongAnswers.size(); b++){ Integer wrapper = (Integer)wrongAnswers.get(b); wrongAnswersArray[wrapper.intValue()-1]++; } break; default: break; } } } class Questions { Random num = new Random(); private String question, answer, A,B,C,D,E; ArrayList options = new ArrayList(); private int questId; public Questions(int id, String quest, String An, String a, String b, String c, String d, String e){ question = quest; questId = id; answer = An; options.add(a); options.add(b); options.add(c); options.add(d); options.add(e); options.add(An); } public String getQuestion(){ return question; } public int getId(){ return questId; } public char getAnswer(){ return (char)answer.charAt(0); } public String mixThemUp(){ String quesAns= ""; int startLetter = 65; int nextAns; //System.out.println(options.size()); while (options.size() > 1){ //nextAns = num.nextInt(options.size()-1); double nextAn = (Math.random() * 10); while((int)nextAn >= options.size()-1){ nextAn = (Math.random() * 10); } nextAns = (int)nextAn; //System.out.println(nextAns); String stringOption = (String)options.get(nextAns); if(stringOption.equals(answer)) { answer = (char)startLetter + " "; //System.out.println(answer); } quesAns+= (char)startLetter + "." + stringOption + "\n"; options.remove(nextAns); startLetter++; //nextAns = num.nextInt(options.size()-1); } return quesAns; } } |