How does a state transition table work? [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How does a state transition table work? [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want to try making a state machine, but I am fairly new to programming. I read something about a state transition table to switch between states if you input something. I do not quite understand how that works and how it translates to Java code. Can anyone please explain?

Answer

A state machine refers to a program that tracks states relative to user input.

I want to be able to input a string and then the progam gives a message and the state changes. For example: “do work”, the program says:”going to work” and changes state. after a while it says”done with working” and the state changes back. kind of like a very small game

Using your example from the comments, we could do something like;

import java.util.Scanner;

enum GoingState {
   TO_HOME,
   TO_WORK,
   TO_SHOP, 
}

public class StateGame{

    public static GoingState state = GoingState.TO_WORK;

    public static void main(String args[]){
        Scanner scanIn = new Scanner(System.in);
        System.out.println("Where do you want to go?");
        if(scanIn.nextLine().toLowerCase().contains("home")){
            state = GoingState.TO_HOME;
            System.out.println("Going home.");
        }
        else if(scanIn.nextLine().toLowerCase().contains("work")){
            state = GoingState.TO_WORK;
            System.out.println("Going to work.");
        }
        else if(scanIn.nextLine().toLowerCase().contains("shop")){
            state = GoingState.TO_SHOP;
            System.out.println("Going to the shop.");
        }
        else{
            System.out.println("No valid input");
        }
    }
}

In the above example I am using enums to track state, but it could just as easily be an int or String, or an Object that tracks the state. This, of course, is an incredibly over simplified example.

State machines usually track which state transitions are possible (which I haven’t done) using a state transition map (or table), which tell the program whether it can or should change from one type of state to another type of state.

A very simple example of a state map could be where there is only a linear progression to your states, ie in the example above only being able to go from home to work, work to the shops, and back again without being able to go directly from the shops to home or vice versa. Tracking this could be fairly simple, because you could put the states in an array and check if where the user wanted to go was one above or below the current state in the array.

I hope this helps.

We are here to answer your question about How does a state transition table work? [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji