import java.util.*;
public class QuizDriver
{
public static void main(String[] args)
{
// Create a new Test object
Test exam = new Test("Sample Exam", "Select the correct answer for each question");
setUp(exam);
Scanner sc = new Scanner(System.in);
System.out.println(exam.getInstructions());
for (int i = 0; i < exam.length(); i++)
{
System.out.println(exam.displayQuestion(i));
System.out.print("Your answer: ");
String ans = sc.nextLine();
if (ans.equals(""))
ans = null;
exam.answer(i, ans);
}
exam.scoreExam(true);
System.out.println("Your final score was " + exam.getScore() + " points.");
}
private static void setUp (Test t)
{
Question x;
x = new TrueFalseQuestion("The sky is blue.", "true", 2);
t.addQuestion(x);
x = new TrueFalseQuestion("The first FORTRAN compiler debuted in 1957.", "true", 5);
t.addQuestion(x);
x = new TrueFalseQuestion("Spock was a Vulcan.", "false", 3);
t.addQuestion(x);
x = new MultipleChoiceQuestion("What is the color of the sun?\na.blue\nb.black\nc.orange\nd.green", "c.orange", 2);
t.addQuestion(x);
}
}
import java.util.*;
public class Test
{
public static final int MAX_NUMBER_OF_QUESTIONS = 10;
private String testName;
private int scoreEarned;
private int scorePossible;
private String instructions;
private ArrayList<Question> questions;
public Test (String name, String instr)
{
testName = name;
scoreEarned = 0;
scorePossible = 0;
instructions = instr;
questions = new ArrayList<Question>();
}
public String getInstructions()
{
return instructions;
}
public int getScore()
{
return scoreEarned;
}
public void addQuestion (Question q)
{
scorePossible += q.getPointsValue();
questions.add(q);
}
public String displayQuestion (int position)
{
if (position < questions.size())
{
return (position+1) + ". " + questions.get(position).getQuestion();
}
else
{
return null;
}
}
public String displayTest ()
{
String result = "";
for (int i = 0; i < questions.size(); i++)
{
result += (i+1) + ". (";
Question t = questions.get(i);
result += t.getPointsValue();
result += " points)\n\n" + displayQuestion(i);
result += "\n\n";
}
return result;
}
public int length ()
{
return questions.size();
}
public boolean answer(int number, String a)
{
if (number >= 0 && number < questions.size())
{
Question t = questions.get(number);
t.submitAnswer(a);
return true;
}
else
{
return false;
}
}
// Score exam
public void scoreExam (boolean useNoBS)
{
scoreEarned = 0;
for (int i = 0; i < questions.size(); i++)
{
Question t = questions.get(i);
scoreEarned += t.getPointsEarned(useNoBS);
}
}
}
public class TrueFalseQuestion extends Question
{
public TrueFalseQuestion (String text, String answer, int pts, String ctgry, int level)
{
super(text, answer, pts, ctgry, level);
}
public TrueFalseQuestion (String text, String answer, int pts)
{
super(text, answer, pts, DEFAULT_CATEGORY_VALUE, DEFAULT_DIFFICULTY_LEVEL);
}
public TrueFalseQuestion (String text, String answer, int pts, String ctgry)
{
super(text, answer, pts, ctgry, DEFAULT_DIFFICULTY_LEVEL);
}
public TrueFalseQuestion (String text, String answer, String ctgry)
{
super(text, answer, DEFAULT_POINT_VALUE, ctgry, DEFAULT_DIFFICULTY_LEVEL);
}
public TrueFalseQuestion (String text, String answer, int pts, int level)
{
super(text, answer, pts, DEFAULT_CATEGORY_VALUE, level);
}
public String[] getPossAns ()
{
String[] possAns = {"true", "false"};
return possAns;
}
}
import java.util.*;
public void loadQuestionsFromFile()throws FileNotFoundException
{
String questionType = "";
int pointsValue = 0;
String category = "";
int difficultyLevel = 0;
String questionText = "";
String correctAnswer = "";
String answerChoices = "";
String questionTerminator = "";
File fileReader = new File("testquestions.txt");
Scanner sc = new Scanner(fileReader);
if (sc == null)
{
throw new FileNotFoundException("File cannot be found");
}
while (sc.hasNextLine())
{
String temp = sc.nextLine();
if (temp.equals("TF"))
{
questionType = temp;
pointsValue = Integer.parseInt(sc.nextLine());
category = sc.nextLine();
difficultyLevel = Integer.parseInt(sc.nextLine());
questionText = sc.nextLine();
correctAnswer = sc.nextLine();
questionTerminator = sc.nextLine();
}
else if (temp.equals("MC"))
{
questionType = temp;
pointsValue = Integer.parseInt(sc.nextLine());
category = sc.nextLine();
difficultyLevel = Integer.parseInt(sc.nextLine());
questionText = sc.nextLine();
correctAnswer = sc.nextLine();
answerChoices = sc.nextLine() + sc.nextLine() + sc. nextLine() + sc.nextLine();
questionTerminator = sc.nextLine();
}
Question x = new Question(questionText, correctAnswer, pointsValue, category, difficultyLevel);
addQuestion(x);
sc.nextLine();
}
}
public void addQuestion (Question q)
{
scorePossible += q.getPointsValue();
questions.add(q);
}
public String displayTest ()
{
String result = "";
for (int i = 0; i < questions.size(); i++)
{
result += (i+1) + ". (";
Question t = questions.get(i);
result += t.getPointsValue();
result += " points)\n\n" + displayQuestion(i);
result += "\n\n";
}
return result;
}
File fileReader = new File("testquestions.txt");
Scanner sc = new Scanner(fileReader);
if (sc == null)
{
throw new FileNotFoundException("File cannot be found");
}
while (sc.hasNextLine())
{
String temp = sc.nextLine();
if (temp.equals("TF"))
{
questionType = temp;
pointsValue = Integer.parseInt(sc.nextLine());
category = sc.nextLine();
difficultyLevel = Integer.parseInt(sc.nextLine());
questionText = sc.nextLine();
correctAnswer = sc.nextLine();
questionTerminator = sc.nextLine();
}
else if (temp.equals("MC"))
{
questionType = temp;
pointsValue = Integer.parseInt(sc.nextLine());
category = sc.nextLine();
difficultyLevel = Integer.parseInt(sc.nextLine());
questionText = sc.nextLine();
correctAnswer = sc.nextLine();
answerChoices = sc.nextLine() + "\n" + sc.nextLine() + "\n" + sc. nextLine() + "\n" + sc.nextLine();
questionText += "\n" + answerChoices; //Cannot append answerChoices to questionText until answerChoices receives new value.
questionTerminator = sc.nextLine();
}
Question x = new Question(questionText, correctAnswer, pointsValue, category, difficultyLevel);
addQuestion(x);
import java.util.Scanner;
public class MultipleChoiceQuestion extends Question
{
public MultipleChoiceQuestion (String text, String answer, int pts, String ctgry, int level)
{
super(text, answer, pts, ctgry, level);
}
public MultipleChoiceQuestion (String text, String answer, int pts)
{
super(text, answer, pts, DEFAULT_CATEGORY_VALUE, DEFAULT_DIFFICULTY_LEVEL);
}
public MultipleChoiceQuestion (String text, String answer, int pts, String ctgry)
{
super(text, answer, pts, ctgry, DEFAULT_DIFFICULTY_LEVEL);
}
public MultipleChoiceQuestion (String text, String answer, String ctgry)
{
super(text, answer, DEFAULT_POINT_VALUE, ctgry, DEFAULT_DIFFICULTY_LEVEL);
}
public MultipleChoiceQuestion (String text, String answer, int pts, int level)
{
super(text, answer, pts, DEFAULT_CATEGORY_VALUE, level);
}
public String[] getPossibleAnswer ()
{
String[] PossibleAns = {"a", "b", "c", "d"};
return PossibleAns;
}
public void addAnswerChoice (String answerChoice)
{
Scanner ansChoice = new Scanner(System.in);
System.out.println("Enter your answers");
String answer = ansChoice.nextLine();
}
}
I can't get the test class to load my set of questions loadQuestionsFromFile. Im also trying to write a FileNotFoundException for if a file name was not found and that doesn't seem to be working either and its unable to run.

Ask A New Question
Reply





MultiQuote





|