I am having issues getting my code to work properly. The objective of the program is to allow the user to enter 5 scores and then it is supposed to calculate the mean and rearrange the data set from highest to lowest (descending order).

I am supposed to run it through the AverageDriver class. The main method is only supposed to declare and instantiate an Average object. The Average object information should then be printed to the console. It should output the data set from highest to lowest and the mean.

I cannot get my selectionSort method to return the data in descending order.

import java.util.Scanner;  // Needed for Scanner class

/**
   Lab #8
   Average.java
   
   This program demonstrates passing an array
   as an argument to a method.
*/

public class Average
{
   private final int SCORES = 5;          // Total number of scores
   private double mean;                   // The arithmetic average of the scores
   private int[] data = new int[SCORES];  // Array of scores

   /**
      The constructor allocates memory
      for the array. A for loop prompts 
      the user to enter the 5 scores.
      @param data The scores.
   */

   public Average()
   {
      // Create a Scanner objects for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      System.out.println("Enter the " +
                    SCORES + " scores.");

      // Read values into the array
      for (int index = 0; index < SCORES; index++)
      {
         System.out.print("Enter score " +
                          (index + 1) + ": ");
         data[index] = keyboard.nextInt();

         // Call the selectionSort method.
         selectionSort();

         // Call the calculateMean method.
         calculateMean();
      }
   }

   /**
      The calculateMean method uses a for loop
      to access each score in the array and add
      it to the running total. The total divided
      by the number of scores is stored in the mean.
      @param mean The mean.
   */

   public void calculateMean()
   {
      double total = 0.0;   // Accumulator
      
      // Sum the values in the array
      for (int index = 0; index < SCORES; index++)
         total += data[index];
      mean = total / SCORES;
   }

   /**
      The selectionSort method performs a 
      selection sort on an int array. The 
      array is sorted in descending order.
      @param data The array to sort.
   */

   public void 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;
      }
   }

   /**
      toString method.
      @return mean The mean and string of descending data.
   */

   public String toString()
   {
      String dataToPrint = new String("");
         for (int i=0; i<data.length; i++)  
         {
            dataToPrint = dataToPrint + data[i];
         }
      dataToPrint = ("Data: " + dataToPrint + " \nMean: " + mean);
      return dataToPrint;
   }
}
public class AverageDriver 
{
   public static void main(String[] args)
   {
      Average object = new Average();
      System.out.println(object.toString());
   }
}

You have two problems in the sort routine which I can see.

The outer for loop initializes the startScan to 0 to run while startScan>data.length()-1 (4),
but startScan will immediately be less than 4 so the code block will not run! (You need >)

When you find the minimum value at the minIndex this is assigned to the next value in the array at startScan overwriting and losing any values at the point.

You will need to swap this minValue with whatever is there!

data[minIndex] = data[startScan;
data[startScan]=minValue;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.