0

I have a problem that goes like this. A user inputs 5 grades into an array. The array must then be sorted from highest to lowest and then averaged.

the methods for the selectionSort and calculateMean must be void.

How do i call them into my constructor? then i need to print the data out to a toString.

This is what i have so far.

import java.util.Scanner;

public class Average {

//the array which will contain the scores
private int data[];
//the average of the scores
private double mean;



public Average(){
    Scanner sc = new Scanner(System.in);

    double data[] = new double[6];

    for(int i = 1; i < data.length; i++){
        System.out.println("Enter score number " + i);
        data[i] = sc.nextDouble();

    }

    /*for(int p = 1;  p < data.length; p ++){
        System.out.println(data[p]);
    }*/
    //selectionSort();




}
public void calculateMean(){
    double total = 0;
    double sum = 0;
    double average = 0;

            for(int counter = 0; counter < data.length; counter++){
                sum = sum + data[counter];
                average = sum / data.length; 

            }

}


public void selectionSort(){
    int temp = 0;
    for(int joey = 0; joey<data.length; joey++){    
        for(int  i = 1; i < data.length; i++) {
            if(data[i - 1] > data[i]) {
            temp = data[i-1];
            data[i-1] = data[i];
            data[i] = temp;
            }
        }
    }
         for(int p = 0; p < data.length; p++){
            System.out.println(data[p]);

    }


    }   
public String toString(){



    return null;
}
}
2
  • Why the downvote???.. This guy has tried something... Commented Dec 2, 2013 at 6:38
  • It's not quite clear what you mean by "call into my constructor". Note that usually, the constructor just initializes the variables for the class, and code that does things like getting user input goes in other methods (or main for a simple program like this one). Commented Dec 2, 2013 at 6:41

1 Answer 1

1

You can call the 2 methods in your constructor like this

// Inside constructor
selectionSort(); // call the method
calculateMean(); // call the method
System.out.println(toString()); // print the data using toString

And your toString() can look like this

public String toString() {
    return Arrays.toString(data);
}

Also, the logic in your calculateMean() is a bit wrong.

for (int counter = 0; counter < data.length; counter++) {
    sum = sum + data[counter];
}
average = sum / data.length; // Calculate the average after finding the sum and not at every iteration.

Moreover, I see that the mean instance variable is not. You might want to assign the average calculated in the calculateMean() method to it, and you might want to print it some method or the toString() itself, depending in your needs.

Edit:

You're shadowing your instance variable int data[]; with the local variable in the constructor, double data[] = new double[6]; and that is the reason you're getting the NPE. Remove the local variable from the constructor and instead initialize it like this

data[] = new int[5];

And the following for loop to get the input from the user to start from the index 0 and not 1.

for(int i = 0; i < data.length; i++){
Sign up to request clarification or add additional context in comments.

5 Comments

Or System.out.println(this); if he overrides toString() appropriately.
@chrylis - Yeah ofcourse. But I thought since the OP wanted to call the methods, it'd be better to print the data by calling the toString() explicitly than by implicitly calling it by just using this.
@R.J ok, so I have the methods called and the toString done. But how exactly do I getting the result of calculateMean() stored into private double mean; ?
@user3056312 - after calculating the average, within the method itself, you can do, mean = average;.
@RJ wow that makes me feel dumb. Ok, so whenever i do that i get this. Exception in thread "main" java.lang.NullPointerException at Average.calculateMean(Average.java:44) at Average.<init>(Average.java:31) at AverageDriver.main(AverageDriver.java:7)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.