import waba.ui.*; import waba.fx.*; import waba.sys.*; import waba.util.*; public class Quiz extends MainWindow { MessageBox mb; Button btnNext, btnPrevious, btnGradeIt, btnQuit; Button btnStart, btnHelp, btnAbout; Label lbQuestionNumber, lbQuestion1, lbQuestion2, lbQuestion3, lbAnswerA, lbAnswerB, lbAnswerC; Radio rdAnswerA, rdAnswerB, rdAnswerC; RadioGroup rg; ComboBox cb; Vector vQuestions; String arAnswers[]; String arKeyAnswers[]; int arRadioState[]; int currentQuestion; int question; public void onStart() { String items[] = {"Java", "State Capitals", "Communications", "More Comms", "Even more Comms", "System Analysis"}; vQuestions = new Vector(70); arAnswers = new String[10]; arKeyAnswers = new String[10]; arRadioState = new int[10]; rg = new RadioGroup(); // add question number add(lbQuestionNumber = new Label(" ")); lbQuestionNumber.setRect(0,0,158,10); // add up to three line of question add(lbQuestion1 = new Label(" ")); lbQuestion1.setRect(0,20,158,10); add(lbQuestion2 = new Label(" ")); lbQuestion2.setRect(0,30,158,10); add(lbQuestion3 = new Label(" ")); lbQuestion3.setRect(0,40,158,10); // add Answer A add(rdAnswerA = new Radio("",rg)); rdAnswerA.setRect(0,60,158,10); add(lbAnswerA = new Label(" ")); lbAnswerA.setRect(13,60,158,10); // add Answer B add(rdAnswerB = new Radio("",rg)); rdAnswerB.setRect(0,70,158,10); add(lbAnswerB = new Label("")); lbAnswerB.setRect(13,70,158,10); // add Answer C add(rdAnswerC = new Radio("",rg)); rdAnswerC.setRect(0,80,158,10); add(lbAnswerC = new Label(" ")); lbAnswerC.setRect(13,80,158,10); add(cb = new ComboBox(items)); cb.setRect(0,100,158,15); //cb.select(0); // add button for Next, Previous, Grade It, and Quit add(btnNext = new Button("Next")); btnNext.setRect(0,this.height-30,PREFERRED,15); add(btnPrevious = new Button("Previous")); btnPrevious.setRect(30,this.height-30,PREFERRED,15); add(btnGradeIt = new Button("Grade It")); btnGradeIt.setRect(70,this.height-30,PREFERRED,15); add(btnQuit = new Button("Quit")); btnQuit.setRect(115,this.height-30,PREFERRED,15); // add button for Pick Exam, Help, About add(btnStart = new Button("Start")); btnStart.setRect(0,this.height-15,PREFERRED,15); add(btnHelp = new Button("Help")); btnHelp.setRect(50,this.height-15,PREFERRED,15); add(btnAbout = new Button("About")); btnAbout.setRect(80,this.height-15,PREFERRED,15); showHelp(); } public void onEvent(Event event) { if (event.type == ControlEvent.PRESSED) { if (event.target == btnQuit) exit(0); else if (event.target == btnAbout) showAbout(); else if (event.target == btnHelp) showHelp(); else if (event.target == btnNext) { doNextButton(); } else if (event.target == btnStart) { doStart(cb.getSelectedIndex()); } else if (event.target == btnPrevious) { if (currentQuestion == 0) { lbQuestionNumber.setText("!!! First Question"); Sound.beep(); } else { currentQuestion -= 7; showQuestions(currentQuestion, vQuestions); lbQuestionNumber.setText("Question number " + --question); updateRadioState(question); } } else if (event.target == btnGradeIt) gradeIt(); if (event.target == rdAnswerA) { arAnswers[question] = lbAnswerA.getText(); arRadioState[question] = 1; } else if (event.target == rdAnswerB) { arAnswers[question] = lbAnswerB.getText(); arRadioState[question] = 2; } else if (event.target == rdAnswerC) { arAnswers[question] = lbAnswerC.getText(); arRadioState[question] = 3; } } } // do Next Button, read answer, clears radio button for the next question private void doNextButton() { if (currentQuestion > 62) { lbQuestionNumber.setText("!!! Last Question"); Sound.beep(); } else { // check for answers currentQuestion += 7; showQuestions(currentQuestion, vQuestions); lbQuestionNumber.setText("Question number " + ++question); updateRadioState(question); } } private void updateRadioState(int n) { // clears all radios rdAnswerA.setChecked(false); rdAnswerB.setChecked(false); rdAnswerC.setChecked(false); // switch only if already answered switch(arRadioState[n]) { case 1 : rdAnswerA.setChecked(true); break; case 2 : rdAnswerB.setChecked(true); break; case 3 : rdAnswerC.setChecked(true); break; default : break; } } private void doStart(int index) { question = 0; currentQuestion = 0; clearArrays(); String fileName = "Q00"+index+".jECb.Q00"+index; ReaderQuestion rq = new ReaderQuestion(fileName); vQuestions = rq.readRecords(); buildKeyAnswers(); showQuestions(currentQuestion, vQuestions); } private void clearArrays() { for(int i=0; i<10; i++) { arAnswers[i] = null; arKeyAnswers[i] = null; arRadioState[i] = 0; } } // build key answers, the seventh record is the key answers private void buildKeyAnswers() { arKeyAnswers[0] = (String) vQuestions.get(6); arKeyAnswers[1] = (String) vQuestions.get(13); arKeyAnswers[2] = (String) vQuestions.get(20); arKeyAnswers[3] = (String) vQuestions.get(27); arKeyAnswers[4] = (String) vQuestions.get(34); arKeyAnswers[5] = (String) vQuestions.get(41); arKeyAnswers[6] = (String) vQuestions.get(48); arKeyAnswers[7] = (String) vQuestions.get(55); arKeyAnswers[8] = (String) vQuestions.get(62); arKeyAnswers[9] = (String) vQuestions.get(69); } // show Questions private void showQuestions(int index, Vector v) { lbQuestion1.setText((String) v.get(index++)); lbQuestion2.setText((String) v.get(index++)); lbQuestion3.setText((String) v.get(index++)); lbAnswerA.setText((String) v.get(index++)); lbAnswerB.setText((String) v.get(index++)); lbAnswerC.setText((String) v.get(index++)); } // grade the quiz private void gradeIt() { MessageBox mb1; String s1[], s2[]; int correct; correct = 0; s1 = new String[10]; s2 = new String[10]; for(int i=0; i<10; i++) if (arKeyAnswers[i].equals(arAnswers[i])) { correct++; s1[i] = "Question" + (i+1) + " correct!"; s2[i] = arKeyAnswers[i]; } else { s1[i] = "Question" + (i+1) + " wrong! "; s2[i] = arKeyAnswers[i]; } String s = String.valueOf(correct); mb1 = new MessageBox("Results", "Total Question 10 | You have " + s + " correct"+"|"+ s1[0]+"|"+s2[0]+"|"+s1[1]+"|"+s2[1]+"|"+s1[2]+"|"+s2[2]+"|"+s1[3]+"|"+s2[3]+"|"+ s1[4]+"|"+s2[4]+"|"+s1[5]+"|"+s2[5]+"|"+s1[6]+"|"+s2[6]+"|"+s1[7]+"|"+s2[7]+"|"+ s1[8]+"|"+s2[8]+"|"+s1[9]+"|"+s2[9]); mb1.setDoubleBuffer(true); popupModal(mb1); clearArrays(); buildKeyAnswers(); question = 0; currentQuestion = 0; showQuestions(currentQuestion, vQuestions); updateRadioState(question); } private void showHelp() { MessageBox mb1; mb1 = new MessageBox("Help", "Pick a topic from the list | by pressing right down arrow | then press start button. | After answering the questions " + " |Use the Next and Previous| buttons to navigate. | Press Grade It button to see | the result of the Quiz."); mb1.setDoubleBuffer(true); popupModal(mb1); } // show about ourself private void showAbout() { mb = new MessageBox("Quiz Program", "Quiz Program by Sonny Cruz | Free for the Palm and compatible. | http://pages.sbcglobal.net/icruz"); mb.setDoubleBuffer(true); popupModal(mb); } }