First and foremost this does require 3 files types. I would need a .h and a .cpp
ID: 3685963 • Letter: F
Question
First and foremost this does require 3 files types. I would need a .h and a .cpp as well as a tester (which will be provided). The tester file was provided by my teacher so only make necessary changes to it. Here is what my teacher said we need.
"The class should be implemented in 2 files - a .cpp and a .h file, as follows:
TestGrader.h file which contains the class definition
TestGrader.cpp which contains the class implementation file
You will need another cpp file to test the class which contains the int main function and instantiates a class variable. Use my tester code.
Therefore, you will create a solution with 3 files in it."
Driver’s License Exam The State Department of Motor Vehicles (DMV) has asked you to write a program that grades the written portion of the driver’s license exam, which has 20 multiple choice questions. Here are the correct answers:
1.B 5.C 9.C 13.D 17.C 2.D 6.A 10.D 14.A 18.B 3.A 7.B 11.B 15.D 19.D 4.A 8.A 12.C 16.C 20.A
To do this you should create a TestGrader class. The class will have a correct_answers array of 20 characters, which holds the correct test answers. It will have two public member functions that enable user programs to interact with the class. These are: setCorrect_answers. The setCorrect_answers function receives a 20-character char array holding the correct answers and copies this information into its correct_answers array. grade. The grade function receives a 20-character array holding the test taker’s answers and compares each of their answers to the answers held in the correct_answers array.An applicant must correctly answer 15 or more of the 20 questions to pass the exam. The grade function should print out the following information: User answers and the correct answer for each question An "X" across the questions that the user answered incorrectly a message indicating whether the applicant passed or failed the exam the number of right answers and the number of wrong answers The client program that creates and uses a TestGrader object should rst make a single call to setCorrect_answers, passing it a char array containing the 20 correct answers. Once this is done it should allow a test taker’s 20 answers to be entered in a 20-character array. Then it should call the grade function to grade the exam to display the results of the grading process. (Here is the tester)
// Chapter 8 - Programming Challenge 11, Driver's License Exam
// This program utilizes a TestGrader class to grade the written portion of a
// driver's license exam. The class grade function compares the applicant's
// answers, stored in one array, to the correct answers, stored in another array.
#include iostream
#include string
#include iomanip
using namespace std;
#include "TestGrader.h"
// ***************** Class tester ***************
int main()
{
const int NUM_QUESTIONS = 20;
const int MIN_CORRECT = 15;
// Create a TestGrader object & set its key with correct answers
char correct_ans[] = { 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A' };
// Array to hold test taker's answers
char user1_ans[20] = { 'A', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'A', 'C', 'D', 'A', 'D',
'A', 'C', 'B', 'D', 'A' };
char user2_ans[20] = { 'A', 'D', 'D', 'A', 'C',
'A', 'B', 'D', 'C', 'D',
'A', 'C', 'D', 'C', 'C',
'A', 'A', 'B', 'D', 'A' };
TestGrader DMVexam;
DMVexam.setCorrect_answers(correct_ans);
//------------- Grade grade User 1's answers ---------
cout << "------------ User # 1 ------------- ";
DMVexam.grade(user1_ans);
//------------- Grade grade User 2's answers ---------
cout << " ------------ User # 2 ------------- ";
DMVexam.grade(user2_ans);
system("pause");
return 0;
}
Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include <cctype>
#include "TestGrader.h"
using namespace std;
int main()
{
TestGrader Exam;
char Test[20];
char Ans;
Exam.setkey("BDAACABACDBCDADCCBDA");
cout << "Enter A, B, C or D to answer each question on the test. ";
for (int i = 0; i < 20; i++)
{
do
{
cout << " " << (i + 1) << "). ";
cin >> Ans;
Ans = toupper(Ans);
if (Ans < 'A' || Ans > 'D')
{
cout << " Error! Invalid Input ";
cout << " Enter A, B, C or D as your answer. ";
}
} while (Ans < 'A' || Ans > 'D');
Test[i] = Ans;
}
Exam.grade(Test);
return 0;
}
TestGrader.cpp
// TestGrader.cpp -- TestGrader implementation file.
#include <iostream>
#include <string>
#include <cctype>
#include "TestGrader.h"
using namespace std;
/*******************************************************
* setKey *
* This member function receives a 20-character string *
* and copies its information into the answers array. *
*******************************************************/
void TestGrader::setkey(string str)
{
for (int i = 0; i < 20; i++)
{
answers[i] = str[i];
}
}
/*******************************************************
* grade *
* The grade function receives a 20-character array *
* holding the test taker's answers and compares each *
* of their answers to the correct one. The function *
* then calculates and displays the results. *
*******************************************************/
void TestGrader::grade(char taker[])
{
int correct = 0;
for (int i = 0; i < 20; i++)
{
if (toupper(taker[i]) == answers[i])
correct++;
}
cout << " Applicant Test Results ";
cout << "--------------------------- ";
cout << "Pass / Fail: ";
if(correct >= 15)
cout << "Passed. ";
else
cout << "Failed. ";
cout << "Total number of correctly answered questions: " << correct << endl;
cout << "Total number of incorrectly answered questions: "
<< (20 - correct) << endl;
cout << "Question numbers for all incorrectly answered questions: ";
for (int i = 0; i < 20; ++i)
{
if (toupper(taker[i]) != answers[i])
{
cout << (i + 1) << " ";
}
}
cout << endl;
}
TestGrader.h
// TestGrader.h -- TestGrader class specification file.
#ifndef TESTGRADER_H
#define TESTGRADER_H
#include <string>
using namespace std;
class TestGrader
{
private:
char answers[20];
public:
void setkey(string);
void grade(char[]);
};
#endif
SAMPLE OUTPUT
Enter A, B, C or D to answer each question on the test.
1). A
2). B
3). D
4). E
Error! Invalid Input
Enter A, B, C or D as your answer.
4). g++ -std=c++11 -o main *.cpp
Error! Invalid Input
Enter A, B, C or D as your answer.
4). Error! Invalid Input
Enter A, B, C or D as your answer.
4). Error! Invalid Input
Enter A, B, C or D as your answer.
4). Error! Invalid Input
Enter A, B, C or D as your answer.
Enter A, B, C or D as your answer.
14). Error! Invalid Input
Enter A, B, C or D as your answer.
14). Error! Invalid Input
Enter A, B, C or D as your answer.
14). Error! Invalid Input
Enter A, B, C or D as your answer.
14). Error! Invalid Input
Enter A, B, C or D as your answer.
14). Error! Invalid Input
Enter A, B, C or D as your answer.
14). Error! Invalid Input
Enter A, B, C or D as your answer.
14). 15). Error! Invalid Input
Enter A, B, C or D as your answer.
15). Error! Invalid Input
16). Error! Invalid Input
Enter A, B, C or D as your answer.
16). Error! Invalid Input
Enter A, B, C or D as your answer.
16). Error! Invalid Input
Enter A, B, C or D as your answer.
16). Error! Invalid Input
Enter A, B, C or D as your answer.
16). 17). Error! Invalid Input
Enter A, B, C or D as your answer.
17). 18). Error! Invalid Input
Enter A, B, C or D as your answer.
18). Error! Invalid Input
Enter A, B, C or D as your answer.
18). Error! Invalid Input
Enter A, B, C or D as your answer.
Enter A, B, C or D as your answer.
19). Error! Invalid Input
Enter A, B, C or D as your answer.
19). Error! Invalid Input
Enter A, B, C or D as your answer.
19). Error! Invalid Input
Enter A, B, C or D as your answer.
19). 20). Error! Invalid Input
Enter A, B, C or D as your answer.
20). Error! Invalid Input
Enter A, B, C or D as your answer.
20). g++ -std=c++11 -o main *.cpp
Error! Invalid Input
Enter A, B, C or D as your answer.
20). Error! Invalid Input
Applicant Test Results
---------------------------
Pass / Fail: Failed.
Total number of correctly answered questions: 5
Total number of incorrectly answered questions: 15
Question numbers for all incorrectly answered questions:
1 2 3 4 7 8 10 11 12 13 15 16 18 19 20
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.