Saturday 15 March 2014

Using the observer class in Java -



Using the observer class in Java -

so supposed create class belt observable 2 observers, i'm not sure how , guides have found far confuse me. issue i'm having difficulty wrapping head around set these observable methods. having issue because problem asks me utilize instance of class plateevent object describes change. help great, , here question , code:

you need modify implementation of belt extends java.util.observable. whenever plate placed on or removed belt, should arrange notify registered observers of change. utilize instance of plateevent (described below) object describes change.

plateevent defines object passed sec parameter update() method when observers of belt notified plate has been placed or removed. within plateevent enumeration called eventtype defines 2 symbols: plate_placed , plate_removed. plateevent object encapsulates 1 of these 2 types, reference plate placed or removed, , position occurs. should read through plateevent.java create sure understand class , how build new instances, should not have need modify code there.

package a6adept; import java.util.nosuchelementexception; import comp401.sushi.plate; public class belt extends java.util.observable { plate[] belt; public belt(int size) { if (size <= 0) { throw new illegalargumentexception("illegal belt size"); } belt = new plate[size]; } public int getsize() { homecoming belt.length; } public plate getplateatposition(int position) { homecoming belt[correct_position(position)]; } public void setplateatposition(plate plate, int position) throws beltplateexception { if (plate == null) { throw new illegalargumentexception(); } if (getplateatposition(position) != null) { throw new beltplateexception(position, plate, this); } else { belt[correct_position(position)] = plate; } } public void clearplateatposition(int position) { belt[correct_position(position)] = null; } public plate removeplateatposition(int position) { plate plate_at_position = getplateatposition(position); if (plate_at_position == null) { throw new nosuchelementexception(); } clearplateatposition(position); homecoming plate_at_position; } public int setplatenearesttoposition(plate plate, int position) throws beltfullexception { (int offset=0; offset < getsize(); offset++) { seek { setplateatposition(plate, position+offset); homecoming position+offset; } grab (beltplateexception e) { } } throw new beltfullexception(this); } public void rotate() { plate last_plate = belt[getsize()-1]; (int i=getsize()-1; i>0; i--) { belt[i] = belt[i-1]; } belt[0] = last_plate; } private int correct_position(int position) { if (position < 0) { homecoming ((position%getsize())+getsize())%getsize(); } homecoming position%getsize(); } } bundle a6adept; import comp401.sushi.plate; public class plateevent { public enum eventtype {plate_placed, plate_removed}; private eventtype type; private plate plate; private int position; public plateevent(eventtype type, plate plate, int position) { this.type = type; this.plate = plate; this.position = position; } public eventtype gettype() { homecoming type; } public plate getplate() { homecoming plate; } public int getposition() { homecoming position; } }

any time plate placed on or removed belt, need build plateevent object appropriate attributes, , phone call notifyobservers(plateevent).

you don't need else, since observable provides mechanism other objects register observe object.

java

No comments:

Post a Comment