Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Quiz Maker Rate Topic: -----

#1 cmdrarc  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-March 08


Dream Kudos: 0

Share |

Quiz Maker

Post icon  Posted 10 May 2008 - 12:47 PM

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.
Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1336
  • View blog
  • Posts: 8,175
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: Quiz Maker

Posted 10 May 2008 - 07:24 PM

Your code is a bit confusing. Is your loadQuestionsFromFile() part of one of the other classes or is it in a file by itself? It should be attached to a class somewhere.

Also when you do file anything you need to include the java.io package. This package also defines FileNotFoundException. So if it is saying something along the lines of symbol not found, then that is why.

So add loadQuestionsFromFile to the appropriate class and whatever class that is, make sure it also has imported the java.io.* package at the top.

:)
Was This Post Helpful? 0
  • +
  • -

#3 cmdrarc  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-March 08


Dream Kudos: 0

Re: Quiz Maker

Posted 11 May 2008 - 07:10 PM

View PostMartyr2, on 10 May, 2008 - 08:24 PM, said:

Your code is a bit confusing. Is your loadQuestionsFromFile() part of one of the other classes or is it in a file by itself? It should be attached to a class somewhere.

Also when you do file anything you need to include the java.io package. This package also defines FileNotFoundException. So if it is saying something along the lines of symbol not found, then that is why.

So add loadQuestionsFromFile to the appropriate class and whatever class that is, make sure it also has imported the java.io.* package at the top.

:)


THIS IS THE PART OFF THE ASSIGNMENT THAT THE LoadQuestionsFromFile suppose to be from and that the coding to what
I have came up with on top
lst class so that it can load a set of questions from a plain-text data file (the file format is described in a separate file posted in the "Assignments" section of Blackboard). To do this, implement a loadQuestionsFromFile() method that conforms to the following specifications:

Your method should have the following header:

public void loadQuestionsFromFile (String filename) throws FileNotFoundException

If no file exists with the name supplied as an argument, your method should throw a FileNotFoundException to allow the client to decide how to handle this situation. Note that this still requires you to use a try-catch block in your method.

The data file may contain an arbitrary number of question records; your method must read all of them, and store them in the Test object's internal ArrayList. To do this, use a while loop that reads data from the file, one line at a time, until your Scanner's hasNextLine() method indicates that the end of the file has been reached.

AND THIS I HAVE NO Idea how to accomplish
Modify your QuizDriver program so that it creates Tests from data files instead of hard-coded data. Your main() method should create a Test from a data file in one of two ways:

If the user supplies a filename as a command-line argument, that file should be used to initialize the Test.

Otherwise, your program should prompt the user to enter the name of a data file to use, and then pass that filename to the loadQuestionsFromFile() method.



Implement the following methods in your Test class:

public void sortByDifficulty()

public void sortByPointsValue()


Both of these methods sort the questions by the relevant field, in increasing order. You may perform the sorting procedure using any algorithm you wish (bubble sort, selection sort, insertion sort, etc.).

(Hint: Use the set() method in the ArrayList class)

Add a saveTestToFile() method to your Test class. This method should take the destination filename (as a String) as its argument, and generate a plain text file containing the list of questions held in the Test object (in the same form that you would display the Test to a user).

This additional functionality is worth an extra 10 points.

Modify your Question class and subclasses to include a method that produces a String representation of the question according to the file format used above. Use this code to implement a saveRawTestToFile() method to your Test class. This method takes a filename (a String) as its argument and generates a data file similar to the ones used as input for the loadQuestionsFromFile() method.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users