Question

In: Computer Science

In C++ Write a class named TestScores. The class constructor should accept an array of test...

In C++

Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function 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 exception. Demonstrate the class in program.

Solutions

Expert Solution

I have written the program using C++PROGRAMMING LANGUAGE.

OUTPUT :

input : 86, 11, 11, 39, 43, 56, 65, 77, 99 , 11

input : 86, -11, 11, 39, 143, 56, 65, -77, 99 , 11

CODE :

//included the required libraries

#include <iostream>

#include <exception>

using namespace std;

//exception class

class myexception: public exception

{

virtual const char* what() const throw()

{

return "Array consists of negative scores or scores which is more than 100!!!";//returning statement when exception comes to the console

}

} myex;

//class TestScores

class TestScores

{

public:

int *scores;

//Constructor

TestScores(int arr[])

{

scores = arr;//input array storing in scores pointer

}

double average();//function declaration

};

//function definition for average

double TestScores::average()

{

int average = 0;//declared average variable to zero

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

{

//condition to check whether the score is negative or more than 100

//cout << scores[i]<<endl;

if(scores[i]>=0 && scores[i]<=100){

average+=scores[i];

}else{

average = 0;

throw myex;//throwing exception

}

}

return (average/10.0);//return average

}

//main method

int main() {

//initialized the scores integer array variable

int Scores[10] = {86, 11, 11, 39, 43, 56, 65, 77, 99 , 11};

TestScores TestScores_object(Scores);//TestScores object

//Calling average method and printing the result on the console

cout << "Average Score is : "<< TestScores_object.average();

}

Thanks..


Related Solutions

Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
How do you write the constructor for the three private fields? public class Student implements Named...
How do you write the constructor for the three private fields? public class Student implements Named { private Person asPerson; private String major; private String universityName; @Override public String name() { return asPerson.name(); }
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
This assignment will test your knowledge and skills in C++. Create a class named employee and...
This assignment will test your knowledge and skills in C++. Create a class named employee and have the data members of: Name ID Salary Create a class named manager that inherits the employee class and adds the data members: Managed_Employees (Array of up to 3 employees) Department Create methods to update each data member in the employee class. Create a method to print out the managed employees sorted by their salary in the manager class. (You can use one of...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT