unsplash-image-qDgTQOYk6B8.jpg

C# - Exam System

Exam System - C#


This program is for creating and taking exams. You can create a question bank of four multiple choice questions from user input, an external file or a combination of both; this includes editing or deleting the created/imported questions. After completing the question bank they can be exported to a separate file. Functionality of taking the test as well as providing your mark is also included.

The main method implements all the methods required to execute this program. There are several classes to deal with the various aspects of this program. The “Exam” class has methods that deal with the exam as a whole. The “Files” class houses all the code that deals with exporting a file and importing from a file. The “InputOutput” class deals with user input and displaying information. The class “MultipleChoiceQuestions” is for creating objects, or questions. Finally, the “Questions” class deals with creating and editing the questions.

A video demonstrating the output and the code is shown below:

Source Code

Main Method
Exam
Files
InputOutput
MultipleChoiceQuestions
Questions
Exported from Notepad++
1 // Name: M Abdelaziz 2 // File: Program.cs 3 // Purpose: A program for a multiple choice exam that creates, displays, edits, deletes, imports and exports questions as well as starts the exam and marks the exam 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_Advanced_Portfolio_mabdelaziz 12 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 // declare variables 18 int menuSelection; 19 bool quitLoop = false; 20 double correctAnswers; 21 22 // declare lists 23 List<MultipleChoiceQuestion> questionsList = new List<MultipleChoiceQuestion>(); 24 List<int> wrongAnswers = new List<int>(); 25 26 // display program header 27 Console.WriteLine("Multiple Choice Exam"); 28 Console.WriteLine("--------------------"); 29 30 while (quitLoop == false) 31 { 32 // display menu 33 InputOutput.DisplayMenu(); 34 35 // ask for menu choice 36 menuSelection = InputOutput.GetMenuChoice(); 37 38 // enact the corresponding decision 39 switch (menuSelection) 40 { 41 case 1: 42 // create a question 43 Questions.Create(questionsList); 44 break; 45 case 2: 46 // display all the questions 47 InputOutput.DisplayQuestions(questionsList); 48 break; 49 case 3: 50 // edit a pre-existing question 51 Questions.Edit(questionsList); 52 break; 53 case 4: 54 // delete a question 55 Questions.Delete(questionsList); 56 break; 57 case 5: 58 // import questions from a file 59 Files.Import(questionsList); 60 break; 61 case 6: 62 // export questions to a file 63 Files.Export(questionsList); 64 break; 65 case 7: 66 // start the exam 67 Exam.Start(questionsList, wrongAnswers); 68 break; 69 case 8: 70 // mark the exam 71 correctAnswers = questionsList.Count - wrongAnswers.Count; 72 Exam.Mark(questionsList, wrongAnswers, correctAnswers); 73 break; 74 default: 75 // quit the program 76 quitLoop = true; 77 break; 78 } 79 80 } 81 82 83 } 84 } 85 } 86
Exported from Notepad++
1 // Name: M Abdelaziz 2 // File: Exam.cs 3 // Purpose: All the methods that deal with the exam as a whole (starting, marking) 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_Advanced_Portfolio_mabdelaziz 12 { 13 class Exam 14 { 15 public static void Start(List<MultipleChoiceQuestion> questionsList, List<int> wrongAnswers) 16 { 17 // declare variables 18 char userInput; 19 20 // clear the wrong Answers array 21 wrongAnswers.Clear(); 22 23 // show what option we are in 24 Console.WriteLine("Multiple Choice Exam"); 25 Console.WriteLine("--------------------\n"); 26 27 // display all the questions 28 for (int index = 0; index < questionsList.Count; index++) 29 { 30 int questionNumber = index + 1; 31 Console.WriteLine("Question {0}: ", questionNumber); 32 Console.WriteLine("------------------------------"); 33 Console.WriteLine("{0} \nA. {1} \nB. {2} \nC. {3} \nD. {4} \n", 34 questionsList[index].Question, 35 questionsList[index].Choice1, 36 questionsList[index].Choice2, 37 questionsList[index].Choice3, 38 questionsList[index].Choice4); 39 Console.Write("Your Answer (A,B,C,D): "); 40 41 // take in the user input 42 userInput = InputOutput.GetQuestionChoice("Your Answer (A,B,C,D): "); 43 Console.WriteLine(""); 44 45 // compare the input with the correct answer and act accordingly 46 if (userInput != questionsList[index].CorrectChoice) 47 { 48 wrongAnswers.Add(questionNumber); 49 } 50 } 51 52 Console.Write("Press any key to continue. "); 53 Console.ReadKey(); 54 } 55 56 public static void Mark(List<MultipleChoiceQuestion> questionsList, List<int> wrongAnswers, double correctAnswers) 57 { 58 // declare variables 59 double percent; 60 61 // decide whether they failed or passed 62 percent = (correctAnswers / questionsList.Count); 63 if (percent >= .50) 64 { 65 // if passed 66 Console.WriteLine("You got {0:P2}. You got {1} questions correct out of {2}.", percent, correctAnswers, questionsList.Count); 67 Console.WriteLine("You passed the exam."); 68 } 69 else 70 { 71 // if failed 72 Console.WriteLine("You got {0:P2}. You got {1} questions correct out of {2}.", percent, correctAnswers, questionsList.Count); 73 Console.WriteLine("You failed the exam."); 74 Console.WriteLine("The following questions were answered incorrectly: "); 75 // display the questions that they got wrong 76 for (int index = 0; index < wrongAnswers.Count; index++) 77 { 78 if (index == wrongAnswers.Count - 1) 79 { 80 Console.WriteLine("{0} ", wrongAnswers[index]); 81 } 82 else 83 { 84 Console.Write("{0} ", wrongAnswers[index]); 85 } 86 } 87 } 88 Console.Write("Press any key to continue. "); 89 Console.ReadKey(); 90 } 91 } 92 } 93
Exported from Notepad++
1 // Name: M Abdelaziz 2 // File: Files.cs 3 // Purpose: All the methods dealing with external files 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.IO; 11 12 namespace CPSC1012_Advanced_Portfolio_mabdelaziz 13 { 14 class Files 15 { 16 public static void Import(List<MultipleChoiceQuestion> questionsList) 17 { 18 // declare variables 19 string lineOfText; 20 string question; 21 string choice1; 22 string choice2; 23 string choice3; 24 string choice4; 25 char correctChoice; 26 string fileName; 27 28 // display what option we are in 29 Console.WriteLine("Multiple Choice Exam - Import Questions"); 30 Console.WriteLine("---------------------------------------"); 31 32 // ask for and capture file path 33 Console.WriteLine("Enter the location of the file to read from:"); 34 fileName = Console.ReadLine(); 35 36 // reads a stream of text 37 StreamReader reader = null; 38 39 try 40 { 41 // create an instance of StreamReader which opens the file and reads a stream of text 42 reader = new StreamReader(fileName); 43 44 // clear current list 45 questionsList.Clear(); 46 47 // read until there are no more records 48 while ((lineOfText = reader.ReadLine()) != null || questionsList.Count == 30) 49 { 50 // create a string array to hold a record 51 // SPLIT splits a record based on the character you say 52 string[] fileRecordArray = lineOfText.Split('|'); 53 54 // populate the variables for one instance 55 question = fileRecordArray[0]; 56 choice1 = fileRecordArray[1]; 57 choice2 = fileRecordArray[2]; 58 choice3 = fileRecordArray[3]; 59 choice4 = fileRecordArray[4]; 60 correctChoice = char.Parse(fileRecordArray[5].Trim().ToUpper()); 61 62 // create a new instance of Student and Add it to the List 63 questionsList.Add(new MultipleChoiceQuestion(question, choice1, choice2, choice3, choice4, correctChoice)); 64 65 if (questionsList.Count == 30) 66 { 67 Console.WriteLine("You have reached the maximum of 30 allowable questions. "); 68 } 69 } 70 } 71 // if file path is incorrect 72 catch (Exception ex) 73 { 74 Console.WriteLine("{0} '{1}'.", ex.Message, fileName); ; 75 } 76 finally 77 { 78 if (reader != null) 79 { 80 // close the file (can be open and closed many times) 81 reader.Close(); 82 // release any connections to the file 83 reader.Dispose(); 84 } 85 } 86 Console.WriteLine("Added {0} questions.", questionsList.Count); 87 Console.Write("Press any key to continue. "); 88 Console.ReadKey(); 89 } 90 91 public static void Export(List<MultipleChoiceQuestion> questionsList) 92 { 93 // declare variables 94 string fileName; 95 96 // display what option we are in 97 Console.WriteLine("Multiple Choice Exam - Export Questions"); 98 Console.WriteLine("---------------------------------------"); 99 100 // ask for and capture file path 101 Console.WriteLine("Enter the location of the file to save to:"); 102 fileName = Console.ReadLine(); 103 104 // create a variable of StreamWriter 105 StreamWriter writer = null; 106 try 107 { 108 // create an instance of StreamWriter 109 writer = new StreamWriter(fileName, false); 110 for (int index = 0; index < questionsList.Count; index++) 111 { 112 // write to the file 113 writer.WriteLine("{0}|{1}|{2}|{3}|{4}|{5}", 114 questionsList[index].Question, 115 questionsList[index].Choice1, 116 questionsList[index].Choice2, 117 questionsList[index].Choice3, 118 questionsList[index].Choice4, 119 questionsList[index].CorrectChoice); 120 } 121 } 122 // if file path is incorrect 123 catch (Exception ex) 124 { 125 Console.WriteLine("{0} '{1}'.", ex.Message, fileName); 126 } 127 finally 128 { 129 if (writer != null) 130 { 131 writer.Close(); 132 writer.Dispose(); 133 } 134 } 135 Console.Write("Press any key to continue. "); 136 Console.ReadKey(); 137 } 138 } 139 } 140
Exported from Notepad++
1 // Name: M Abdelaziz 2 // File: InputOutput.cs 3 // Purpose: All the methods dealing with user input and functions that require only displaying information (nothing with a mixture of the two) 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_Advanced_Portfolio_mabdelaziz 12 { 13 class InputOutput 14 { 15 public static void DisplayMenu() 16 { 17 // declare variables 18 // set menu choices here to easily change if need be 19 string menuHeader = "Please enter:"; 20 string menuOptionOne = "1 - to create a new question,"; 21 string menuOptionTwo = "2 - to display all questions,"; 22 string menuOptionThree = "3 - to edit a question,"; 23 string menuOptionFour = "4 - to delete a question,"; 24 string menuOptionFive = "5 - to import qestions from a file,"; 25 string menuOptionSix = "6 - to export questions to a file,"; 26 string menuOptionSeven = "7 - to start the exam,"; 27 string menuOptionEight = "8 - to mark the exam,"; 28 string menuOptionZero = "0 - to exit the program."; 29 30 // place an empty line before 31 Console.WriteLine(""); 32 // display menu 33 Console.WriteLine("{0}", menuHeader); 34 Console.WriteLine(" {0}", menuOptionOne); 35 Console.WriteLine(" {0}", menuOptionTwo); 36 Console.WriteLine(" {0}", menuOptionThree); 37 Console.WriteLine(" {0}", menuOptionFour); 38 Console.WriteLine(" {0}", menuOptionFive); 39 Console.WriteLine(" {0}", menuOptionSix); 40 Console.WriteLine(" {0}", menuOptionSeven); 41 Console.WriteLine(" {0}", menuOptionEight); 42 Console.WriteLine(" {0}", menuOptionZero); 43 // place an empty line afterwards 44 Console.WriteLine(""); 45 } 46 47 public static int GetMenuChoice() 48 { 49 // declare variables 50 int intValue; 51 bool inputIsValid = false; 52 string inputQuestion = "Option? "; 53 string errorMessage = "*** Invalid menu choice."; 54 55 // loop to determine whether char is valid menu choice 56 do 57 { 58 // ask for and capture an char value 59 Console.Write(inputQuestion); 60 61 // loop to determine whether the input is a character 62 while (!int.TryParse(Console.ReadLine().Trim(), out intValue)) 63 { 64 Console.WriteLine(errorMessage); 65 Console.Write(inputQuestion); 66 } 67 if (intValue != 1 && intValue != 2 && intValue != 3 && intValue != 4 && intValue != 5 && 68 intValue != 6 && intValue != 7 && intValue != 8 && intValue != 0) 69 { 70 Console.WriteLine(errorMessage); 71 } 72 else 73 { 74 inputIsValid = true; 75 } 76 } while (inputIsValid == false); 77 78 return intValue; 79 } 80 81 public static char GetQuestionChoice(string inputQuestion) 82 { 83 // declare variables 84 char charValue; 85 bool inputIsValid = false; 86 string errorMessage = "*** Invalid answer"; 87 88 // loop to determine whether char is valid menu choice 89 do 90 { 91 // loop to determine whether the input is a character 92 while (!char.TryParse(Console.ReadLine().Trim().ToUpper(), out charValue)) 93 { 94 Console.WriteLine(errorMessage); 95 Console.Write(inputQuestion); 96 } 97 if (!charValue.Equals('A') && !charValue.Equals('B') && !charValue.Equals('C') && !charValue.Equals('D')) 98 { 99 Console.WriteLine(errorMessage); 100 Console.Write(inputQuestion); 101 } 102 else 103 { 104 inputIsValid = true; 105 } 106 } while (inputIsValid == false); 107 108 return charValue; 109 } 110 111 112 public static void DisplayQuestions(List<MultipleChoiceQuestion> questionsList) 113 { 114 // write that we are displaying all the questions 115 Console.WriteLine("Multiple Choice Exam - Display All Questions"); 116 Console.WriteLine("--------------------------------------------"); 117 118 // loop to display all question properties at each index of array 119 for (int index = 0; index < questionsList.Count; index++) 120 { 121 Console.WriteLine("Question {0}: ", index + 1); 122 Console.WriteLine("------------------------------"); 123 Console.WriteLine("{0} \nA. {1} \nB. {2} \nC. {3} \nD. {4} \n", 124 questionsList[index].Question, 125 questionsList[index].Choice1, 126 questionsList[index].Choice2, 127 questionsList[index].Choice3, 128 questionsList[index].Choice4); 129 } 130 131 Console.Write("Press any key to continue. "); 132 Console.ReadKey(); 133 } 134 } 135 } 136
Exported from Notepad++
1 // Name: M Abdelaziz 2 // File: MultipleChoiceQuestion.cs 3 // Purpose: The class for the object MultipleChoiceQuestion 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_Advanced_Portfolio_mabdelaziz 12 { 13 class MultipleChoiceQuestion 14 { 15 // Fields 16 private string _question; 17 private string _choice1; 18 private string _choice2; 19 private string _choice3; 20 private string _choice4; 21 private char _correctChoice; 22 23 // Properties 24 public string Question 25 { 26 get 27 { 28 return _question; 29 } 30 set 31 { 32 _question = value; 33 } 34 } 35 public string Choice1 36 { 37 get 38 { 39 return _choice1; 40 } 41 set 42 { 43 _choice1 = value; 44 } 45 } 46 public string Choice2 47 { 48 get 49 { 50 return _choice2; 51 } 52 set 53 { 54 _choice2 = value; 55 } 56 } 57 public string Choice3 58 { 59 get 60 { 61 return _choice3; 62 } 63 set 64 { 65 _choice3 = value; 66 } 67 } 68 public string Choice4 69 { 70 get 71 { 72 return _choice4; 73 } 74 set 75 { 76 _choice4 = value; 77 } 78 } 79 public char CorrectChoice 80 { 81 get 82 { 83 return _correctChoice; 84 } 85 set 86 { 87 if (value != 'A' && value != 'B' && value != 'C' && value != 'D') 88 { 89 throw new Exception("The correct choice for all questions must be A, B, C, or D. Check and fix file "); 90 } 91 else 92 { 93 _correctChoice = value; 94 } 95 } 96 } 97 98 // Constructor(s) 99 // default constructor 100 public MultipleChoiceQuestion() 101 { 102 Question = "Question?"; 103 Choice1 = "Answer A"; 104 Choice2 = "Answer B"; 105 Choice3 = "Answer C"; 106 Choice4 = "Answer D"; 107 CorrectChoice = 'A'; 108 } 109 110 // non-default constructor 111 public MultipleChoiceQuestion(string question, string choice1, string choice2, string choice3, string choice4, char correctChoice) 112 { 113 Question = question; 114 Choice1 = choice1; 115 Choice2 = choice2; 116 Choice3 = choice3; 117 Choice4 = choice4; 118 CorrectChoice = correctChoice; 119 } 120 // Methods 121 } 122 } 123
Exported from Notepad++
1 // Name: M Abdelaziz 2 // File: Questions.cs 3 // Purpose: All the methods that deal with questions other than output (creating, editing, deleting) 4 5 using System; 6 using System.Collections.Generic; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace CPSC1012_Advanced_Portfolio_mabdelaziz 12 { 13 class Questions 14 { 15 public static void Create(List<MultipleChoiceQuestion> questionsList) 16 { 17 // declare variables 18 string question; 19 string choice1; 20 string choice2; 21 string choice3; 22 string choice4; 23 char correctChoice; 24 25 // display what option we are in 26 Console.WriteLine("Multiple Choice Exam - New Question"); 27 Console.WriteLine("-----------------------------------"); 28 Console.WriteLine("There are currently {0} questions in the exam. \n", questionsList.Count); 29 30 if (questionsList.Count > 30) 31 { 32 Console.WriteLine("The exam is at the maximum of 30 questions"); 33 } 34 else 35 { 36 // ask for and capture components 37 Console.WriteLine("Enter the question:"); 38 question = Console.ReadLine(); 39 Console.WriteLine("Enter choice 1 for the question:"); 40 choice1 = Console.ReadLine(); 41 Console.WriteLine("Enter choice 2 for the question:"); 42 choice2 = Console.ReadLine(); 43 Console.WriteLine("Enter choice 3 for the question:"); 44 choice3 = Console.ReadLine(); 45 Console.WriteLine("Enter choice 4 for the question:"); 46 choice4 = Console.ReadLine(); 47 Console.WriteLine("Enter the correct choice (A, B, C, D):"); 48 correctChoice = InputOutput.GetQuestionChoice("Enter the correct choice (A, B, C, D):\n"); 49 50 // add components to new object in list 51 questionsList.Add(new MultipleChoiceQuestion(question, choice1, choice2, choice3, choice4, correctChoice)); 52 Console.WriteLine("Successfully added question {0} to the exam.", questionsList.Count); 53 } 54 55 Console.Write("Press any key to continue. "); 56 Console.ReadKey(); 57 } 58 59 public static void Delete(List<MultipleChoiceQuestion> questionsList) 60 { 61 // declare variables 62 int questionNumber; 63 char confirmation; 64 65 // display what option we are in 66 Console.WriteLine("Multiple Choice Exam - Delete Question"); 67 Console.WriteLine("--------------------------------------"); 68 69 // ask for and capture question to delete 70 Console.WriteLine("Enter the question number to delete:"); 71 questionNumber = int.Parse(Console.ReadLine()); 72 int index = questionNumber - 1; 73 74 // check if question number is valid 75 // if valid 76 if (questionNumber <= questionsList.Count && questionNumber > 0) 77 { 78 Console.WriteLine("Found the following question:"); 79 80 // display question 81 Console.WriteLine("Question {0}: ", questionNumber); 82 Console.WriteLine("------------------------------"); 83 Console.WriteLine("{0} \n{1} \n{2} \n{3} \n{4} \n", 84 questionsList[index].Question, 85 questionsList[index].Choice1, 86 questionsList[index].Choice2, 87 questionsList[index].Choice3, 88 questionsList[index].Choice4); 89 90 // ask for confirmation 91 Console.WriteLine("Are you sure you want to delete question {0} (y/n)?", questionNumber); 92 confirmation = char.Parse(Console.ReadLine()); 93 94 if (confirmation == 'y') 95 { 96 // delete question 97 questionsList.RemoveAt(index); 98 Console.WriteLine("Successfully deleted question {0}", questionNumber); 99 } 100 else 101 { 102 // tell them question was not deleted 103 Console.WriteLine("Question {0} was not deleted. ", questionNumber); 104 } 105 } 106 // if not valid 107 else 108 { 109 Console.WriteLine("Error! {0} is not a valid question number.", questionNumber); 110 } 111 112 Console.Write("Press any key to continue. "); 113 Console.ReadKey(); 114 } 115 116 public static void Edit(List<MultipleChoiceQuestion> questionsList) 117 { 118 // declare variables 119 int questionNumber; 120 string question; 121 string choice1; 122 string choice2; 123 string choice3; 124 string choice4; 125 char correctChoice; 126 127 // display what option we are in 128 Console.WriteLine("Multiple Choice Exam - Edit Question"); 129 Console.WriteLine("------------------------------------"); 130 131 // ask for and capture question to delete 132 Console.WriteLine("Enter the question number to Edit:"); 133 questionNumber = int.Parse(Console.ReadLine()); 134 int index = questionNumber - 1; 135 136 // check if question number is valid 137 // if valid 138 if (questionNumber <= questionsList.Count && questionNumber > 0) 139 { 140 Console.WriteLine("The question is as follows:"); 141 142 // display question 143 Console.WriteLine("Question {0}: ", questionNumber); 144 Console.WriteLine("------------------------------"); 145 Console.WriteLine("{0} \nA. {1} \nB. {2} \nC. {3} \nD. {4} \n", 146 questionsList[index].Question, 147 questionsList[index].Choice1, 148 questionsList[index].Choice2, 149 questionsList[index].Choice3, 150 questionsList[index].Choice4); 151 152 // ask for and capture components 153 Console.WriteLine("Enter the question:"); 154 question = Console.ReadLine(); 155 Console.WriteLine("Enter choice 1 for the question:"); 156 choice1 = Console.ReadLine(); 157 Console.WriteLine("Enter choice 2 for the question:"); 158 choice2 = Console.ReadLine(); 159 Console.WriteLine("Enter choice 3 for the question:"); 160 choice3 = Console.ReadLine(); 161 Console.WriteLine("Enter choice 4 for the question:"); 162 choice4 = Console.ReadLine(); 163 Console.WriteLine("Enter the correct choice (A, B, C, D):"); 164 correctChoice = InputOutput.GetQuestionChoice("Enter the correct choice (A, B, C, D):\n"); 165 166 // replace list entry at index 167 questionsList.RemoveAt(index); 168 questionsList.Insert(index, new MultipleChoiceQuestion(question, choice1, choice2, choice3, choice4, correctChoice)); 169 170 Console.WriteLine("Successfully updated question {0}", questionNumber); 171 } 172 // if not valid 173 else 174 { 175 Console.WriteLine("Error! {0} is not a valid question number.", questionNumber); 176 } 177 178 Console.Write("Press any key to continue. "); 179 Console.ReadKey(); 180 181 } 182 183 } 184 } 185