-1

We were given this specification for the PercolationStats class:

public class PercolationStats {
   public PercolationStats(int N, int T)    // perform T independent computational experiments on an N-by-N grid
   public double mean()                     // sample mean of percolation threshold
   public double stddev()                   // sample standard deviation of percolation threshold
   public double confidenceLo()             // returns lower bound of the 95% confidence interval
   public double confidenceHi()             // returns upper bound of the 95% confidence interval
   public static void main(String[] args)   // test client, described below
}

and to implement mean() and stddev(), we had to use a special library that has a class called StdStats:

public final class StdStats {

private StdStats() { }

/* All methods declared static. */

}

I tried to write something like

public mean() {

return StdStats.mean();

}

but I get the following error:

Cannot make a static reference to the non-static method mean() from the type PercolationStats

Here is what's presumably generating it:

main() {

/* ... */

        System.out.println("-- Summary --\n");
        System.out.printf("mean\tstdev\t[lo\thi]\n\n");
        System.out.printf("%1.3f\t%.3f\t%.3f\t%.3f", PercolationStats.mean(), 
                          PercolationStats.stddev(), PercolationStats.confidenceLo(), PercolationStats.confidenceHi());

        System.out.println("-- End --");
}

Is there a way to get rid of this error without changing the specification? I believe we're supposed to be able to make PercolationStats objects. Thanks for any help!

user1505713
  • 615
  • 5
  • 20
  • could you show the code that's inside your `main` method? – Jeroen Vannevel Aug 31 '13 at 22:45
  • 2
    _we're supposed to be able to make PercolationStats objects_ Yes, do that and call the method on one of those objects. – Sotirios Delimanolis Aug 31 '13 at 22:45
  • 1
    StdStats is a class name. `mean` is presumably an instance method of that class. You need to create an instance of StdStats (perhaps via some sort of factory) and use that to call `mean`. But note that you no doubt need to first provide some data to that instance for it to have something to calculate the mean of. – Hot Licks Aug 31 '13 at 22:48
  • (Show us the definition of the version of `mean` in StdStats.) – Hot Licks Aug 31 '13 at 22:50
  • From the error _type PercolationStats_ it would seem that you are accessing the method `mean` in your class rather than in the `StdStats` class. – Boris the Spider Aug 31 '13 at 22:52
  • Edited to show line that generates error. – user1505713 Aug 31 '13 at 22:53
  • You need to instantiate a `PercolationStats` in order to call its `mean()` method, only `StdStats` has a static version--your definition above doesn't. – Dave Newton Aug 31 '13 at 22:58
  • **Show us the definition of the version of mean in StdStats.** – Hot Licks Sep 01 '13 at 01:57

1 Answers1

1

You cannot access a non-static method from your main method. Create a new object and perform your operations in that constructor.

class PercolationStats {
 public static void main(String[] args){
  new PercolationStats ();
 }

 public PercolationStats() {
  System.out.println("-- Summary --\n");
  System.out.printf("mean\tstdev\t[lo\thi]\n\n");
  System.out.printf("%1.3f\t%.3f\t%.3f\t%.3f", mean(), 
                          stddev(), confidenceLo(), confidenceHi());

  System.out.println("-- End --");
 }
}

I have adjusted it so it uses your class names. You were trying to access non-static methods from a static method which is impossible. When you access a static method, no non-static methods are available to the static method at that point and will always result in an error. You had to separate those.

Well, you could have also created a PercolationStats object in your main method and used everything there, but this keeps it concise.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170