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...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
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...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) 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. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT