In: Computer Science
Design an Essay class that is derived from the GradedActivity
class:
class GradedActivity{
private:
double score;
public:
GradedActivity()
{score = 0.0;}
GradedActivity(double s)
{score = s;}
void setScore(double s)
{score = s;}
double getScore() const
{return score;}
char getLetterGrade() const;
};
char GradedActivity::getLetterGrade() const{
char letterGrade;
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;
}
The Essay class should determine the grade a student receives on an
essay. The student's essay score can be up to 100, and is made up
of four parts:
The Essay class should have a double member variable for each of
these sections, as well as a mutator that sets the values of these
variables. It should add all of these values to get the student's
total score on an Essay.
Demonstrate your class in a program that prompts the user to input
points received for grammar, spelling, length, and content, and
then prints the numeric and letter grade received by the
student.
I have created a class Essay with 4 double data members which store the values of the points received by the student for grammar,spelling,length and content as gram,spel,length and content respectively. The constructor initializes the values of all the data members to zero. The function init() takes input from the user and checks if the scores in different parts of the essay are below the limits, that is, grammar up to 30 points, spelling up to 20 points, length up to 20 points, and content up to 30 points. If the limits are met then the values are stored in the variables of the object, else a "Invalid values" prompt is generated. totalscore() function returns the total score of the student by adding all the values of gram,spel,length and content.
In the main function, the user is prompted to enter the points received for various parts in the essay. The object 'a' calls the init() function to check and store the inputs. If its valid then the numeric score is displayed and the letter grade is calculated by passing the total score obtained from 'a.totalscore()' into the setScore() function which is called using the GradeActivity class object 'g'. This sets the value of the score variable and then the same object is used to call the getLetterGrade() function which gives us the letter grade of the student. Then we print the letter grade.
If the inputs are not valid then the user will get two messages:
"The entered values are invalid"
"Please enter valid inputs".