Java Thanks! Write a class named Test Scores. The class constructor should accep
ID: 3789425 • Letter: J
Question
Java
Thanks!
Write a class named Test Scores. 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 an application called TestScoresDemo1. Write an exception class named InvalidTestScore. Modify the TestScores class you previously wrote so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid. Demonstrate the class in an application called TestScoresDemo2.Explanation / Answer
1)
TestScores.java
public class TestScores {
int arr[];
public TestScores(int[] arr) {
this.arr = arr;
}
public double average()
{
double avg=0.0,sum=0.0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<0 || arr[i]>100)
{
try {
throw new IllegalArgumentException();
} catch (Exception e) {
System.out.println("Exception "+e);
}
}
else
{
sum+=arr[i];
}
avg=(double)sum/arr.length;
}
return avg;
}
}
_____________________
TestScoresDemo1.java
import java.util.Scanner;
public class TestScoresDemo1 {
public static void main(String[] args) {
int testScores[]=new int[10];
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.println("** Enter 10 test Scores **");
for(int i=0;i<10;i++)
{
System.out.print("Enter the Test Score"+(i+1)+":");
testScores[i]=sc.nextInt();
}
TestScores tc=new TestScores(testScores);
tc.average();
}
}
______________________
output:
** Enter 10 test Scores **
Enter the Test Score1:56
Enter the Test Score2:78
Enter the Test Score3:-34
Enter the Test Score4:67
Enter the Test Score5:88
Enter the Test Score6:99
Enter the Test Score7:114
Enter the Test Score8:34
Enter the Test Score9:67
Enter the Test Score10:89
Exception java.lang.IllegalArgumentException
Exception java.lang.IllegalArgumentException
_______________
2)
TestScores.java
package org.students;
public class TestScores {
int arr[];
public TestScores(int[] arr) {
this.arr = arr;
}
public double average()
{
double avg=0.0,sum=0.0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]<0 || arr[i]>100)
{
try {
throw new InvalidTestScore();
} catch (Exception e) {
System.out.println("Exception "+e);
}
}
else
{
sum+=arr[i];
}
avg=(double)sum/arr.length;
}
return avg;
}
}
___________________
InvalidTestScore.java
public class InvalidTestScore extends Exception {
}
____________________
TestScoresDemo2.java
import java.util.Scanner;
public class TestScoresDemo2 {
public static void main(String[] args) {
int testScores[]=new int[10];
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.println("** Enter 10 test Scores **");
for(int i=0;i<10;i++)
{
System.out.print("Enter the Test Score"+(i+1)+":");
testScores[i]=sc.nextInt();
}
TestScores tc=new TestScores(testScores);
tc.average();
}
}
_____________________
Output:
** Enter 10 test Scores **
Enter the Test Score1:56
Enter the Test Score2:78
Enter the Test Score3:90
Enter the Test Score4:-45
Enter the Test Score5:117
Enter the Test Score6:89
Enter the Test Score7:-67
Enter the Test Score8:145
Enter the Test Score9:90
Enter the Test Score10:95
Exception org.students.InvalidTestScore
Exception org.students.InvalidTestScore
Exception org.students.InvalidTestScore
Exception org.students.InvalidTestScore
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.