Question

Java... Write a class named TestScores. The class constructor should accept an array of test scores...

Java...

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 counted,
and then each individual test score. It should then make an array of those scores,
create a TestScore object, and print the average of the scores.

If an IllegalArgumentException is thrown, the main method should catch it, print "Test scores must have a value less than 100 and greater than 0." and terminate the program.

Homework Answers

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("Average2 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;
    }
}

Know the answer?
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
Similar Questions
In C++ PART 1: Write a constructor for a class data type named Test that has...
In C++ PART 1: Write a constructor for a class data type named Test that has the following two member variables: double arr*; int size; The constructor should take one argument for the size and then dynamically allocate an array of that size. PART 2: Write a destructor for the Test class.
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students....
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students. Each record contains an id number and three test scores (Note: the test scores are integers). For example, a student record might look like: BK1234 87 72 91 b) Without using an array, read each student’s information, one record at a time and compute the student’s average (as a double). c) Write to an output file, testScoreOut.txt, as a neatly formatted table with column...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
Java: A teacher has five students who have taken four tests. The teacher uses the following...
Java: A teacher has five students who have taken four tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her four test scores: Test Score Letter Grade 90–100 A 80–89 B 70–79 C 60–69 D 0–59 F Write a class that uses a String array (or an ArrayList object) to hold the five students’ names, an array of five characters to hold the five students’ letter...
in java Write a class Battery that models a rechargeable battery. A battery has a constructor...
in java Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The method public void drain(double amount) drains the capacity of the battery by the given amount. The method public void charge() charges the battery to its original capacity. The method public double getRemainingCapacity() gets the remaining capacity of the battery....
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT