Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I've done most of the work, but have a problem with the The average() method will change all the grades in the array to 60

I've done most of the work, but have a problem with the "The average() method will change all the grades in the array to 60 that are less than 60, after the average is calculated. This will also be done if an exception is thrown, and the average is not calculated. This will be done in a finally block INSIDE the average method." section. This is in Java programming, by the way.

image text in transcribed

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

public static void main(String[] args) { int[] grades1 = { 1000, 2, 3, 4 }; try { System.out.println("The average of the grades is " + average(grades1));

} catch (InvalidGradeException ige) { System.out.println(ige.getMessage()); }

}// end main

private static int average(int[] grades) throws InvalidGradeException { int average = 0; for (int i = 0; i 100) { throw new InvalidGradeException("Invalid grade at index " + i + ". Value is " + grades[i] + "."); } average += grades[i]; }

average /= grades.length; return average; }// end average

public class InvalidGradeException extends Exception { public InvalidGradeException(String message) { super(message); }

}

Write a method called average) that accepts an array of integers called grades If the method does not throw an exception, it will return the average of the grades in the array. Start by implementing that. The method will throw an exception of type InvalidradeException if any element of the array is greater than 100 or less than 0 You must create the InxalidGradeExcsption class. The InvalidGradeException object should contain information about what went wrong, i.e., which of the grades was invalid (its index), and what the invalid value was. The exception will be caught by the caller (the main method) and print out the information contained in the lavalidGradeException object. You are demonstrating that you can pass information from a method back to the caller, by putting it in an Exception object and throwing it The average0) method wil change all the grades in the array to 60 that are less than 60, after the average is calculated. This will also be done if an exception is thrown, and the average is not calculated. This will be done in a finally block INSIDE the average method. Demonstrate that the average method will work for an int array of various sizes, even though the method has only one parameter. Demonstrate that your method will work with an array that has no invalid grades, AND with an array that has invalid grades

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions