0

When I run the code with the test scores: 71, 100, 98, 74, and 87, this is the output I get:

The test scores you entered, in descending order, are: [I@1a17589 The average is 86.

Why is my output for the selectionSort method not printing out correctly?

import java.util.Scanner;

/**
 * The Average class calculates the average of the test 
 * scores entered and puts the scores into descending order.
 */

public class Average
{
  private int[] data;

  public Average(int[] dataParam) 
  {
    data=dataParam;
  }

    public int calculateMean()
 {
   int total = 0;
   int mean;

    for (int index = 0; index < data.length; index++)
      total += data[index];
   mean = total / data.length;
   return mean;
 }

 public int[] selectionSort()
 {
   int startScan, index, minIndex, minValue;

    for (startScan = 0; startScan < (data.length-1); startScan++)
    {
      minIndex = startScan;
      minValue = data[startScan];
      for(index = startScan + 1; index < data.length; index++)
      {
        if (data[index] < minValue)
        {
          minValue = data[index];
          minIndex = index;
        }
      }
      data[minIndex] = data[startScan];
      data[startScan] = minValue;
    }

    return data;
 }
}

import java.util.Scanner;

public class AverageDriver
{
  public static void main(String[] args)
  {
   int[] data = new int[5];

    Scanner keyboard = new Scanner(System.in);

    for (int index = 0; index < data.length; index++) 
    {
      System.out.println("Enter score #" + (index + 1) + ": ");
      data[index] = keyboard.nextInt();
    }

    Average object = new Average(data);

   System.out.println("The test scores you entered, in descending order, are: ");
   System.out.println(object.selectionSort());
   System.out.println("The average is " + object.calculateMean() +".");

  }

}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Meg
  • 25
  • 2
  • It _is_ printing out correctly. – Sotirios Delimanolis Dec 13 '14 at 01:05
  • Yes, after reading the other question asked, I realized I have the code wrong. I'm just not sure how to write it when I'm using a method from a different file. – Meg Dec 13 '14 at 01:13
  • It doesn't matter where the method is. What matters is the return type. Your method returns an `int[]`. You're taking its value and passing it as an argument to `println`. You can store the value in a variable and use it as you wish. – Sotirios Delimanolis Dec 13 '14 at 01:14
  • @Meg You know how to pass the result of `object.selectionSort()` to `Arrays.toString()`, right? – Dawood ibn Kareem Dec 13 '14 at 01:19
  • @DavidWallace No that's what I'm trying to figure out – Meg Dec 13 '14 at 01:25
  • 1
    OK, there are a couple of options. You can make a variable to store the result of one, then pass it to the other. Like `int[] sortedData = object.selectionSort();` and then on the next line `System.out.println(Arrays.toString(sortedData));` or you can combine it all on one line like `System.out.println(Arrays.toString(object.selectionSort()));` – Dawood ibn Kareem Dec 13 '14 at 01:29

0 Answers0