im trying to do a read/write int property called currentQuestionIndex. This property is the index of the question currently selected, and the value of the property is 0 when a DrivingTest object is first created. I dont understand how to do this. im not sure how to get the index of the current question. i was trying to use an iterator but i dont think i was doin it right.
my question class that hold all my data
package cs320Labs.servlet; public class Question { String description; String answerA; String answerB; String answerC; int correctAnswer; int answer; boolean answerCorrect; public Question(){ } public Question(String description, String answerA, String answerB, String answerC, int correctAnswer) { this.description = description; this.answerA = answerA; this.answerB = answerB; this.answerC = answerC; this.correctAnswer = correctAnswer; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getAnswerA() { return answerA; } public void setAnswerA(String answerA) { this.answerA = answerA; } public String getAnswerB() { return answerB; } public void setAnswerB(String answerB) { this.answerB = answerB; } public String getAnswerC() { return answerC; } public void setAnswerC(String answerC) { this.answerC = answerC; } public int getCorrectAnswer() { return correctAnswer; } public void setCorrectAnswer(int correctAnswer) { this.correctAnswer = correctAnswer; } public int getAnswer() { return answer; } public void setAnswer(int answer) { this.answer = answer; } public boolean isAnswerCorrect() { if (this.answer == correctAnswer) { return answerCorrect == true; } else { return answerCorrect == false; } } public void setAnswerCorrect(boolean answerCorrect) { this.answerCorrect = answerCorrect; } public static void main(String[] args) { } }
my driving test class
public class DrivingTest { static List<Question> driveTestQ = new ArrayList<Question>(); DrivingTest(){ List<Question> driveTestQ = new ArrayList<Question>(); File file = new File("C:/Users/Anthony/Desktop/DrivingTest.txt"); try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { //store first line into qestions String question = scanner.nextLine().trim(); if (question.length() == 0) continue; String[] answers = new String[3]; // store answers into answers array answers[0] = scanner.nextLine(); answers[1] = scanner.nextLine(); answers[2] = scanner.nextLine(); // stores correct answer int correct_answer = Integer.parseInt(scanner.nextLine()); //adds into array list, mus tmatch ur Quetions class driveTestQ.add(new Question(question, answers[0], answers[1], answers[2], correct_answer)); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static int getCurrentQuestionIndex(){ ListIterator listIterator = driveTestQ.listIterator(); return listIterator.nextIndex(); } public static int setCurrentQuestionIndex(int index){ return index; } public static int getCurrentQuestion(){ return 0; } public static boolean islastQuestion (){ boolean current_question = false; boolean last_question = false; if(current_question==last_question){ return false; } return true; } public static int getScore(){ int number_of_correct_answers = 0; int number_of_questions = 0; int score; score =(int) ((double)Math.round(100 * number_of_correct_answers / number_of_questions)); return score; } public static void main(String[] args) { DrivingTest test = new DrivingTest(); System.out.println(test.getCurrentQuestionIndex()); System.out.println(test.setCurrentQuestionIndex(2)); // while( true ) // { // // display the current question // Question question = DrivingTest.getCurrentQuestion(); // System.out.println( question.getDescription() ); // System.out.println( "\t" + question.getAnswerA() ); // System.out.println( "\t" + question.getAnswerB() ); // System.out.println( "\t" + question.getAnswerC() + "\n" ); // // // set the answer to the current question to 1 // DrivingTest.getCurrentQuestion().setAnswer( 1 ); // // // if this is the last question, we are done. // if( DrivingTest.islastQuestion() ) break; // // // it is not the last question, so increment CurrentQuestionIndex // int currentQuestionIndex = DrivingTest.getCurrentQuestionIndex(); // DrivingTest.setCurrentQuestionIndex( currentQuestionIndex + 1 ); // } // // // display the test score // System.out.println( "Your test score is: " + DrivingTest.getScore() ); // } // } }