Java and my following program, I need help in creatin an Exception class named InvalidScore. Modify my program so that it "throws" an InvalidScore Exception, when any of my test scores are not b/w 0 through 100. //class AverageScores class AverageScores { //array of scores declared int scores[]=new int[100]; //constructor public AverageScores(int[] scores) { super(); //accepts an array of test scores this.scores = scores; } //method that returns the average of the test scores double average() { //declares variables of integer type int sum=0; int count=0; //obtain length of array int len = scores.length; //for loop for(int i=0;i100) { //show a "DeniedException" System.out.println("DeniedException at index "+i+" and value "+scores[i]); //continue the loop continue; } //computed sum of the array sum=sum+scores[i]; //increment count by 1 count++; } //determine the average double average=(double)sum/count; //return average return average; } } //class test to Test the "AverageScores" class program public class test { //main method public static void main(String args[]) { //declared and initialize the array int scores[]= {61,72,-1,39,40,78,43,101,203,45,65}; //created object of class AverageScores AverageScores as=new AverageScores(scores); //calling method average() double avg=as.average(); //print average System.out.println("Average= "+avg); } }
In Java and my following
//class AverageScores
class AverageScores {
//array of scores declared
int scores[]=new int[100];
//constructor
public AverageScores(int[] scores) {
super();
//accepts an array of test scores
this.scores = scores;
}
//method that returns the average of the test scores
double average()
{
//declares variables of integer type
int sum=0;
int count=0;
//obtain length of array
int len = scores.length;
//for loop
for(int i=0;i<len;i++)
{
//if statement to check any test score in the array is negative or above 100
if(scores[i]<0 || scores[i]>100)
{
//show a "DeniedException"
System.out.println("DeniedException at index "+i+" and value "+scores[i]);
//continue the loop
continue;
}
//computed sum of the array
sum=sum+scores[i];
//increment count by 1
count++;
}
//determine the average
double average=(double)sum/count;
//return average
return average;
}
}
//class test to Test the "AverageScores" class program
public class test
{
//main method
public static void main(String args[])
{
//declared and initialize the array
int scores[]= {61,72,-1,39,40,78,43,101,203,45,65};
//created object of class AverageScores
AverageScores as=new AverageScores(scores);
//calling method average()
double avg=as.average();
//print average
System.out.println("Average= "+avg);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images