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 TestScores. The class constructor should accept an array of test scores as...
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. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
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...
1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the...
1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the Rectangles takes an array of Rectangle objects. You can assume the array is filled Provide these methods An averageArea method that returns the average area of the Rectangle objects in the array. Only divide one time A method swapMaxAndMin which swaps the Rectangle with the largest area with the one with the smallest area in the array. Only use one loop A method toString...
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...
The following code is for a class named Box. The class Box includes a constructor method...
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume(). For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT