Ask Question

Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Part I so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid. Test your exception in a program (in a subclass located in the same file). Your program should prompt the user to enter the number of test scores, and then ask for each test score individually. Then, it should print the average of test scores. If the average method throws an InvalidTestScore exception, the main method should catch it and print "Invalid test score."

+2
Answers (1)
  1. 24 February, 17:30
    0
    See Explaination

    Explanation:

    package testscores;

    import java. util. ArrayList;

    import java. util. List;

    public class TestScores {

    public List scorearray=new ArrayList ();

    public TestScores (List scores) throws InvalidTestScore{

    this. scorearray=scores;

    for (int i=0; i
    if (scorearray. get (i) >100 || scorearray. get (i) <0) {

    throw new InvalidTestScore (this. scorearray. get (i));

    }

    }

    }

    public double average () {

    int tot=0;

    for (int i=0; i
    tot=tot+this. scorearray. get (i);

    }

    return tot * (1.0) / (this. scorearray. size ());

    }

    class InvalidTestScore extends Exception

    {

    private double amount;

    public InvalidTestScore (int Score)

    {

    System. out. println ("Invalid Score "+Score);

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Part I so that it throws an InvalidTestScore ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers