1

Here is a problem given to me. The challenge for me is knowing how to find the AVERAGE from an array list and the value in the array that is closest to that average. All by writing code that can execute this with any array given in the test.

Here's the test I was given to write a class for:

import static org.junit.Assert.*;

import org.junit.Test;

public class Question2Test 
{

    int[] arrayInts = {1, 1, 3, 4, 9};

    private Question2 avg1;
    private Question2 avg2;
    private Question2 avg3;

    @Test
    public void testFindMiddle() 
    {
        Question2 avg1 = new Question2();
         assertEquals(1, avg1.getAverage());
         Question2 avg2 = new Question2();
         assertEquals(4, avg2.getAverage());
         Question2 avg3 = new Question2();
         assertEquals(0, avg3.getAverage());
    }
//find what I need to do with "getAverage"
}

what I have so far:

/**
 * Find the value in an array that is closest to the average.
 */
public class Question2 
{
  private int avg;

  public double findMiddle(int[] arrays)
  { 
    //Find the average

    double sum = 0;

    for(int i = 0; i < arrays.length; i++)
    {

        sum += arrays[i]; 
    }    
    double avg = sum/arrays.length; 
    return avg;


    // Now Find the value in an array that is closest to the average:

  for(int i = 0; i < arrays.length; i++)
  {
      int arrays[i] = Math.abs(arrays[i] + 1);

    if(arrays[i] == avg)
    {
        return arrays[i];
    }

  } 
}

public int getAverage(int[] array) // is a getter: a method whose purpose is to return the value stored in an instance variable!
{   
  return avg;
}   

}

So the first problem is it does not take my seconds for loop at all. I was able to find the average, now Java is not accepting my finding the value closest to it.

  • 1
    Test is asserting 3 different averages, but only 1 array is given (and not passed to Question2 class), so something is completely wrong with test first – Iłya Bursov Aug 06 '18 at 21:31
  • 1
    Please share a complete and verifiable example of `Question2Test` so that we can understand what is required. The current version doesn't even compile. – Mick Mnemonic Aug 06 '18 at 21:35
  • 1
    Welcome to StackOverflow. Please read about how to post a [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve). It's the best way for us to help you. – Jeff Learman Aug 06 '18 at 22:08

1 Answers1

1

As you said you have two different parts of the question:

  1. Finding the average
  2. Find the number in the array closest to that average.

Regarding problem 1, in your code you have the right idea, but the wrong implementation. you want to add up all of the elements in the array and then divide that sum by the amount of elements in the array. In practice, all you need to do is to take the division out of the loop. It would look something like this:

double sum = 0;
for(int i = 0; i < arrays.length; i++)
        {
            sum += arrays[i]; //the += takes the current value of sum and adds the array[i] value to it. 
        }    
double avg = sum/arrays.length; //Since sum is a double avg can be a double as well.

For the second part you want to find the number in the array closest to this average. With the Math.abs() method you can get the absolute value of the difference between two numbers. Using this method you can cycle through all of the elements in your array, call the Math.abs() method on them, and save the one where the result is the smallest.

A. K.
  • 150
  • 9