Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix code: I keep getting it wrong: / / package Grades; import java.util.Arrays; import java.util.Scanner; public class Grades { / / Studying values from the

Fix code: I keep getting it wrong://package Grades;
import java.util.Arrays;
import java.util.Scanner;
public class Grades {
// Studying values from the consumer
public static int[] readGrades(){
Scanner read = new Scanner(System.in);
System.out.print("Input the number of grades: ");
int length = read.nextInt();
int[] grades = new int[length];
for (int i =0; i length; i++){
System.out.print("Input "+(i +1)+"th grade: ");
int grade = read.nextInt();
grades[i]= grade;
}
read.close();
return grades;
}
// Calculating the sum of grades in the array
public static int sum(int[] arr){
int sum =0;
for (int i =0; i arr.length; i++){
sum = sum + arr[i];
}
return sum;
}
// Calculating the sum of grades at the first and last index
public static int sum(int[] arr, int firstIndex, int lastIndex){
return arr[firstIndex]+ arr[lastIndex];
}
// Calculating the average of grades in the array
public static double average(int[] arr){
int sum =0;
for (int i =0; i arr.length; i++){
sum = sum + arr[i];
}
return (double) sum / arr.length;
}
// Finding the maximum grade in the array of grades
public static int maxValue(int[] arr){
int max = arr[0];
for (int i =1; i arr.length; i++){
if (max arr[i]){
max = arr[i];
}
}
return max;
}
// Finding the minimum grade in the array of grades
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;
}
// calculateAverage method that takes an array of int as input and returns the average of its elements.
public static double calculateAverage(int[] array){
if (array == null || array.length ==0){
throw new IllegalArgumentException("Array is null or empty");
}
double sum =0;
for (int num : array){
sum += num;
}
return sum / array.length;
}
// Java function that calculates the number of elements below a certain average in an array
public static int countElementsBelowAverage(int[] array){
if (array == null || array.length ==0){
throw new IllegalArgumentException("Array is null or empty");
}
double average = average(array);
int count =0;
for (int num : array){
if (num average){
count++;
}
}
return count;
}
// Java function that calculates the number of elements above a certain average in an array
public static int countElementsAboveAverage(int[] array){
if (array == null || array.length ==0){
throw new IllegalArgumentException("Array is null or empty");
}
double average = average(array);
int count =0;
for (int num : array){
if (num > average){
count++;
}
}
return count;
}
// Here's a simple function in Java that reverses an array
public static int[] reverseArray(int[] array){
if (array == null || array.length ==0){
throw new IllegalArgumentException("Array is null or empty");
}
int[] reversedArray = new int[array.length];
for (int i =0; i array.length; i++){
reversedArray[i]= array[array.length -1- i];
}
return reversedArray;
}
// If you want to rotate elements in an array to a given count and return the rotated array, you can use the following Java function:
public static int[] rotateArray(int[] array, int positions){
if (array == null || array.length ==0){
throw new IllegalArgumentException("Array is null or empty");
}
int n = array.length;
positions = positions % n; // Adjust for positions larger than array length
int[] rotatedArray = new int[n];
for (int i =0; i n; i++){
int newIndex =(i + positions)% n;
rotatedArray[newIndex]= array[i];
}
return rotatedArray;
}
// Main approach
public static void main(String[] args){
int[] arr;
arr = readGrades();
System.out.println("Sum: "+ sum(arr));
System.out.println("Sum of the first index and last index values: "+ sum(arr,0, arr.length -1));
System.out.println("Average of grades: "+ average(arr));
System.out.println("Maximum Grade: "+ maxValue(arr));
Sys
image text in transcribed

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

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions