-2

The version of java: SDK 1.8.0_151

IDE: IDEA IntelliJ

import java.awt.Queue;
import java.util.LinkedList;

public class SimpleMovingAverage {
    private final Queue<Double> window = new LinkedList<Double>();
    private final int period;
    private double sum;

    public SimpleMovingAverage(int period) {
        assert period > 0 : "Period must be a positive integer";
        this.period = period;
    }

    public void newNum(double num) {
        sum += num;
        window.add(num);
        if (window.size() > period) {
            sum -= window.remove();
        }
    }

    public double getAvg() {
        if (window.isEmpty()) {return 0.0;} // technically the average is undefined
        return sum / window.size();
    }

    public static void main(String[] args) {
        double[] testData = {1, 2, 3, 4, 5, 5, 4, 3, 2, 1};
        int[] windowSizes = {3, 5};
        for (int windSize : windowSizes) {
            SimpleMovingAverage ma = new SimpleMovingAverage(windSize);
            for (double x : testData) {
                ma.newNum(x);
                System.out.println("Next number = " + x + ", SMA = " + ma.getAvg());
            }
            System.out.println();
        }
    }
}

The code above is coming from https://rosettacode.org/wiki/Averages/Simple_moving_average#Java

When I create a Class, called SimpleMovingAverage and copy the code from the above website, an error is reported.

'java.awt.Queue' is not public in 'java.awt'. Cannot be accessed from outside package

How to solve it?

user207421
  • 305,947
  • 44
  • 307
  • 483
Luk Aron
  • 1,235
  • 11
  • 34

1 Answers1

1

You need java.util.Queue not java.awt.Queue, which can hold whatever you want

The java.awt package is about UI, graphics and images: Documentation, and the java.awt.Queue is here to hold java.awt.Event elements


For Improvement ONLY : for the implementation of a Circular FIFO, here some infos

Which could give something like

public void newNum(double num) {
    window.add(num);
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • You keep breaking the `newNum()` method. – user207421 Oct 29 '19 at 09:56
  • *If* he uses a `CircularQueue`, wherever he's going to find that. He's having enough trouble finding `java.util.Queue`. I fail to see the relevance. This is a simple import problem. Don't gussy it up. – user207421 Oct 29 '19 at 10:05
  • You don't know who downvoted, let alone why, but there have been enough mistakes and irrelevance in this answer to justify a downvote. – user207421 Oct 29 '19 at 10:10
  • @user207421 So there'is no place for mistakes, improvement and goodwill ? Ok nice – azro Oct 29 '19 at 10:11