Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Just trying to run through some code for an assignment I'm doing. It is probably simple but for the life of me I can't figure out why I get the above error at the first line

(public WaterLog.......). 

Later I want to pass it this line:

[ log = new WaterLog(8, damCapacity); ]

Any help would be appreciated, I am new to this sorry.

public class WaterLog(Integer windowSize, Integer maxEntry) {

private Integer size = windowSize;
private Integer max = maxEntry;
private ArrayList theLog(int windowSize);
private int counter = 0;


public void addEntry(Integer newEntry) throws SimulationException {

    theLog.add(0, newEntry);
    counter++;

}

public Integer getEntry(Integer index) throws SimulationException {

    If (thelog.isEmpty() || thelog.size() < index) {
        return null;
    }
    return thelog.get(index);

}

public Integer variation() throws SimulationException {

    int old, recent = 0;
    recent = thelog.get(0);
    old = thelog.get(thelog.size-1);
    return recent-old;
}

public Integer numEntries() {

    return counter;

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
188 views
Welcome To Ask or Share your Answers For Others

1 Answer

Assuming SimulationException is defined correctly:

class WaterLog{

private Integer size;
private Integer max ;
private ArrayList<Integer> theLog; //parameterize your lists
private int counter = 0;

public WaterLog(Integer windowSize, Integer maxEntry) //this is the behavior you were    looking for
{
this.size = windowSize;
this.max = maxEntry;
theLog = new ArrayList<Integer>(windowSize);
}

public void addEntry(Integer newEntry) throws SimulationException {

theLog.add(0, newEntry);
counter++;

}

public Integer getEntry(Integer index) throws SimulationException {

if (theLog.isEmpty() || theLog.size() < index) { //Java is case sensitive
    return null;
}
return theLog.get(index);

}

public Integer variation() throws SimulationException {

int old, recent = 0;
recent = theLog.get(0);
old = theLog.get(theLog.size()-1); //again, watch case, also size is a method
return recent-old;
}

public Integer numEntries() {

return counter;

}

}

See the comments I added.

EDIT: To explain a bit further what was going on, let's take a look at what you were doing.

public class WaterLog(Integer windowSize, Integer maxEntry) {

private Integer size = windowSize;
private Integer max = maxEntry;
private ArrayList theLog(int windowSize);
private int counter = 0;

You seem to have confused a class with a constructor. The variables you defined were attributes, which was correct. You needed to use the syntax I showed in my answer to create a constructor. For that same reason, you don't have access to variables like windowSize. To remedy this, we allow them to still be defined outside the constructor, but assigned values inside it, where we have access to windowSize and maxEntry.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...