Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

java - Syntax error, telling me it wants ; and several other things

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...