Creating a Sorting method to collect input and order from greatest to least

623 views java
2

When running the program it asks the user for 5 test scores and inputs them into an array. It is supposed to sort them from greatest to least and display them. The result given was " 0 0 0 0 0 ".

The first fie is Average.java, which is run by AverageDriver.java which is included under this code

import java.util.Scanner;

public class Average
{
private int[] data;
private double mean;

public Average()
{
    int[] data = new int[5];
    int number = 0;
    int Temp = 0;
    String[] Scores = new String[5];
    Scanner keyboard = new Scanner(System.in);

    for(int index = 0; index < data.length; index++)
    {
        number++;
        System.out.print("Enter test score #" + number + ":");
        Temp = keyboard.nextInt();
        Temp = data[index];
        data[index]=Scores[index];
    }

    calculateMean();
    selectionSort();
}

public void selectionSort()
    {
        int startScan, index, minIndex, minValue;
        int[] number = new int[5];

             for(startScan = 0; startScan < (number.length-1); startScan++)
             {
                minIndex = startScan;
                minValue = number[startScan];

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

     public void calculateMean()
     {
         int Sum = 0;
         int Mean = 0;
         int[] Values = new int[5];

         for(int index = 0; index < Values.length; index++)
         {
             Sum+=Values[index];
         }
         Mean = Sum/Values.length;
     }

     public String toString()
    {
        String MyScore = "Your test scores in Descending Order are:";
        int[] Numbers = new int[5];

        for(int index = 0; index < Numbers.length; index++)
        {
            MyScore += Numbers[index] + " ";
        }
        return MyScore;
    }
}

The program requires this file to run

public class AverageDriver
{
    public static void main(String[] args)
    {
        Average MyScores = new Average();
        System.out.print(MyScores);
    }
}

answered question

1 Answer

0

In the constructor you have:

public Average()
{
    int[] data = new int[5];   
    ...
}

Here you are creating a local variable data instead of using your class variable. This means that once the constructor is left, your local variable is destroyed, and data remains untouched. Remove the int[] part:

data = new int[5];

And again in your toString():

public String toString()
{
    String MyScore = "Your test scores in Descending Order are:";

    int[] Numbers = new int[5];

    for(int index = 0; index < Numbers.length; index++)
    {
        MyScore += Numbers[index] + " ";
    }
    return MyScore;
}

You create a local Array with all zeroes

posted this

Have an answer?

JD

Please login first before posting an answer.