Thursday 15 July 2010

java - How can I utilize my Event class methods in bankSim class? -



java - How can I utilize my Event class methods in bankSim class? -

when compile banksim class, error: non-static method setwhichq(int) cannot referenced static context. can reference event class methods in banksim class? below processarrival error occurs. after entire event class.

public void processarrival(arrival arrevent, arrival[] inputdata, int inputdataindex, sortedlist<event> eventlist, queuedsvector<arrival> teller1, queuedsvector<arrival> teller2) { boolean atfront1 = teller1.isempty(); // 1 here? boolean atfront2 = teller2.isempty(); if (atfront1) { // if no other customers, served departure newdep = new departure(arrevent.getarrtime(), arrevent); // because customer's next event departure eventlist.insert(newdep); // set departure eventlist } // end if else if (atfront2) { // if no other customers, served departure newdep = new departure(arrevent.getarrtime(), arrevent); // because customer's next event departure eventlist.insert(newdep); // set departure eventlist } // end if else if ( teller1.size()< teller2.size() ) { //queue of teller 1 less teller 2 teller1.enqueue(arrevent); // set new client bank line wait event.setwhichq(1); } else if (teller2.size() < teller1.size()) { teller2.enqueue(arrevent); event.setwhichq(2); } public abstract class event implements comparable<event> { protected int whichq; public event() { whichq = 0; } public int compareto (event other) { int thistime = this.gettime(); int othertime = other.gettime(); homecoming (thistime - othertime); } public int getwhichq() { homecoming whichq; } // public void setwhichq(int q) { if (q >= 2) whichq = 2; else if (q<=1) // < 0 = 0 whichq = 1; else whichq = q; } public abstract int gettime(); public abstract string tostring(); } // end event class

the problem appears you're calling event.setwhich(), referencing event type, not event object. in order utilize non-static method, have first instantiate object:

event test = new event();

then phone call method on instantiated object:

test.setwhichq(1);

likewise, if wanted utilize setwhichq() method you've indicated above, you'd have alter event static class, , create whichq static field. in case, event.setwhichq() called reference class itself, , likewise modify whichq.

java static-methods

No comments:

Post a Comment