Ty Givens
Ty Givens

Reputation: 111

Average Class not working

What is wrong here? It's not printing test scores in descending order, nor am I getting a value for mean. Shows up 0.0

Her are the instructions that I was given:

This class will allow a user to enter 5 scores into an array. It will then rearrange the data in descending order and calculate the mean for the data set.

Attributes:

• data[]—the array which will contain the scores

• mean—the arithmetic average of the scores

Methods:

• Average—the constructor. It will allocate memory for the array. Use a for loop to repeatedly display a prompt for the user which should indicate that user should enter score number 1, score number 2, etc. Note: The computer starts counting with 0, but people start counting with 1, and your prompt should account for this. For example, when the user enters score number 1, it will be stored in indexed variable 0. The constructor will then call the selectionSort and the calculateMean methods.

• calculateMean—this is a method that uses a for loop to access each score in the array and add it to a running total. The total divided by the number of scores (use the length of the array), and the result is stored into mean.

• toString—returns a String containing data in descending order and the mean.

• selectionSort—his method uses the selection sort algorithm to rearrange the data set from highest to lowest.

import java.util.Scanner;

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

    public Average()
    {

        data = new int[5];
        Scanner keyboard = new Scanner(System.in);

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

    public void calculateMean()
    {

        int i, s = 0;
        for(i = 0; i < data.length; i++)
        {
            s = s + data[i];
        }

        mean = (double)s / (data.length);

    }

    public void selectionSort()
    {
        int maxIndex;
        int maxValue;

        for(int startScan = 0; startScan < data.length - 1; startScan++)
        {
            maxIndex = startScan;
            maxValue = data[startScan];
            for(int index = startScan + 1; index < data.length; index++)
            {
                if(data[index] > maxValue)
                {
                    maxValue = data[index];
                    maxIndex = index;
                }
            }
            data[maxIndex] = data[startScan];
            data[startScan] = maxValue;
        }
    }

    public String toString()
    {
        String output;
        output = "The test scores in descending order are \n";

        for(int i = 0; i < data.length; i++)
        {
            output = output + data[i] + " ";
        }
        output = output + "\nThe average is " + mean;
        return output;
    }
}

Upvotes: 1

Views: 5247

Answers (4)

alvarodev
alvarodev

Reputation: 127

It looks like you are not executing the calculateMean() method call first like that:

public static void main(String[] args) {
    Average average = new Average();
    average.calculateMean();
    average.selectionSort();
    System.out.println(average.toString());
}

Upvotes: 2

HeatfanJohn
HeatfanJohn

Reputation: 7333

You never make a call to calculateMean(), that is why the average is zero. In you main or where ever you create your instance of Average, you need to call calculateMean() before referencing Average.toString().

Upvotes: 1

dmahapatro
dmahapatro

Reputation: 50265

All you need is

public static void main(String[] args){
       Average avg = new Average();
       avg.selectionSort();
       avg.calculateMean();    
       System.out.println(avg);
    }

Everything is in place. I second @HeatfanJohn

Upvotes: 2

syb0rg
syb0rg

Reputation: 8247

You need methods that return and take in values with your methods. Here is a little mock-up of your program to show what I mean:

public static void main(String... args)
{
    System.out.println(calculateMean(getData()));
}

public static int[] getData()
{
    data = new int[5];
    Scanner s = new Scanner(System.in);

    for (int i = 0; i < data.length; i++)
    {
        System.out.print("Enter score number " + (i++) + ": ");
        data[i] = Integer.parseInt(s.nextLine());
    }
    s.close();
    return data;
}

public static double calculateMean(int[] data)
{

    int s = 0;
    for (int i = 0; i < data.length; i++)
    {
        s += data[i];
    }
    return mean = (double) s / (data.length);
}

The getData() method gets all the information we need from the user, and then we take that data and we pass it right along to the calculateMean() method. This, in turn, spits out the average of all the scores for us. Then all we do is print that. I'll leave the rest up to you since this looks like homework.


Trial run:

Input: 4, 67, 3, 7, 3 (comma's indicate new line)

Output: 16.8

Upvotes: 2

Related Questions