In: Computer Science
Lab to be performed in Java.
Lab:
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. Write a driver class to test that demonstrates that an exception happens for these scenarios
2.) Write a class named InvalidTestScore by reusing the code in TestScores class you wrote for 1) so that it throws an InvalidTestScore exception. Write a driver class to test that demonstrates that an exception happens for these scenarios.
Note 1) and 2) are two separate programs that must be handed in as well as your test score array.
---- Create a class TestScores and TestScores constructor should accept an array of test scores as its argument. Users will enter 5 test scores use a scanner to get input from the console.
---- Create a method that returns or print to console the correct average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException.
---- Create a class InvalidTestScore by reusing the code of TestScores that print to console the line “Test score values should not be greater than 100 or negative” when IllegalArgumentException is thrown.
1.) Solution:
import java.util.*;
class TestScores{
private int[] scores;
public TestScores(int[] scores){
this.scores=scores;
}
public int average(){
int sum=0;
for(int i=0;i<scores.length;i++){
if(scores[i]>0&&scores[i]<=100){
sum+=scores[i];
}
else{
throw new IllegalArgumentException();
}
}
return sum/scores.length;
}
}
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of test scores:");
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
System.out.print("Enter the Test Score"+(i+1)+":");
a[i]=sc.nextInt();
}
TestScores obj=new TestScores(a);
System.out.println("\nThe average of test scores is:"+obj.average());
}
}
2.)Solution:
import java.util.*;
class TestScores{
public int[] scores;
public TestScores(int[] scores){
this.scores=scores;
}
public int average(){
int sum=0;
for(int i=0;i<scores.length;i++){
if(scores[i]>0&&scores[i]<=100){
sum+=scores[i];
}
else{
throw new IllegalArgumentException();
}
}
return sum/scores.length;
}
}
class InvalidTestScore extends TestScores{
public InvalidTestScore(int[] scores){
super(scores);
}
public int average(){
int sum=0;
for(int i=0;i<scores.length;i++){
if(scores[i]>0&&scores[i]<=100){
sum+=scores[i];
}
else{
throw new IllegalArgumentException("Test score values should not be greater than 100 or negative");
}
}
return sum/scores.length;
}
}
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of test scores:");
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
System.out.print("Enter the Test Score"+(i+1)+":");
a[i]=sc.nextInt();
}
InvalidTestScore obj=new InvalidTestScore(a);
System.out.println("\nThe average of test scores is:"+obj.average());
}
}
Thank you! if you have any queries post it below in the comment section I will try my best to resolve your queries and I will add it to my answer if required. Please give upvote if you like it.