Question

--C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h"...

--C++--
--spc16-7.cpp--

// Chapter 16, Programming Challenge 7: TestScores class
#include <iostream>
#include "TestScores.h"
#include "NegativeScore.h"
using namespace std;

int main()
{
// Constant for the number of test scores
const int NUM_SCORES = 5;
  
   try
   {
       // Create an array of valid scores.
       int myScores[NUM_SCORES] = { 88, 90, 93, 87, 99 };

       // Create a TestScores object.
       //TestScores myTestScores(myScores, NUM_SCORES); // optional constructor
       TestScores myTestScores(myScores);

       // Display the average.
       cout << "The average score is ";
       cout << myTestScores.getAverage() << endl;

       // Create an array with an invalid score.
       int badScores[NUM_SCORES] = { 88, 90, -99, 87, 99 };

       // Create a TestScores object.
       //TestScores badTestScores(badScores, NUM_SCORES); // optional constructor
TestScores badTestScores(badScores);

       // Display the average.
       cout << "The average score is ";
       cout << badTestScores.getAverage() << endl;
   }
   catch(NegativeScore e)
   {
       cout << "An invalid score was found. The "
           << "invalid score is ";
// display invalid score from exception
   }

   return 0;
}


--NegativeScore.h--

// version 2
#ifndef NEGATIVE_SCORE_H
#define NEGATIVE_SCORE_H

class NegativeScore
{
private:
   // Variable to hold the bad score
z
public:
   // Constructor


   // getScore function


};

#endif



--TestScores.h--

// version 2
#ifndef TEST_SCORES_H
#define TEST_SCORES_H
#include <iostream>
#include "NegativeScore.h"
using namespace std;

// Constant for the number of test scores (used with 1 parameter version)
const int NUM_SCORES = 5;

class TestScores
{
private:
   // Pointer to array of scores
   // Number of scores (optional - used with 2 parameter version)

public:
   // Default constructor
// Initialize variable to NULL


   // Constructor (1 parameter)
   // Allocate memory for the array.
   // Copy the array (throw an error on negative value).
  
  
// Optional Constructor (2 parameters)
   // Assign number of scores from parameter.
   // Allocate memory for the array.
   // Copy the array (throw an error on negative value).


   // getAverage function

  
// Destructor
// Delete pointer
  
};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for the above assignment.

Feel free to comment on any doubt and give thumbs up!

Code for NegativeScore.h

#ifndef NEGATIVE_SCORE_H

#define NEGATIVE_SCORE_H

class NegativeScore

{

private:

// Variable to hold the bad score

int score;

public:

// Constructor

NegativeScore(int score)

{

this->score = score;

}

// getScore function

int getScore()

{

return score;

}


};

#endif

Code for TestScores.h

#ifndef TEST_SCORES_H

#define TEST_SCORES_H

#include <iostream>

#include "NegativeScore.h"

using namespace std;

// Constant for the number of test scores (used with 1 parameter version)

const int NUM_SCORES = 5;

class TestScores

{

private:

// Pointer to array of scores

int *arrayOfScore;

// Number of scores (optional - used with 2 parameter version)

int numberOfScores;

public:

// Default constructor

// Initialize variable to NULL

TestScores()

{

arrayOfScore = NULL;

numberOfScores = 0;

}

// Constructor (1 parameter)

// Allocate memory for the array.

// Copy the array (throw an error on negative value).

TestScores(int myScores[])

{

numberOfScores = NUM_SCORES;

arrayOfScore = new int[NUM_SCORES];

for(int i = 0 ; i < NUM_SCORES ; ++i)

{

if(myScores[i] < 0){

throw NegativeScore(myScores[i]);

}

arrayOfScore[i] = myScores[i];

}

}

// Optional Constructor (2 parameters)

// Assign number of scores from parameter.

// Allocate memory for the array.

// Copy the array (throw an error on negative value).

TestScores(int myScores[] , int size)

{

numberOfScores = size;

arrayOfScore = new int[size];

for(int i = 0 ; i < numberOfScores ; ++i)

{

if(myScores[i] < 0){

throw myScores[i];

}

arrayOfScore[i] = myScores[i];

}

}

// getAverage function

double getAverage()

{

double sum = 0.0;

for(int i = 0 ; i < numberOfScores ; ++i)

{

sum += arrayOfScore[i];

}

return sum/numberOfScores;

}

// Destructor

// Delete pointer

~TestScores()

{

delete[] arrayOfScore;

}

};

#endif

Code for Main.cpp (which contains the main method)

#include <iostream>

#include "TestScores.h"

#include "NegativeScore.h"

using namespace std;

int main()

{

// Constant for the number of test scores

const int NUM_SCORES = 5;

try

{

// Create an array of valid scores.

int myScores[NUM_SCORES] = {88, 90, 93, 87, 99};

// Create a TestScores object.

//TestScores myTestScores(myScores, NUM_SCORES); // optional constructor

TestScores myTestScores(myScores);

// Display the average.

cout << "The average score is ";

cout << myTestScores.getAverage() << endl;

// Create an array with an invalid score.

int badScores[NUM_SCORES] = {88, 90, -99, 87, 99};

// Create a TestScores object.

//TestScores badTestScores(badScores, NUM_SCORES); // optional constructor

TestScores badTestScores(badScores);

// Display the average.

cout << "The average score is ";

cout << badTestScores.getAverage() << endl;

}

catch (NegativeScore e)

{

cout << "An invalid score was found. The "

<< "invalid score is ";

// display invalid score from exception

cout << e.getScore() << endl;

}

return 0;

}

Screenshot of test run

Thank You!

Add a comment
Know the answer?
Add Answer to:
--C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h"...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores...

    #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores { private: string studentName; // The student's name double *testScores; // Points to array of test scores int numTestScores; // Number of test scores // Private member function to create an // array of test scores. void createTestScoresArray(int size) { numTestScores = size; testScores = new double[size]; for (int i = 0; i < size; i++) testScores[i] = DEFAULT_SCORE; } public: // Constructor StudentTestScores(string...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

  • C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; }...

    C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; } bool checkinventoryprice(float price) {    return price > 0; } void endProgram() {    system("pause");    exit(0); } void errorID(string str) {    cout << "Invalid Id!!! " << str << " should be greater than 0" << endl; } void errorPrice(string str) {    cout << "Invalid Price!!!" << str << " should be greater than 0" << endl; } int inputId() {...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
Active Questions
ADVERTISEMENT