Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Default AverageDriver won't pull data correctly

    Could someone please help me?? I'm unable to get the output for "AverageDriver" to print correctly(5 scores in descending order and average). The output displays the scores the same way they are entered and the average is always zero. Could someone please take a look and help me fix this code. The output for the Average program works fine and with the correct output.

    import java.util.Scanner;

    //This class keeps 5 scores entered by the user

    public class Average
    {
    private int[] data;
    private double mean;
    public Average()
    {
    data = new int[5];
    Scanner keyboard = new Scanner(System.in);

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

    public void calculateMean() //Calculates the mean of the 5 scores entered by the user
    {
    int score, sum = 0;
    for(score = 0; score < data.length; score++)
    {
    sum = sum + data[score];
    }

    mean = (double)sum / (data.length);
    }

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

    //Reads values into the array
    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 = "Your scores in descending order are \n";

    for(int score = 0; score < data.length; score++)
    {
    output = output + data[score] + " ";
    }
    output = output + "\nThe average score = " + mean;
    return output;
    }
    public static void main(String[] args){
    Average AverageScore = new Average();
    AverageScore.selectionSort();
    AverageScore.calculateMean();
    System.out.println(AverageScore);
    }
    }
    --------------------------------------------------------------------------

    public class AverageDriver{
    public void main(String [] args) {

    //Create Average object
    Average average = new Average();

    //Print Details
    System.out.println(average.toString());
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: AverageDriver won't pull data correctly

    The output displays the scores the same way they are entered and the average is always zero.
    Java Code:
    public void main(String [] args) {
        //Create Average object
        Average average = new Average();
    
        //Print Details
        System.out.println(average.toString());
    }
    Do you ever call selectionSort() or calculateMean()? If you don't then the data will remain in the order it is entered and the mean will remain at zero.

Similar Threads

  1. Pull Out JPanel
    By TE5LA in forum AWT / Swing
    Replies: 1
    Last Post: 10-02-2014, 12:04 AM
  2. About to pull my hair out!!
    By louiedogg418 in forum New To Java
    Replies: 36
    Last Post: 03-25-2012, 04:13 AM
  3. Website to pull data from java sockets?
    By josephjah in forum Advanced Java
    Replies: 2
    Last Post: 07-24-2011, 08:24 AM
  4. Oracle & JSTL - Data not being retrieved correctly?
    By kev_thomas in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-09-2009, 12:08 PM
  5. Oracle & JSTL - Data not being retrieved correctly?
    By kev_thomas in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-09-2009, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •