Sunday, January 28, 2018

Programming Challenge 15.6 - Essay Class

Example Files: EssayClass.7z

Notice: The GradedActivity.h and GradedActivity.cpp files are taken from the book and are provided as is.

GradedActivity.h


#ifndef GRADED_ACTIVITY_H_
#define GRADED_ACTIVITY_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 function
        double getScore() const
        { return score; }

        char getLetterGrade() const;
};

#endif

GradedActivity.cpp


#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;
}

Essay.h


#ifndef ESSAY_H_
#define ESSAY_H_

#include "GradedActivity.h"

// Max amount of achievable points
const int GRAMMAR = 30;
const int SPELLING = 20;
const int CORRECT_LENGTH = 20;
const int CONTENT = 30;

// Essay class derived from the GradedActivity class
class Essay : public GradedActivity
{
    private:
        double essayScore;            // The score a student has achieved
        int     grammar;                // Grammar score
        int    spelling;                // Spelling score
        int     length;                    // Correct length score
        int     content;                // Content score

    public:
        // Constructor
        Essay() : GradedActivity()
        {
            essayScore = 0.0;
            grammar = 0;
            spelling = 0;
            length = 0;
            content = 0;
        }

        // Parameterized constructor
        Essay(int gr, int sp, int len, int cont) : GradedActivity()
        {
            set(gr, sp, len, cont);
        }

        // Mutator functions
        void set(int, int, int, int);
        void addScore();

        // Accessor functions
        int getGrammarScore() const
        { return grammar; }

        int getSpellingscore() const
        { return spelling; }

        int getCorrectLengthScore() const
        { return length; }

        int getContentScore() const
        { return content; }
};

#endif

Essay.cpp


#include "Essay.h"

/* **********************************************************
            Essay::set() : int, int, int, int
    This function assigns the scores achieved by a student to
    the appropriate member variables.
   ********************************************************** */

void Essay::set(int gr, int sp, int len, int cont)
{
    grammar = gr;
    spelling = sp;
    length = len;
    content = cont;
}

/* **********************************************************
            Essay::addScore()
    This function calculates the total score achieved, then
    calls the inherited setScore() function to set the numeric
    score.
   ********************************************************** */

void Essay::addScore()
{
    essayScore = grammar + spelling + length + content;

    setScore(essayScore);
}

EssayDm.cpp


#include "Essay.h"

#include <iomanip>
using std::right;
using std::setw;

#include <iostream>
using std::cin;
using std::cout;

void getScores(int &, int &, int &, int &);
void displayGrade(const Essay &);

int main()
{
    int gr = 0;
    int sp = 0;
    int len = 0;
    int cont = 0;

    cout << "YAMAGATA 4th JUNIOR HIGH SCHOOL ESSAY GRADER\n\n";

    // Get the scores
    getScores(gr, sp, len, cont);

    // Create an Essay score object
    Essay essay(gr, sp, len, cont);
    essay.addScore();
   
    displayGrade(essay);

    cin.get();
    cin.ignore();
    return 0;
}

/* **********************************************************
            getScores() : int &, int &, int &, int &
    This function asks the teacher to input scores for the
    four categories that make up an essay's total score. The
    input is validated.
   ********************************************************** */

void getScores(int &gr, int &sp, int &len, int &cont)
{
    cout << "Enter the score for the following items:\n\n";
    cout << "Grammar (Max " << GRAMMAR << " points): ";
    cin >> gr;

    while (gr < 0 || gr > GRAMMAR)
    {
        cout << "Grammar: ";
        cin >> gr;
    }

    cout << "Spelling (Max " << SPELLING << " points): ";
    cin >> sp;

    while (sp < 0 || sp > SPELLING)
    {
        cout << "Spelling: ";
        cin >> sp;
    }

    cout << "Correct Length (Max " << SPELLING << " points): ";
    cin >> len;

    while (len < 0 || len > CORRECT_LENGTH)
    {
        cout << "Correct Length: ";
        cin >> len;
    }

    cout << "Content: (Max " << CONTENT << " points): ";
    cin >> cont;

    while (cont < 0 || cont > CONTENT)
    {
        cout << "Content: ";
        cin >> cont;
    }
}

/* **********************************************************
            displayGrade() : const obj &
    This function displays the scores achieved in each
    category, the total points and the letter grade.
   ********************************************************** */

void displayGrade(const Essay &essay)
{
    cout << "\n\nESSAY GRADER - SUMMARY\n\n";
    cout << "Essay Title: " << setw(10)
          << "ERASE BAD MEMORIES - KEEP GOOD ONES\n\n";

    cout << "GRAMMAR" << setw(14) << "SPELLING" << setw(12)
          << "LENGTH" << setw(14) << "CONTENT\n";
    cout << setw(7) << right << essay.getGrammarScore()
          << setw(14) << right << essay.getSpellingscore()
          << setw(12) << right << essay.getCorrectLengthScore()
          << setw(13) << right << essay.getContentScore() << "\n\n";

    cout << "Total  Score: " << essay.getScore() << "/100\n";
    cout << "Letter Grade: " << essay.getLetterGrade();
   
}

Example Output:



No comments:

Post a Comment