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
Java Write a recursive boolean method named isMember. The method should accept two arguments: an int...
Java Write a recursive boolean method named isMember. The method should accept two arguments: an int array and an int value. The method should return true if the value is found in the array, or false if the value is not found in the array. Demonstrate the method in a program that takes input from the user. First, the program should prompt the user to enter the length of the array. Then, it should prompt the user for every member...
Write a class named ScoreBoard containing: An instance variable named score of type integer. A constructor...
Write a class named ScoreBoard containing: An instance variable named score of type integer. A constructor that accepts an integer parameter, whose value is used to initialize the score instance variable. A method named addScores that accepts a variable-length parameter list of int named scores and returns the total score of the score instance variable plus the scores in the variable length paremeter list named scores. A method named bonusPoints that accepts an integer parameter. The value of score is...
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,...
Write an object oriented programming (JAVA) code. Create a class called Circle. The class has an...
Write an object oriented programming (JAVA) code. Create a class called Circle. The class has an attribute radius, which default to 1 by the first no-arg constructor. The class also has another constructor that instantiates the radius to a given value. Provide methods that calculate the circumference and the area of the circle (use Math.PI and Math.pow). Provide set and get methods. The set method should verify that radius is greater than or equal to 0.0 and less than 20.0,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT