Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/State1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kz.edu.nu.cs.exercise;

public class State1 extends State{
protected StateContext sc;
protected boolean accept = false;

public State1(StateContext stateContext) {
this.sc = stateContext;
}
public void actionA() {
this.sc.setCurrentState(this.sc.getState2());
}

public void actionB() {
// no change of the state
}

public boolean isAccept() {
return this.accept;
}
}

23 changes: 23 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/State2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kz.edu.nu.cs.exercise;

public class State2 extends State{
protected StateContext sc;
protected boolean accept = false;

public State2(StateContext stateContext) {
this.sc = stateContext;
}
public void actionA() {
this.sc.setCurrentState(this.sc.getState3());
}

public void actionB() {
this.sc.setCurrentState(this.sc.getState1());
}

public boolean isAccept() {
return this.accept;
}
}


22 changes: 22 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/State3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kz.edu.nu.cs.exercise;

public class State3 extends State{
protected StateContext sc;
protected boolean accept = true;

public State3(StateContext stateContext) {
this.sc = stateContext;
}
public void actionA() {
// no change of the state
}

public void actionB() {
this.sc.setCurrentState(this.sc.getState2());
}

public boolean isAccept() {
return this.accept;
}

}
14 changes: 13 additions & 1 deletion src/main/java/kz/edu/nu/cs/exercise/StateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ public StateContext() {
public void actionA() {
// complete this method by
// delegation to the current state
this.currentState.actionA();
}

public void actionB() {
// complete this method
// delegate to the current state
this.currentState.actionB();
}

public boolean inAcceptState() {
// complete this method and return correct value
// delegate to the current state
return false;
return currentState.isAccept();
}

public State getCurrentState() {
Expand All @@ -35,4 +37,14 @@ public void setCurrentState(State currentState) {
this.currentState = currentState;
}

public State getState1() {
return state1;
}
public State getState2() {
return state2;
}
public State getState3() {
return state3;
}

}
16 changes: 16 additions & 0 deletions src/main/java/kz/edu/nu/cs/exercise/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kz.edu.nu.cs.exercise;

public class Test {

public static void main ( String [] args ) {
StateContext sc = new StateContext ();
sc . actionA ();
sc . actionA ();
// sc . actionB ();
// Is machine in an accept state
// after receiving AAB
System.out.println(sc.inAcceptState());
// sc . inAcceptState ();
}

}