--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
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!
--C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h"...
#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 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" 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 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: // 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 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;
}
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 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 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...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to be...