Working on homework for Java class, question I am having difficulty on is
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 Copyright © 2016 Pearson Education, Inc., Hoboken NJ 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.
So that being said what I have done is created an AverageDriver class as instructed which creates a new instance of the Average class. When I run the main method no errors occur but the constructor does not ask for any user input, it just skips past the constructor and displays an array of 5 zeros.
AverageDriver code:
public class AverageDriver
{
public static void main(String[] args)
{
Average calcAverage = new Average();
System.out.print(calcAverage.toString());
}
}
and Average code here:
import java.util.Scanner;
import java.util.Arrays;
public class Average
{
final int GRADES = 5;
private int[] data = new int[GRADES] ;
private double mean = 0;
public void Average() {
Scanner keyboard = new Scanner(System.in);
for(int i=0; i<5; i++)
{
System.out.print("Enter grade number "
+ (i+1) + ": ");
data[i] = keyboard.nextInt();
}
selectionSort();
calculateMean(); }
public void selectionSort()
{
Arrays.sort(data);
}
public void calculateMean()
{
for(int i=0; i<5; i++)
mean += data[i];
mean = mean/5;
}
public String toString()
{
String stringData = Arrays.toString(data);
String stringMean = Double.toString(mean);
return stringData;
}
}