Question

In: Computer Science

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.

// Notice that element 3 contains an invalid score.

double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };

// Another array with test scores.

// All of these scores are good.

double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };

// Create a TestScores object initialized with badScores.

try

{

TestScores tBad = new TestScores(badScores);

// The following statement should not execute.

System.out.println("The average of the bad scores is " +

tBad.getAverage());

}

catch (IllegalArgumentException e)

{

System.out.println("Invalid score found.\n" + e.getMessage());

}

// Create a TestScores object initialized with goodScores.

try

{

TestScores tGood = new TestScores(goodScores);

System.out.println("The average of the good scores is " +

tGood.getAverage());

}

catch (IllegalArgumentException e)

{

System.out.println("Invalid score found.\n" + e.getMessage());

}

}

}

Solutions

Expert Solution

Below is the class TestScores screenshotAnd Below is the output of the program:

And below this i am giving the whole program to be executed with all the required classes, if you face any difficulty executing the program please leave a comment :)


import java.lang.IllegalArgumentException;
class TestScores{
double scores[]; //Scores array to calculate the average if its a valid score array
TestScores(double[] scores) {
for(int i=0; i<scores.length; i++){
try{
if (scores[i]<0)
throw new IllegalArgumentException("score must be a positive number");
else if(scores[i]>100)
throw new IllegalArgumentException("score must be not be greater than 100");
//Storing it after checking the conditions to save memory space by unnecessary assignment of array if not being used
this.scores = scores; //this.scores points to the datamember of the class and scores points to the local array obtained as parameter to the constructor
}catch(IllegalArgumentException e){
throw e; //re throwing the exception which is catched by the catch method in the main function
}
}
}
double getAverage(){
double sum = 0;
for(int i=0; i<this.scores.length; i++){ //this.scores.length gives the size of the array
sum += this.scores[i];
}
return (sum/this.scores.length); //To compute the average we divide the sum with total numbers of elements present in the array
}
}

public class TestScoresDemo {
public static void main(String[] args) {
// An array with test scores.
// Notice that element 3 contains an invalid score.
double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };

// Another array with test scores.
// All of these scores are good.
double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };

// Create a TestScores object initialized with badScores.
try{
TestScores tBad = new TestScores(badScores);
// The following statement should not execute.
System.out.println("The average of the bad scores is " + tBad.getAverage());
}catch (IllegalArgumentException e) {
System.out.println("Invalid score found.\n" + e.getMessage());
}

// Create a TestScores object initialized with goodScores.
try{
TestScores tGood = new TestScores(goodScores);
System.out.println("The average of the good scores is " + tGood.getAverage());
}catch (IllegalArgumentException e) {
System.out.println("Invalid score found.\n" + e.getMessage());
}
}
  
}


Related Solutions

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.
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....
- 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...
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...
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...
Write a program in C++ that declares an array of 100 integers named scores[]. Prompt the...
Write a program in C++ that declares an array of 100 integers named scores[]. Prompt the user for how many scores they want to enter. Then read in the specified number of ints and store them in the array. Then prompt the user for a passing grade. Use a for loop to go trough the array and count how many scores are passing. Print the count of how many passing scores, and also print a double that is the percent...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT