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

Challenge 1 Write a class named TestScores . The class constructor should accept

ID: 3718917 • Letter: C

Question

Challenge 1

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. TestScores Class Custom Exception. Write an exception class named InvalidTestScore . Modify the TestScores class so that it throws an InvalidTestScore exception.

Challenge 2

Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Exercise 1 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 Driver class 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 the test scores.

1 (TestScores Class) + 2 (TestScores Class Custom Exception) + 9 (TestScores Modification for Serialization)

Challenge 9

Modify the TestScores Class that your created in challenge 1 and 2 to be serialized. Write a program that creates an array of at least five TestScore Objects and serializes them. Write another program that deserializes the objects from the file. Comment each line of code between { } brackets.

Only have to do Challenge 9.

Explanation / Answer

TestScores.java

import java.io.Serializable;

public class TestScores implements Serializable {

// Declaring instance variables

private int nos[] = null;

// Parameterized constructor

public TestScores(int arr[]) {

this.nos = arr;

}

// This method will return the average of all scores in the array

public double average(){

double average = 0.0, tot = 0.0;

for (int i = 0; i < nos.length; i++) {

tot += nos[i];

}

average = tot / nos.length;

return average;

}

}

_________________

InvalidTestScore.java

public class InvalidTestScore extends Exception {

private String mesg;

public InvalidTestScore(String mesg) {

this.mesg = mesg;

}

public String toString() {

return ("MyException Occurred: " + mesg);

}

}

_________________

Test.java

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

// Declaring variables

int size;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

// Getting the input entered by the user

System.out.print("Enter no of testscores :");

size = sc.nextInt();

// Creating an Integer array

int nos[] = new int[size];

int tarray[];

// getting the numbers entered by the user and populate them into an

// array

for (int i = 0; i < size;) {

System.out.print("Enter test score " + (i + 1) + ":");

try {

nos[i] = sc.nextInt();

if (nos[i] < 0 || nos[i] > 100) {

throw new InvalidTestScore("**Invalid Test Score**");

}

i++;

} catch (InvalidTestScore e) {

System.out.println(e);

}

}

// Creating an TestScores class instance

TestScores ts = new TestScores(nos);

System.out.println("Average :" + ts.average());

// writing array to disk

FileOutputStream fout;

try {

fout = new FileOutputStream("serial.txt");

ObjectOutputStream oout = new ObjectOutputStream(fout);

oout.writeObject(nos);

oout.close();

System.out.println("__Array is Serialized__");

// reading array from disk

FileInputStream fin = new FileInputStream("serial.txt");

ObjectInputStream oin = new ObjectInputStream (fin);

tarray = (int[])oin.readObject();

System.out.println("__Deserialized__");

System.out.println("Displaying the array Elements :");

for(int i=0;i<tarray.length;i++)

{

System.out.print(tarray[i]+" ");

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

___________________

Output:

Enter no of testscores :5
Enter test score 1:-50
MyException Occurred: **Invalid Test Score**
Enter test score 1:101
MyException Occurred: **Invalid Test Score**
Enter test score 1:89
Enter test score 2:98
Enter test score 3:87
Enter test score 4:76
Enter test score 5:67
Average :83.4
__Array is Serialized__
__Deserialized__
Displaying the array Elements :
89 98 87 76 67

_____________Thank You