Updated quiz code, how to imlement C# OOP classes to accomplish the same thing
$begingroup$
I asked for help on a C# Quiz program I am writing for my ENGR115 class in this thread: Original Question
Roland replied with some good advice for me, which I followed, and came up with this revised code:
using System;
using System.Threading;
namespace Quiz
{
class Program
{
static void Main(string args)
{
//start the quiz by telling the student the details of the structure of the quiz and the subject being tested
Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 4 & 5");
Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
Console.WriteLine();
//create an array with all the questions and possible answers to display to the user
var questions = new string { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros?",
"Question 2: Multiple choice – Add the following binary numbers: 1011+0011. "
+"Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032.",
"Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. "
+"Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001.",
"Question 4: True or False – Binary numbers are base-10? ",
"Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. "
+"Answers: 1) 1010, 2) 1001, 3) 1000, 4) 0100.",
"Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. "
+"Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101.",
"Question 7: True or False – Binary numbers are base-2? ",
"Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? "
+"Answers: 1) 11, 2) 22, 3) 14, 4) 7.",
"Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? "
+"Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110.",
"Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? "};
//creating array of correct answers, input variables for each question to accept user input in an array, and question status array to track if the question was answered correctly the first time
var questionAnswer = new string { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };
var questionInput = new string[10];
var questionStatus = new string { "n", "n", "n", "n", "n", "n", "n", "n", "n", "n" };
//define the question types (tf or mc) in an array, set the question iteration globally, and set the question index globally
var questionType = new string { "tf", "mc", "mc", "tf", "mc", "mc", "tf", "mc", "mc", "tf" };
var questionIteration = 0;
var questionIndex = 0;
//defines and sets the true/false counter and multiple choice counters to zero
var trueFalseCount = 0; var multipleChoiceCount = 0;
//start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
for (questionIteration = 1; questionIteration < 3; questionIteration++)//set up a for loop to run 2 iterations of the questions
{
questionIndex = 0;
if (questionIteration == 2)
{
Console.WriteLine("Second attempt. Previously incorrectly answered questions will be displayed again. Good luck!");
}
//foreach loop to handle each question and the answer input by the user, plus logic to test for invalid and correct/incorrect answers
foreach (string question in questions)
{
if (questionStatus[questionIndex] == "n")//first attempt at the question
{
Console.WriteLine(question); //displays the question, starting with question 1
if (questionType[questionIndex] == "tf")
{
Console.WriteLine("Press T or F and Enter to submit");
}
else if (questionType[questionIndex] == "mc")
{
Console.WriteLine("Select the number (1, 2, 3 or 4) of the correct answer and press Enter to submit");
}
if (questionType[questionIndex] == "tf")//logic for true/false questions
{
questionInput[questionIndex] = InvalidTrueFalseEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
else if (questionType[questionIndex] == "mc")//logic for multiple choice questions
{
questionInput[questionIndex] = InvalidMultipleChoiceEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
}
questionIndex++;//increment the question counter for the next iteration
}
}
Console.WriteLine("Test Complete!");//tells user the test is over
Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
//this method will check multiple choice answers to ensure they are a number 1-4
string InvalidMultipleChoiceEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "1" || answer == "2" || answer == "3" || answer == "4")
{
return answer;
}
Console.WriteLine("Invalid answer type (not 1-4), try again.");
}
}
//this method will check true/false answers to ensure input was a "t" or an "f"
string InvalidTrueFalseEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "t" || answer == "f")
{
return answer;
}
Console.WriteLine("Invalid answer type (not t or f), try again.");
}
}
//this method will check the answer to see if it is correct or not, if the entry was valid for the questionType
string CheckForCorrectAnswer()
{
var answer = questionInput[questionIndex];
if (answer != questionAnswer[questionIndex])//tests for incorrect answer given from a valid input by question type
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Sorry, wrong answer. :-(");
Console.WriteLine();
questionStatus[questionIndex] = "n";//make sure the question status reflects "n"
return answer;
}
else //it must be correct, since we've checked against invalid and incorrect answers!
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Correct answer!");
questionStatus[questionIndex] = "y";
if (questionType[questionIndex] == "tf")
{
trueFalseCount++;//increment the true/false counter to total the number answered correctly
}
else
{
multipleChoiceCount++;//increment the multiple choice counter to total the number answered correctly
}
Console.WriteLine();
return answer;
}
}
}
}
}
This week I have a requirement to implement C# OOP principles into my code design and provide my source code; a document with a problem statement and narrative about my approach and experience; the program's design (flow chart and class diagram); and testing, execution and debugging output from my program.
As I try to wrap my head around all these OOP principles, between reading my text, doing practice coding of small projects, and attempting to re-write my code, I've become "stuck" on the best way to do this. Currently, I'm looking at this post
as my "example" to model from and to learn from the responses Coding Yoshi provided the OP in an effort to complete this project.
Any advice on how best to do this, using my existing code as the example of what the Quiz behavior is supposed to be, but implementing OOP? Right now I'm thinking:
Program.cs - class that runs the quiz program;
QuestionsAndAnswers.cs - class with the questions and answers defined;
TestAnswersToDetermineIfCorrect.cs - class that implements the logic to determine is the answers the user inputs are 1. valid entries and 2. correct or not;
CalculateResults.cs - class that calculates number of questions answered correctly;
DisplayResults.cs - class that displays the results of the test at the end.
Does this sound plausible? I'm concerned that because this is such a small program that this will make it more complicated than it is currently. Of course, if this is true and i leave my code "as-is", it defeats the purpose of me using this assignment as a learning tool, so I have to re-write it using OOP regardless. My challenge now is to actually code these classes. All suggestions/help is welcome and appreciated. Thanks in advance.
c# object-oriented quiz
New contributor
$endgroup$
add a comment |
$begingroup$
I asked for help on a C# Quiz program I am writing for my ENGR115 class in this thread: Original Question
Roland replied with some good advice for me, which I followed, and came up with this revised code:
using System;
using System.Threading;
namespace Quiz
{
class Program
{
static void Main(string args)
{
//start the quiz by telling the student the details of the structure of the quiz and the subject being tested
Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 4 & 5");
Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
Console.WriteLine();
//create an array with all the questions and possible answers to display to the user
var questions = new string { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros?",
"Question 2: Multiple choice – Add the following binary numbers: 1011+0011. "
+"Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032.",
"Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. "
+"Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001.",
"Question 4: True or False – Binary numbers are base-10? ",
"Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. "
+"Answers: 1) 1010, 2) 1001, 3) 1000, 4) 0100.",
"Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. "
+"Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101.",
"Question 7: True or False – Binary numbers are base-2? ",
"Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? "
+"Answers: 1) 11, 2) 22, 3) 14, 4) 7.",
"Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? "
+"Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110.",
"Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? "};
//creating array of correct answers, input variables for each question to accept user input in an array, and question status array to track if the question was answered correctly the first time
var questionAnswer = new string { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };
var questionInput = new string[10];
var questionStatus = new string { "n", "n", "n", "n", "n", "n", "n", "n", "n", "n" };
//define the question types (tf or mc) in an array, set the question iteration globally, and set the question index globally
var questionType = new string { "tf", "mc", "mc", "tf", "mc", "mc", "tf", "mc", "mc", "tf" };
var questionIteration = 0;
var questionIndex = 0;
//defines and sets the true/false counter and multiple choice counters to zero
var trueFalseCount = 0; var multipleChoiceCount = 0;
//start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
for (questionIteration = 1; questionIteration < 3; questionIteration++)//set up a for loop to run 2 iterations of the questions
{
questionIndex = 0;
if (questionIteration == 2)
{
Console.WriteLine("Second attempt. Previously incorrectly answered questions will be displayed again. Good luck!");
}
//foreach loop to handle each question and the answer input by the user, plus logic to test for invalid and correct/incorrect answers
foreach (string question in questions)
{
if (questionStatus[questionIndex] == "n")//first attempt at the question
{
Console.WriteLine(question); //displays the question, starting with question 1
if (questionType[questionIndex] == "tf")
{
Console.WriteLine("Press T or F and Enter to submit");
}
else if (questionType[questionIndex] == "mc")
{
Console.WriteLine("Select the number (1, 2, 3 or 4) of the correct answer and press Enter to submit");
}
if (questionType[questionIndex] == "tf")//logic for true/false questions
{
questionInput[questionIndex] = InvalidTrueFalseEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
else if (questionType[questionIndex] == "mc")//logic for multiple choice questions
{
questionInput[questionIndex] = InvalidMultipleChoiceEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
}
questionIndex++;//increment the question counter for the next iteration
}
}
Console.WriteLine("Test Complete!");//tells user the test is over
Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
//this method will check multiple choice answers to ensure they are a number 1-4
string InvalidMultipleChoiceEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "1" || answer == "2" || answer == "3" || answer == "4")
{
return answer;
}
Console.WriteLine("Invalid answer type (not 1-4), try again.");
}
}
//this method will check true/false answers to ensure input was a "t" or an "f"
string InvalidTrueFalseEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "t" || answer == "f")
{
return answer;
}
Console.WriteLine("Invalid answer type (not t or f), try again.");
}
}
//this method will check the answer to see if it is correct or not, if the entry was valid for the questionType
string CheckForCorrectAnswer()
{
var answer = questionInput[questionIndex];
if (answer != questionAnswer[questionIndex])//tests for incorrect answer given from a valid input by question type
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Sorry, wrong answer. :-(");
Console.WriteLine();
questionStatus[questionIndex] = "n";//make sure the question status reflects "n"
return answer;
}
else //it must be correct, since we've checked against invalid and incorrect answers!
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Correct answer!");
questionStatus[questionIndex] = "y";
if (questionType[questionIndex] == "tf")
{
trueFalseCount++;//increment the true/false counter to total the number answered correctly
}
else
{
multipleChoiceCount++;//increment the multiple choice counter to total the number answered correctly
}
Console.WriteLine();
return answer;
}
}
}
}
}
This week I have a requirement to implement C# OOP principles into my code design and provide my source code; a document with a problem statement and narrative about my approach and experience; the program's design (flow chart and class diagram); and testing, execution and debugging output from my program.
As I try to wrap my head around all these OOP principles, between reading my text, doing practice coding of small projects, and attempting to re-write my code, I've become "stuck" on the best way to do this. Currently, I'm looking at this post
as my "example" to model from and to learn from the responses Coding Yoshi provided the OP in an effort to complete this project.
Any advice on how best to do this, using my existing code as the example of what the Quiz behavior is supposed to be, but implementing OOP? Right now I'm thinking:
Program.cs - class that runs the quiz program;
QuestionsAndAnswers.cs - class with the questions and answers defined;
TestAnswersToDetermineIfCorrect.cs - class that implements the logic to determine is the answers the user inputs are 1. valid entries and 2. correct or not;
CalculateResults.cs - class that calculates number of questions answered correctly;
DisplayResults.cs - class that displays the results of the test at the end.
Does this sound plausible? I'm concerned that because this is such a small program that this will make it more complicated than it is currently. Of course, if this is true and i leave my code "as-is", it defeats the purpose of me using this assignment as a learning tool, so I have to re-write it using OOP regardless. My challenge now is to actually code these classes. All suggestions/help is welcome and appreciated. Thanks in advance.
c# object-oriented quiz
New contributor
$endgroup$
$begingroup$
As the current implementation is not really object oriented, and you are asking specifically how to make it object oriented, this question is essentially asking for code to be written rather than reviewed, and hence off-topic for Code Review.
$endgroup$
– 200_success
3 hours ago
add a comment |
$begingroup$
I asked for help on a C# Quiz program I am writing for my ENGR115 class in this thread: Original Question
Roland replied with some good advice for me, which I followed, and came up with this revised code:
using System;
using System.Threading;
namespace Quiz
{
class Program
{
static void Main(string args)
{
//start the quiz by telling the student the details of the structure of the quiz and the subject being tested
Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 4 & 5");
Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
Console.WriteLine();
//create an array with all the questions and possible answers to display to the user
var questions = new string { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros?",
"Question 2: Multiple choice – Add the following binary numbers: 1011+0011. "
+"Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032.",
"Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. "
+"Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001.",
"Question 4: True or False – Binary numbers are base-10? ",
"Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. "
+"Answers: 1) 1010, 2) 1001, 3) 1000, 4) 0100.",
"Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. "
+"Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101.",
"Question 7: True or False – Binary numbers are base-2? ",
"Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? "
+"Answers: 1) 11, 2) 22, 3) 14, 4) 7.",
"Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? "
+"Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110.",
"Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? "};
//creating array of correct answers, input variables for each question to accept user input in an array, and question status array to track if the question was answered correctly the first time
var questionAnswer = new string { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };
var questionInput = new string[10];
var questionStatus = new string { "n", "n", "n", "n", "n", "n", "n", "n", "n", "n" };
//define the question types (tf or mc) in an array, set the question iteration globally, and set the question index globally
var questionType = new string { "tf", "mc", "mc", "tf", "mc", "mc", "tf", "mc", "mc", "tf" };
var questionIteration = 0;
var questionIndex = 0;
//defines and sets the true/false counter and multiple choice counters to zero
var trueFalseCount = 0; var multipleChoiceCount = 0;
//start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
for (questionIteration = 1; questionIteration < 3; questionIteration++)//set up a for loop to run 2 iterations of the questions
{
questionIndex = 0;
if (questionIteration == 2)
{
Console.WriteLine("Second attempt. Previously incorrectly answered questions will be displayed again. Good luck!");
}
//foreach loop to handle each question and the answer input by the user, plus logic to test for invalid and correct/incorrect answers
foreach (string question in questions)
{
if (questionStatus[questionIndex] == "n")//first attempt at the question
{
Console.WriteLine(question); //displays the question, starting with question 1
if (questionType[questionIndex] == "tf")
{
Console.WriteLine("Press T or F and Enter to submit");
}
else if (questionType[questionIndex] == "mc")
{
Console.WriteLine("Select the number (1, 2, 3 or 4) of the correct answer and press Enter to submit");
}
if (questionType[questionIndex] == "tf")//logic for true/false questions
{
questionInput[questionIndex] = InvalidTrueFalseEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
else if (questionType[questionIndex] == "mc")//logic for multiple choice questions
{
questionInput[questionIndex] = InvalidMultipleChoiceEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
}
questionIndex++;//increment the question counter for the next iteration
}
}
Console.WriteLine("Test Complete!");//tells user the test is over
Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
//this method will check multiple choice answers to ensure they are a number 1-4
string InvalidMultipleChoiceEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "1" || answer == "2" || answer == "3" || answer == "4")
{
return answer;
}
Console.WriteLine("Invalid answer type (not 1-4), try again.");
}
}
//this method will check true/false answers to ensure input was a "t" or an "f"
string InvalidTrueFalseEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "t" || answer == "f")
{
return answer;
}
Console.WriteLine("Invalid answer type (not t or f), try again.");
}
}
//this method will check the answer to see if it is correct or not, if the entry was valid for the questionType
string CheckForCorrectAnswer()
{
var answer = questionInput[questionIndex];
if (answer != questionAnswer[questionIndex])//tests for incorrect answer given from a valid input by question type
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Sorry, wrong answer. :-(");
Console.WriteLine();
questionStatus[questionIndex] = "n";//make sure the question status reflects "n"
return answer;
}
else //it must be correct, since we've checked against invalid and incorrect answers!
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Correct answer!");
questionStatus[questionIndex] = "y";
if (questionType[questionIndex] == "tf")
{
trueFalseCount++;//increment the true/false counter to total the number answered correctly
}
else
{
multipleChoiceCount++;//increment the multiple choice counter to total the number answered correctly
}
Console.WriteLine();
return answer;
}
}
}
}
}
This week I have a requirement to implement C# OOP principles into my code design and provide my source code; a document with a problem statement and narrative about my approach and experience; the program's design (flow chart and class diagram); and testing, execution and debugging output from my program.
As I try to wrap my head around all these OOP principles, between reading my text, doing practice coding of small projects, and attempting to re-write my code, I've become "stuck" on the best way to do this. Currently, I'm looking at this post
as my "example" to model from and to learn from the responses Coding Yoshi provided the OP in an effort to complete this project.
Any advice on how best to do this, using my existing code as the example of what the Quiz behavior is supposed to be, but implementing OOP? Right now I'm thinking:
Program.cs - class that runs the quiz program;
QuestionsAndAnswers.cs - class with the questions and answers defined;
TestAnswersToDetermineIfCorrect.cs - class that implements the logic to determine is the answers the user inputs are 1. valid entries and 2. correct or not;
CalculateResults.cs - class that calculates number of questions answered correctly;
DisplayResults.cs - class that displays the results of the test at the end.
Does this sound plausible? I'm concerned that because this is such a small program that this will make it more complicated than it is currently. Of course, if this is true and i leave my code "as-is", it defeats the purpose of me using this assignment as a learning tool, so I have to re-write it using OOP regardless. My challenge now is to actually code these classes. All suggestions/help is welcome and appreciated. Thanks in advance.
c# object-oriented quiz
New contributor
$endgroup$
I asked for help on a C# Quiz program I am writing for my ENGR115 class in this thread: Original Question
Roland replied with some good advice for me, which I followed, and came up with this revised code:
using System;
using System.Threading;
namespace Quiz
{
class Program
{
static void Main(string args)
{
//start the quiz by telling the student the details of the structure of the quiz and the subject being tested
Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 4 & 5");
Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
Console.WriteLine();
//create an array with all the questions and possible answers to display to the user
var questions = new string { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros?",
"Question 2: Multiple choice – Add the following binary numbers: 1011+0011. "
+"Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032.",
"Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. "
+"Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001.",
"Question 4: True or False – Binary numbers are base-10? ",
"Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. "
+"Answers: 1) 1010, 2) 1001, 3) 1000, 4) 0100.",
"Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. "
+"Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101.",
"Question 7: True or False – Binary numbers are base-2? ",
"Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? "
+"Answers: 1) 11, 2) 22, 3) 14, 4) 7.",
"Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? "
+"Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110.",
"Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? "};
//creating array of correct answers, input variables for each question to accept user input in an array, and question status array to track if the question was answered correctly the first time
var questionAnswer = new string { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };
var questionInput = new string[10];
var questionStatus = new string { "n", "n", "n", "n", "n", "n", "n", "n", "n", "n" };
//define the question types (tf or mc) in an array, set the question iteration globally, and set the question index globally
var questionType = new string { "tf", "mc", "mc", "tf", "mc", "mc", "tf", "mc", "mc", "tf" };
var questionIteration = 0;
var questionIndex = 0;
//defines and sets the true/false counter and multiple choice counters to zero
var trueFalseCount = 0; var multipleChoiceCount = 0;
//start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
for (questionIteration = 1; questionIteration < 3; questionIteration++)//set up a for loop to run 2 iterations of the questions
{
questionIndex = 0;
if (questionIteration == 2)
{
Console.WriteLine("Second attempt. Previously incorrectly answered questions will be displayed again. Good luck!");
}
//foreach loop to handle each question and the answer input by the user, plus logic to test for invalid and correct/incorrect answers
foreach (string question in questions)
{
if (questionStatus[questionIndex] == "n")//first attempt at the question
{
Console.WriteLine(question); //displays the question, starting with question 1
if (questionType[questionIndex] == "tf")
{
Console.WriteLine("Press T or F and Enter to submit");
}
else if (questionType[questionIndex] == "mc")
{
Console.WriteLine("Select the number (1, 2, 3 or 4) of the correct answer and press Enter to submit");
}
if (questionType[questionIndex] == "tf")//logic for true/false questions
{
questionInput[questionIndex] = InvalidTrueFalseEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
else if (questionType[questionIndex] == "mc")//logic for multiple choice questions
{
questionInput[questionIndex] = InvalidMultipleChoiceEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
questionInput[questionIndex] = CheckForCorrectAnswer();//checks for correct answer
}
}
questionIndex++;//increment the question counter for the next iteration
}
}
Console.WriteLine("Test Complete!");//tells user the test is over
Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
//this method will check multiple choice answers to ensure they are a number 1-4
string InvalidMultipleChoiceEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "1" || answer == "2" || answer == "3" || answer == "4")
{
return answer;
}
Console.WriteLine("Invalid answer type (not 1-4), try again.");
}
}
//this method will check true/false answers to ensure input was a "t" or an "f"
string InvalidTrueFalseEntry()
{
while (true)
{
var answer = Console.ReadLine();
if (answer == "t" || answer == "f")
{
return answer;
}
Console.WriteLine("Invalid answer type (not t or f), try again.");
}
}
//this method will check the answer to see if it is correct or not, if the entry was valid for the questionType
string CheckForCorrectAnswer()
{
var answer = questionInput[questionIndex];
if (answer != questionAnswer[questionIndex])//tests for incorrect answer given from a valid input by question type
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Sorry, wrong answer. :-(");
Console.WriteLine();
questionStatus[questionIndex] = "n";//make sure the question status reflects "n"
return answer;
}
else //it must be correct, since we've checked against invalid and incorrect answers!
{
Console.WriteLine("Your answer was: " + questionInput[questionIndex]); //displays the answer chosen by the user
Console.WriteLine("Correct answer!");
questionStatus[questionIndex] = "y";
if (questionType[questionIndex] == "tf")
{
trueFalseCount++;//increment the true/false counter to total the number answered correctly
}
else
{
multipleChoiceCount++;//increment the multiple choice counter to total the number answered correctly
}
Console.WriteLine();
return answer;
}
}
}
}
}
This week I have a requirement to implement C# OOP principles into my code design and provide my source code; a document with a problem statement and narrative about my approach and experience; the program's design (flow chart and class diagram); and testing, execution and debugging output from my program.
As I try to wrap my head around all these OOP principles, between reading my text, doing practice coding of small projects, and attempting to re-write my code, I've become "stuck" on the best way to do this. Currently, I'm looking at this post
as my "example" to model from and to learn from the responses Coding Yoshi provided the OP in an effort to complete this project.
Any advice on how best to do this, using my existing code as the example of what the Quiz behavior is supposed to be, but implementing OOP? Right now I'm thinking:
Program.cs - class that runs the quiz program;
QuestionsAndAnswers.cs - class with the questions and answers defined;
TestAnswersToDetermineIfCorrect.cs - class that implements the logic to determine is the answers the user inputs are 1. valid entries and 2. correct or not;
CalculateResults.cs - class that calculates number of questions answered correctly;
DisplayResults.cs - class that displays the results of the test at the end.
Does this sound plausible? I'm concerned that because this is such a small program that this will make it more complicated than it is currently. Of course, if this is true and i leave my code "as-is", it defeats the purpose of me using this assignment as a learning tool, so I have to re-write it using OOP regardless. My challenge now is to actually code these classes. All suggestions/help is welcome and appreciated. Thanks in advance.
c# object-oriented quiz
c# object-oriented quiz
New contributor
New contributor
New contributor
asked 5 hours ago
MX372MX372
222
222
New contributor
New contributor
$begingroup$
As the current implementation is not really object oriented, and you are asking specifically how to make it object oriented, this question is essentially asking for code to be written rather than reviewed, and hence off-topic for Code Review.
$endgroup$
– 200_success
3 hours ago
add a comment |
$begingroup$
As the current implementation is not really object oriented, and you are asking specifically how to make it object oriented, this question is essentially asking for code to be written rather than reviewed, and hence off-topic for Code Review.
$endgroup$
– 200_success
3 hours ago
$begingroup$
As the current implementation is not really object oriented, and you are asking specifically how to make it object oriented, this question is essentially asking for code to be written rather than reviewed, and hence off-topic for Code Review.
$endgroup$
– 200_success
3 hours ago
$begingroup$
As the current implementation is not really object oriented, and you are asking specifically how to make it object oriented, this question is essentially asking for code to be written rather than reviewed, and hence off-topic for Code Review.
$endgroup$
– 200_success
3 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
MX372 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213419%2fupdated-quiz-code-how-to-imlement-c-oop-classes-to-accomplish-the-same-thing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
MX372 is a new contributor. Be nice, and check out our Code of Conduct.
MX372 is a new contributor. Be nice, and check out our Code of Conduct.
MX372 is a new contributor. Be nice, and check out our Code of Conduct.
MX372 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f213419%2fupdated-quiz-code-how-to-imlement-c-oop-classes-to-accomplish-the-same-thing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
As the current implementation is not really object oriented, and you are asking specifically how to make it object oriented, this question is essentially asking for code to be written rather than reviewed, and hence off-topic for Code Review.
$endgroup$
– 200_success
3 hours ago