Question

Write a class named TestScores. The class constructor should accept an array test scores as its...

  1. Write a class named TestScores. The class constructor should accept an array test scores as its argument. The class should a method that returns the average of the test scores. If any test score is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate this with an array of five items.
  2. This is JAVA program.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

class Driver {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int totalTestScores;
        try {

            System.out.println("Enter total number of Test Scores:");
            totalTestScores = sc.nextInt();
            int[] testScores = new int[totalTestScores];

            System.out.println("Please enter Test Scores:");
            for (int i = 0; i < testScores.length; i++) {
                testScores[i] = sc.nextInt();
            }
            TestScores testScoresObj = new TestScores(testScores);
            System.out.println("Average Test Score is : " + testScoresObj.averageTestScore());

        } catch (IllegalArgumentException e) {
            System.out.println("Test scores must have a value less than 100 and greater than 0.");
        }
    }
}

class TestScores {

    private int testScores[];

    public TestScores(int[] testScores) throws IllegalArgumentException {
        for (int i = 0; i < testScores.length; i++) {
            //If any of input value contain less than zero
            //or greater than 100 throw IllegalArgumentException
            if (testScores[i] < 0 || testScores[i] > 100) {
                throw new IllegalArgumentException();
            }
        }
        //If everything went fine then assign testScores to class testScores variable
        this.testScores = testScores;
    }

    public double averageTestScore() {
        int sum = 0;
        double average;
        for (int i = 0; i < testScores.length; i++) {
            sum = sum + testScores[i];
        }
        average = (double) sum / testScores.length;
        return average;
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a class named TestScores. The class constructor should accept an array test scores as its...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 (create a Driver class in the same file). The program should ask the user to input the number of test scores to be...

  • IN JAVA (ECLIPSE) Design a TestScores class that has fields to hold three test scores. (If...

    IN JAVA (ECLIPSE) Design a TestScores class that has fields to hold three test scores. (If you have already written the TestScores class for programming challenge 8 of chapter 3, you can modify it.) the class constructor should accept three test scores as arguments and assign these arguments to the test score fields. The class should also have accessor methods for the test score fields, a method that returns the average of the test scores, and a method that returns...

  • Write a class named DartSector. The constructor should accept a sector number and position {Single, Double,...

    Write a class named DartSector. The constructor should accept a sector number and position {Single, Double, Treble, Outer & Inner} represented with the integers 1 – 5 respectively. The class should have 3 methods: • getSectorColor – method should return the pocket’s color (as a String) • singleThrow – method should generate 2 random numbers; one in the range (1..20) and another (1..5); and return the score for the throw. • throwThree – method should generate 3 sets of random...

  • Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

    Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for the month (1 = January, 2 February, etc). l An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and if...

  • TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( )...

    TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( ) : int findMin ) int findScore (value: int): bool res(): int Write the constructor method. It fills the array with random number from 0-100. Find below the code to get your started 1. public TestScores () Random rand -new Random); for (int i-0 i<SIZE i+) socresi] -rand.nextInt (101); Finding the maximum value in an array. Write the method findMax. It returns the maximum value...

  • Write a javascript program that asks the user to enter five test scores. The program should...

    Write a javascript program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage—This method should accept five test scores as arguments and return the average of the scores. determineGrade—This method should accept a test score as an argument and return a letter grade for the score,

  • --C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h"...

    --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);...

  • Using Python, Write a class named Scores. This data structure will store a collection of lab...

    Using Python, Write a class named Scores. This data structure will store a collection of lab scores (or programs scores or any other collection of scores). Since numbered assignments generally start with 1 – your first assignment this summer was lab 1 – the user of this class will think of this collection as being a 1-based list of values. All the methods which require a lab number will assume that value is based upon what the user sees, a...

  • Write a program that asks the user to enter five test scores. The program should display...

    Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Design the following functions in the program: calcAverage—This function should accept five test scores as arguments and return the average of the scores. determineGrade—This function should accept a test score as an argument and return a letter grade for the score (as a String), based on the following grading scale: Round the average...

  • (JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...

    (JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT