Question

5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accep
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Output 556 111.2Let me know if you have any doubts in the comments. Please upvote if the answer helped you.

Add a comment
Know the answer?
Add Answer to:
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 5: Design and implement a Java program (name it ArrayMethods), that defines 4 methods as...

    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...

  • Design and implement a Java program (name it ArrayMethods), that defines 4 methods as follows: int...

    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...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    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...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    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...

  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // 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...

    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...

    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...

    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:...

  • I need some with this program, please look at the changes I need closely. Its running...

    I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need: 1. All of the integers on a single line, sorted in ascending order. 2. The median value of the sorted array on a single line 3. The average of the sorted array on a single line Here is the program: #include<stdio.h> #include<stdlib.h> /* This places the Adds...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT