Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exceptions and Advanced File I/O Programming Challenges 1, TestScores Class Writ

ID: 3548736 • Letter: E

Question


Exceptions and Advanced File I/O


Programming Challenges



1, TestScores Class

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




2, TestScores Class Custom Exception

Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Programming Challenges 1 so that it throws an InvalidTestScores exception if any of the test scores in the array are invalid.

Explanation / Answer

// PROGRAM 1
//TestScores Class
class TestScores
{
int[] score_array;
// The class constructor should accept an array of test scores as its argument .
public TestScores(int[] array) throws IllegalArgumentException
{
score_array = new int[array.length];
for(int i=0; i<array.length; i++)
{
//. If any test score in the array is negative or greater than 100,
// the class should throw an IllegalArgumentException.
if(array[i]<0 || array[i]>100)
throw new IllegalArgumentException("Invalid test Score ");
else
score_array[i] = array[i];
}
}
//The class should have a method that returns the average of the scores
public double getAverage()
{
double total = 0;
for(int i=0; i<score_array.length; i++)
total = total+score_array[i];
return total/score_array.length;
}
}
// demonstrate the class in a program.
public class TestScoresMain
{
public static void main(String[] args)
{
int array[] = {89,90,91,92};
// test case 1. all scores are good.
TestScores TS1 = new TestScores(array);
// here no exception will be thrown or caught because all scores are good.
System.out.println("Test Score Average Given by :"+TS1.getAverage());
int array1[] = {89,90,91,-92};
// test case 2.
// here one score is invalid and it is less than 0 so
// it should throw InvalidTestScore Exception.
try
{
TestScores TS2 = new TestScores(array1);
}
// one InvalidTestScore Exception thrown catch and display message.
catch(IllegalArgumentException ep)
{
System.out.println("Exception thrown :" + ep);
}
}
}

// PROGRAM 2
//Write an exception class named InvalidTestScore.
class InvalidTestScore extends Exception
{
public InvalidTestScore(String message)
{
super(message);
}
}

class TestScores
{
int[] score_array;
// below constructor throws InvalidTestScore Exception if any score is invalid.
public TestScores(int[] array) throws InvalidTestScore
{
score_array = new int[array.length];
for(int i=0; i<array.length; i++)
{
// check any score is less than 0 or > 100
// if so throw InvalidTestScore Exception.
if(array[i]<0 || array[i]>100)
throw new InvalidTestScore("Invalid test Score ");
else
score_array[i] = array[i];
}
}
//The class should have a method that returns the average of the scores
public double getAverage()
{
double total = 0;
for(int i=0; i<score_array.length; i++)
total = total+score_array[i];
return total/score_array.length;
}
}



public class TestScoresMain
{
public static void main(String[] args)
{
// test case 1. all scores are good.
int array[] = {89,90,91,92};
try
{
TestScores TS1 = new TestScores(array);
System.out.println("Test Score Average Given by :"+TS1.getAverage());
}
// here no exception will be thrown or caught because all scores are good.
catch(InvalidTestScore ep)
{
System.out.println("Exception thrown :" + ep);
}
int array1[] = {89,90,91,-92};
// test case 2.
// here one score is invalid and it is less than 0 so
// it should throw InvalidTestScore Exception.
try
{
TestScores TS2 = new TestScores(array1);
}
// one InvalidTestScore Exception thrown catch and display message.
catch(InvalidTestScore ep)
{
System.out.println("Exception thrown :" + ep);
}
}
}