Student Course Grades – Complete C++ program
Course Grades
In a course, a teacher gives the following tests and assignments:
• A lab activity that is observed by the teacher and assigned a numeric score.
• A pass/fail exam that has 10 questions, The minimum passing score is 70.
• An essay that is assigned a numeric score.
• A final exam that has 50 questions.
Write a class named CourseGrades. The class should have a member named grades
that is an array of GradedActivity pointers. The grades array should have four elements,
one for each of the assignments previously described. The class should have
the following member functions:
setLab: This function should accept the address of a GradedActivity
object as its argument. This object should already hold the
student’s score for the lab activity. Element 0 of the grades array
should reference this object.
setPassFailExam: This function should accept the address of a PassFailExam
object as its argument. This object should already hold the
student’s score for the pass/fail exam. Element 1 of the grades
array should reference this object.
setEssay: This function should accept the address of an Essay object as its
argument. (See Programming Challenge 6 for the Essay class. use a
GradedActivity object instead.) This object should already
hold the student’s score for the essay. Element 2 of the grades
array should reference th is object.
setPassFailExam; This function should accept the address of a FinalExam object
as its argument. This object should already hold the student’s
score for the final exam. Element 3 of the grades array should
reference this object.
print: This function should display the numeric scores and grades for
each clement in the grades array.
Demonstrate the class in a program.
Answer:
CourseGrades Class
#include <iostream> #include "GradedActivity.h" #include "PassFailExam.h" #include "FinalExam.h" #include "CourseGrades.h" using namespace std; int main() { // Create a GradedActivity object for the lab. GradedActivity lab(80); // Create a PassFailExam object. 50 questions, // 5 missed questions, minimum passing score is 70. PassFailExam pfexam(50, 5, 70); // Create an Essay object and set the different // types of points. Essay essay; essay.setGrammerPoints(25); essay.setSpellingPoints(15); essay.setLengthPoints(20); essay.setContentPoints(30); // Create a FinalExam object. FinalExam finalExam(50, 5); // Create a CourseGrades object. CourseGrades myGrades; // Store the grade items in the // CourseGrades object. myGrades.setLab(&lab); myGrades.setPassFailExam(&pfexam); myGrades.setEssay(&essay); myGrades.setFinalExam(&finalExam); // Print the grade information. myGrades.print(); return 0; }
#ifndef PASSFAILEXAM_H #define PASSFAILEXAM_H #include "PassFailActivity.h" class PassFailExam : public PassFailActivity { private: int numQuestions; // Number of questions double pointsEach; // Points for each question int numMissed; // Number of questions missed public: // Default constructor PassFailExam() : PassFailActivity() { numQuestions = 0; pointsEach = 0.0; numMissed = 0; } // Constructor PassFailExam(int questions, int missed, double mps) : PassFailActivity(mps) { set(questions, missed); } // Mutator function void set(int, int); // Defined in PassFailExam.cpp // Accessor functions double getNumQuestions() const { return numQuestions; } double getPointsEach() const { return pointsEach; } int getNumMissed() const { return numMissed; } }; #endif
#include "PassFailExam.h" //******************************************************** // set function * // The parameters are the number of questions and the * // number of questions missed. * //******************************************************** void PassFailExam::set(int questions, int missed) { double numericScore; // To hold the numeric score // Set the number of questions and number missed. numQuestions = questions; numMissed = missed; // Calculate the points for each question. pointsEach = 100.0 / numQuestions; // Calculate the numeric score for this exam. numericScore = 100.0 - (missed * pointsEach); // Call the inherited setScore function to set // the numeric score. setScore(numericScore); }
#ifndef PASSFAILACTIVITY_H #define PASSFAILACTIVITY_H #include "GradedActivity.h" class PassFailActivity : public GradedActivity { protected: double minPassingScore; // Minimum passing score. public: // Default constructor PassFailActivity() : GradedActivity() { minPassingScore = 0.0; } // Constructor PassFailActivity(double mps) : GradedActivity() { minPassingScore = mps; } // Mutator void setMinPassingScore(double mps) { minPassingScore = mps; } // Accessors double getMinPassingScore() const { return minPassingScore; } char getLetterGrade() const; }; #endif
#include "PassFailActivity.h" //****************************************************** // Member function PassFailActivity::getLetterGrade * // This function returns 'P' if the score is passing, * // otherwise it returns 'F'. * //****************************************************** char PassFailActivity::getLetterGrade() const { char letterGrade; if (score >= minPassingScore) letterGrade = 'P'; else letterGrade = 'F'; return letterGrade; }
GradedActivity class declaration
#ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H class GradedActivity { protected: double score; // To hold the numeric score public: // Default constructor GradedActivity() { score = 0.0; } // Constructor GradedActivity(double s) { score = s; } // Mutator function void setScore(double s) { score = s; } // Accessor functions virtual double getScore() const { return score; } virtual char getLetterGrade() const; }; #endif
#include "GradedActivity.h" //****************************************************** // Member function GradedActivity::getLetterGrade * //****************************************************** char GradedActivity::getLetterGrade() const { char letterGrade; // To hold the letter grade if (score > 89) letterGrade = 'A'; else if (score > 79) letterGrade = 'B'; else if (score > 69) letterGrade = 'C'; else if (score > 59) letterGrade = 'D'; else letterGrade = 'F'; return letterGrade; }
#ifndef FINALEXAM_H #define FINALEXAM_H #include "GradedActivity.h" class FinalExam : public GradedActivity { private: int numQuestions; // Number of questions double pointsEach; // Points for each question int numMissed; // Number of questions missed public: // Default constructor FinalExam() { numQuestions = 0; pointsEach = 0.0; numMissed = 0; } // Constructor FinalExam(int questions, int missed) { set(questions, missed); } // Mutator functions void set(int, int); // Defined in FinalExam.cpp void adjustScore(); // Defined in FinalExam.cpp // Accessor functions double getNumQuestions() const { return numQuestions; } double getPointsEach() const { return pointsEach; } int getNumMissed() const { return numMissed; } }; #endif
#include "FinalExam.h" //******************************************************** // set function * // The parameters are the number of questions and the * // number of questions missed. * //******************************************************** void FinalExam::set(int questions, int missed) { double numericScore; // To hold the numeric score // Set the number of questions and number missed. numQuestions = questions; numMissed = missed; // Calculate the points for each question. pointsEach = 100.0 / numQuestions; // Calculate the numeric score for this exam. numericScore = 100.0 - (missed * pointsEach); // Call the inherited setScore function to set // the numeric score. setScore(numericScore); // Call the adjustScore function to adjust // the score. adjustScore(); } //***************************************************************** // Definition of Test::adjustScore. If score is within 0.5 points * // of the next whole point, it rounds the score up and * // recalculates the letter grade. * //***************************************************************** void FinalExam::adjustScore() { double fraction = score - static_cast<int>(score); if (fraction >= 0.5) { // Adjust the score variable in the GradedActivity class. score += (1.0 - fraction); } }
Specification file for the Essay class
#ifndef ESSAY_H #define ESSAY_H #include "GradedActivity.h" class Essay : public GradedActivity { private: double grammerPoints; // To hold grammer points double spellingPoints; // To hold spelling points double lengthPoints; // To hold length points double contentPoints; // To hold content points public: // Default constructor Essay() { grammerPoints = 0.0; spellingPoints = 0.0; lengthPoints = 0.0; contentPoints = 0.0; } // Mutators void setGrammerPoints(double); void setSpellingPoints(double); void setLengthPoints(double); void setContentPoints(double); // Accessors double getGrammerPoints() const { return grammerPoints; } double getSpellingPoints() const { return spellingPoints; } double getLengthPoints() const { return lengthPoints; } double getContentPoints() const { return contentPoints; } virtual double getScore(); virtual char getLetterGrade(); }; #endif
Implementation file for the Essay class
#include <iostream> #include "Essay.h" using namespace std; //****************************************** // setGrammerPoints member function * //****************************************** void Essay::setGrammerPoints(double p) { // Validate the points. if (p < 0 || p > 30) { // Invalid data cout << "Invalid number of grammar points.\n"; exit(EXIT_FAILURE); } // Assign the points. grammerPoints = p; } //****************************************** // setSpellingPoints member function * //****************************************** void Essay::setSpellingPoints(double p) { // Validate the points. if (p < 0 || p > 20) { // Invalid data cout << "Invalid number of spelling points.\n"; exit(EXIT_FAILURE); } // Assign the points. spellingPoints = p; } //****************************************** // setLengthPoints member function * //****************************************** void Essay::setLengthPoints(double p) { // Validate the points if (p < 0 || p > 20) { // Invalid data cout << "Invalid number of length points.\n"; exit(EXIT_FAILURE); } // Assign the points. lengthPoints = p; } //****************************************** // setContentPoints member function * //****************************************** void Essay::setContentPoints(double p) { // Validate the points. if (p < 0 || p > 30) { // Invalid data cout << "Invalid number of content points.\n"; exit(EXIT_FAILURE); } // Assign the points. contentPoints = p; } //********************************************************* // getScore * //********************************************************* double Essay::getScore() { return grammerPoints + spellingPoints + lengthPoints + contentPoints; } //********************************************************* // getLetterGrade * //********************************************************* char Essay::getLetterGrade() { // Add up all the points. score = getScore(); // Return the letter grade as reported from // the base class function. return GradedActivity::getLetterGrade(); }
Leave a reply