Note that there are some explanatory texts on larger screens.

plurals
  1. POAverage Class not working
    primarykey
    data
    text
    <p>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</p> <p>Her are the instructions that I was given:</p> <p>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.</p> <p>Attributes:</p> <p>• data[]—the array which will contain the scores</p> <p>• mean—the arithmetic average of the scores</p> <p>Methods:</p> <p>• 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.</p> <p>• 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.</p> <p>• toString—returns a String containing data in descending order and the mean.</p> <p>• selectionSort—his method uses the selection sort algorithm to rearrange the data set from highest to lowest.</p> <pre><code>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 &lt; 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 &lt; data.length; i++) { s = s + data[i]; } mean = (double)s / (data.length); } public void selectionSort() { int maxIndex; int maxValue; for(int startScan = 0; startScan &lt; data.length - 1; startScan++) { maxIndex = startScan; maxValue = data[startScan]; for(int index = startScan + 1; index &lt; data.length; index++) { if(data[index] &gt; 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 &lt; data.length; i++) { output = output + data[i] + " "; } output = output + "\nThe average is " + mean; return output; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload