We will just iterate through the array and find the sum of the elements in the first method.
Then we will use the sum() method to implement the avergae() method.
And then, we will use the average() method to implement the aboveAverage() and belowAverage() methods.
I have also tested the methods on a sample array.
Here's the code.
import java.util.*;
import java.lang.*;
import java.io.*;
class ArrayMethods
{
public static int sum(int[] arr, int firstIndex, int
lastIndex){
if(firstIndex < 0 || lastIndex >= arr.length || firstIndex
> lastIndex){
return -666;
}
int sum = 0;
for(int i = firstIndex; i <= lastIndex; i++){
sum += arr[i];
}
return sum;
}
public static double average(int[] arr, int firstIndex, int
lastIndex){
double avg = 0;
double sum = sum(arr, firstIndex, lastIndex);
avg = sum / (lastIndex - firstIndex);
return avg;
}
public static int belowAverage(int[] arr, int firstIndex, int
lastIndex){
double avg = average(arr, firstIndex, lastIndex);
int count = 0;
for(int i = firstIndex; i <= lastIndex; i++){
if(arr[i] < avg){
count++;
}
}
return count;
}
public static int aboveAverage(int[] arr, int firstIndex, int
lastIndex){
double avg = average(arr, firstIndex, lastIndex);
int count = 0;
for(int i = firstIndex; i <= lastIndex; i++){
if(arr[i] > avg){
count++;
}
}
return count;
}
public static void main (String[] args){
int[] arr = {23, 367, 36, 26, 82, 7, 38, 18,
75};
System.out.println(sum(arr, 1, 6));
System.out.println(average(arr, 1, 6));
System.out.println(aboveAverage(arr, 1, 6));
System.out.println(belowAverage(arr, 1, 6));
}
}
Here are the screenshots of the code and the output.
![import java.util.*; import java.lang.*; import java.io.*; class ArrayMethods public static int sum(int[] arr, int first Index](http://img.homeworklib.com/questions/39143e50-1e45-11eb-b508-9f46a6b4c8f0.png?x-oss-process=image/resize,w_560)
Let me know if you have any
doubts in the comments. Please upvote if the answer helped you.
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....
Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) determines and returns the largest value within an array int arrayMin(int[] arr) determines and returns the smallest value within an array void arrayReverse(int[] arr) reverses the array (for example: 7 8 1 2 becomes 2 1 8 7) void arraySquared(int[] arr) changes every value within the array to value² Test your methods by creating an array of length 5 within...
Write a program named Review.java and implement the following two methods: public static int NumberOfElementsLessThanAverage(int[] arr) that returns the number of elements that are less than the average of all elements of the array. public static int[] countChars(File file), this method reads data from a file and counts the frequency of each alphabetic letters, and returns those numbers as an int array. the data file name is "data.txt"
Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...
Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax (int [ ] arr) determines and returns the maximum value within an array int arrayMin (int [ ] arr) determines and returns the minimum value within an array void arraySquared (int [] arr) changes every value within the array to value2 void arrayReverse (int [ ] arr) reverses the array (for example: 7 8 12 becomes 2 18 7) Test your methods by...
You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...
3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...
// CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...
Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...
in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...