How do I fix the code?   Code:  import java.util.Scanner; public class Grades {     static int count = 0;     public static int readGrades() {         int grade,count = 0;         Scanner input = new Scanner(System.in);         grade = input.nextInt();         return grade;     }     public static int sum(int[] arr) {         int sum = 0;         for (int i = 0; i < arr.length; i++)             sum = sum + arr[i];         return sum;     }     public static int sum(int[] arr, int firstIndex, int lastIndex) {         if (firstIndex < 0 || lastIndex >= arr.length || lastIndex >= firstIndex || firstIndex >= arr.length || lastIndex < 0 || firstIndex < lastIndex) {         return -666;         }         int sum = 0;         for (int i = firstIndex; i <= lastIndex; i++)             sum = sum + arr[i];         return sum;     }     public static double average(int[] arr) {         double sum = sum(arr);         return ((float) sum / (float) (arr.length));     }     public static int maxValue(int[] arr) {         int max = 0;         for (int i = 0; i < arr.length; i++) {             if (max < arr[i]) {                 max = arr[i];             }         }         return max;     }     public static int maxValue(int[] arr, int firstIndex, int lastIndex) {         if (firstIndex < 0 || lastIndex >= arr.length || lastIndex >= firstIndex || firstIndex >= arr.length || lastIndex < 0 || firstIndex < lastIndex) {         return -666;         }         int max = 0;         for (int i = firstIndex; i <= lastIndex; i++) {             if (max < arr[i]) {                 max = arr[i];             }         }         return max;     }     public static int indexOfFirstMaxValue(int[] arr) {         int max = 0;         for (int i = 0; i < arr.length; i++) {             if (maxValue(arr) == arr[i]) {                 max = i;             }         }         return max;     }     public static int minValue(int[] arr) {         int min = arr[0];         for (int i = 1; i < arr.length; i++) {             if (min > arr[i]) {                 min = arr[i];             }         }         return min;     }     public static int minValue(int[] arr, int firstIndex, int lastIndex) {         if (firstIndex < 0 || lastIndex >= arr.length || lastIndex >= firstIndex || firstIndex >= arr.length || lastIndex < 0 || firstIndex < lastIndex) {         return -666;         }         int min = arr[0];         for (int i = firstIndex; i <= lastIndex; i++) {             if (min > arr[i]) {                 min = arr[i];             }         }         return min;     }     public static int indexOfFirstMinValue(int[] arr) {         int min = 0;         for (int i = 0; i < arr.length; i++) {             if (minValue(arr) == arr[i]) {                 min = i;             }         }         return min;     }     public static int numberOfBelowAverageElements(int[] arr) {         int count = 0;         for (int i = 0; i < arr.length; i++) {             if (arr[i] < average(arr)) {                 count++;             }         }         return count;     }     public static int numberOfAboveAverageElements(int[] arr) {         int count = 0;         for (int i = 0; i < arr.length; i++) {             if (arr[i] > average(arr)) {                 count++;             }         }         return count;     }     public static void rotateElements(int[] arr) {         int i, temp;         temp = arr[0];         for (i = 0; i < arr.length - 1; i++) {             arr[i] = arr[i + 1];         }         arr[arr.length - 1] = temp;         System.out.print("\nRotated array is: \n");         for (int k = 0; k < arr.length; k++) {             System.out.print(arr[k] + " ");         }     }     public static void rotateElements(int[] arr, int rotationCount) {         for (int i = 0; i < rotationCount; i++)             rotateElements(arr);         System.out.print("\nRotated array is: \n");         for (int k = 0; k < arr.length; k++) {             System.out.print(arr[k] + " ");         }     }     public static void reverseArray(int[] arr) {         int n = arr.length;         int[] b = new int[n];         int j = n;         for (int i = 0; i < n; i++) {             b[j - 1] = arr[i];             j = j - 1;         }         System.out.print("\nReversed array is: \n");         for (int k = 0; k < n; k++)             System.out.print(b[k] + " ");     }     public static void main(String[] args) {         int grade[] = new int[5];         int i = 0;         System.out.println("Enter a grade : ");         while ( i < 5) {             grade[i] = readGrades();             if (grade[i] == -1)                {                    break;                }             else {                 i++;                 count++;             }         }         System.out.println("Number of grades: " + count);                 //take care of the firstIndex and lastIndex parameters [0, count-1]         System.out.println("The maximum grades is: " + maxValue(grade, 0, count-1));         System.out.println("The minimum grades is: " + minValue(grade, 0, count -1));     }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How do I fix the code?

 

Code: 

import java.util.Scanner;

public class Grades {

    static int count = 0;

    public static int readGrades() {
        int grade,count = 0;
        Scanner input = new Scanner(System.in);
        grade = input.nextInt();
        return grade;
    }

    public static int sum(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++)
            sum = sum + arr[i];
        return sum;
    }

    public static int sum(int[] arr, int firstIndex, int lastIndex) {
        if (firstIndex < 0 || lastIndex >= arr.length || lastIndex >= firstIndex || firstIndex >= arr.length || lastIndex < 0 || firstIndex < lastIndex) {
        return -666;
        }


        int sum = 0;
        for (int i = firstIndex; i <= lastIndex; i++)
            sum = sum + arr[i];
        return sum;
    }

    public static double average(int[] arr) {
        double sum = sum(arr);
        return ((float) sum / (float) (arr.length));
    }

    public static int maxValue(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }

    public static int maxValue(int[] arr, int firstIndex, int lastIndex) {
        if (firstIndex < 0 || lastIndex >= arr.length || lastIndex >= firstIndex || firstIndex >= arr.length || lastIndex < 0 || firstIndex < lastIndex) {
        return -666;
        }

        int max = 0;
        for (int i = firstIndex; i <= lastIndex; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }

    public static int indexOfFirstMaxValue(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (maxValue(arr) == arr[i]) {
                max = i;
            }
        }
        return max;
    }

    public static int minValue(int[] arr) {
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        return min;
    }

    public static int minValue(int[] arr, int firstIndex, int lastIndex) {
        if (firstIndex < 0 || lastIndex >= arr.length || lastIndex >= firstIndex || firstIndex >= arr.length || lastIndex < 0 || firstIndex < lastIndex) {
        return -666;
        }

        int min = arr[0];
        for (int i = firstIndex; i <= lastIndex; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        return min;
    }

    public static int indexOfFirstMinValue(int[] arr) {
        int min = 0;
        for (int i = 0; i < arr.length; i++) {
            if (minValue(arr) == arr[i]) {
                min = i;
            }
        }
        return min;
    }

    public static int numberOfBelowAverageElements(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < average(arr)) {
                count++;
            }
        }
        return count;
    }

    public static int numberOfAboveAverageElements(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > average(arr)) {
                count++;
            }
        }
        return count;
    }

    public static void rotateElements(int[] arr) {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < arr.length - 1; i++) {
            arr[i] = arr[i + 1];
        }
        arr[arr.length - 1] = temp;
        System.out.print("\nRotated array is: \n");
        for (int k = 0; k < arr.length; k++) {
            System.out.print(arr[k] + " ");
        }
    }

    public static void rotateElements(int[] arr, int rotationCount) {
        for (int i = 0; i < rotationCount; i++)
            rotateElements(arr);
        System.out.print("\nRotated array is: \n");
        for (int k = 0; k < arr.length; k++) {
            System.out.print(arr[k] + " ");
        }
    }

    public static void reverseArray(int[] arr) {
        int n = arr.length;
        int[] b = new int[n];
        int j = n;
        for (int i = 0; i < n; i++) {
            b[j - 1] = arr[i];
            j = j - 1;
        }
        System.out.print("\nReversed array is: \n");
        for (int k = 0; k < n; k++)
            System.out.print(b[k] + " ");
    }

    public static void main(String[] args) {
        int grade[] = new int[5];
        int i = 0;
        System.out.println("Enter a grade : ");
        while ( i < 5) {
            grade[i] = readGrades();
            if (grade[i] == -1)
               {
                   break;
               }
            else {
                i++;
                count++;
            }
        }

        System.out.println("Number of grades: " + count);
       
        //take care of the firstIndex and lastIndex parameters [0, count-1]
        System.out.println("The maximum grades is: " + maxValue(grade, 0, count-1));
        System.out.println("The minimum grades is: " + minValue(grade, 0, count -1));

    }
}

**Testing `Grades.sum(int[] arr, int firstIndex, int lastIndex)` with `firstIndex < 0`:**

- **Your output:**  
  Incorrect indexes entered.

- **Test feedback:**  
  The method `Grades.sum(int[] arr, int firstIndex, int lastIndex)` is not properly checking `firstIndex < 0`.
Transcribed Image Text:**Testing `Grades.sum(int[] arr, int firstIndex, int lastIndex)` with `firstIndex < 0`:** - **Your output:** Incorrect indexes entered. - **Test feedback:** The method `Grades.sum(int[] arr, int firstIndex, int lastIndex)` is not properly checking `firstIndex < 0`.
**Testing Scenario**

This test evaluates the `Grades.sum(int[] arr, int firstIndex, int lastIndex)` method by checking the condition where `firstIndex >= arr.length`.

**Expected Output:**
- The method should handle incorrect index entries properly.

**Your Output:**
- Message: "Incorrect indexes entered."

**Test Feedback:**
- Error Message: "***** Error ***** Grades.sum(int[] arr, int firstIndex, int lastIndex) invoked with invalid indexes."

**Explanation:**
The test checks if the method correctly identifies and handles cases where the starting index (`firstIndex`) is beyond or equal to the length of the array, which is an invalid scenario for array operations. The feedback suggests that there was an error due to invalid index parameters used in the method call.
Transcribed Image Text:**Testing Scenario** This test evaluates the `Grades.sum(int[] arr, int firstIndex, int lastIndex)` method by checking the condition where `firstIndex >= arr.length`. **Expected Output:** - The method should handle incorrect index entries properly. **Your Output:** - Message: "Incorrect indexes entered." **Test Feedback:** - Error Message: "***** Error ***** Grades.sum(int[] arr, int firstIndex, int lastIndex) invoked with invalid indexes." **Explanation:** The test checks if the method correctly identifies and handles cases where the starting index (`firstIndex`) is beyond or equal to the length of the array, which is an invalid scenario for array operations. The feedback suggests that there was an error due to invalid index parameters used in the method call.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 12 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education