CS代写 Disclaimer

Disclaimer
Introduction to State Machines
This is a practical introduction to the concept of state machines to support your programming needs for this course. This is not intended to be a formal tutorial, rigorous, or complete.
In ordinary life context often a􏰃ects how we respond to stimuli. For example, at some point you might realize you feel tired. If you are in bed with your head on a pillow, your response might be to close your eyes and go to sleep. However, if you are driving a car or 􏰇ying a plane, hopefully you would choose a di􏰃erent response to that same feeling. In the case where you’re driving, you might choose to change your context at the soonest opportunity, perhaps you would 􏰄nd a place to stop and then in that context take a nap. In the case where you’re 􏰇ying a plane, where you might not have the option of pulling over for a rest, you might respond by drinking a cup of co􏰃ee.

Copyright By PowCoder代写 加微信 powcoder

For writing programs, a similar pattern often helps work through the organization of a solution to a complicated problem. A 􏰀state machine􏰁 is a section of the code that has multiple states (contexts) and performs an action for each input. Those actions may include transitioning to another state.
Here is little bit of the code matching the sleepiness example. (See below for an explanation of the switch-case statements used in this code.)
while(input) {

switch(state) {
switch(input) {
case SLEEPY:
close_eyes()
state = SLEEPING;
case HUNGRY:
state = KITCHEN;
case FLYING:
switch(input) {
case SLEEPY:
drink_coffee(); // Failure of this operation may result in a crash
case HUNGRY:
Going back to the sentiment analysis task you implemented in an earlier assignment, you needed to process the score and tokens of a line from a 􏰄le. Part of a state machine for such a task might is presented below. (See below for an explanation of the enum statement.)
enum STATES { START, SIGN, SCORE, TEXT, WORD, NON_WORD }
state = STATES.START;
score = 1;
for(char c: line.toCharArray()) {
switch(state) {
case START:
switch(c) {

score = -1;
state = STATES.SIGN;
score = c – ‘0’;
state = STATES.SCORE;
case SIGN:
switch(c) {
score *= c – ‘0’;
state = STATES.SCORE;
case SCORE:
if (Character.isWhiteSpace(c))
state = STATES.TEXT;
else fail();
case TEXT:
if (Character.isWhiteSpace(c))
else if(Character.isLetter(c)) {
cur_word.append(c);
state = STATES.WORD;
state = NON_WORD;

case NON_WORD
if (Character.isWhiteSpace(c))
state = STATES.TEXT;
if (Character.isWhiteSpace(c))
state = STATES.TEXT;
cur_word.append(c);
Note: this is still pseudocode, and not intended to be complete.
Switch Statement
Switch statements are a compact way of branching for multiple possible values of a variable. They are more compact than writing many if-else statements.
Instead of writing:
if(a == 0) {
// code for 0
} else if(a == 1) {
// code for 1
} else if(a == 2) {
// code for 2
// code for any other value of a
You can write:
switc(a) {
case 0: // code for 0

case 1: // code for 1
case 2: // code for 2
default: // code for any other value of a
It might not look simpler at 􏰄rst, but your perspective may shift with experience. Aside from the visual appeal, switch is usually considered1 more e􏰅cient than a long sequence of if-then-else. The optional break statements also let you de􏰄ne control 􏰇ows that are not easy to do with if-then-else. Also, when used with enumerations, code checkers (such as those run in the background in Eclipse and IntelliJ) may warn if you forget to cover all cases.
See: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Enumerations
The enum statement may be used to compactly de􏰄ne a set of [usually] related constants. The STATES enumeration in the example could be replaced with a bunch of individually declared and de􏰄ned constants:
static final int START = 0;
static final int SIGN = 1;
static final int SCORE = 2;
// multiple declaration form:
static final int TEXT = 3, WORD = 4, NON_WORD = 5;
Enumerations have the added bene􏰄t that they limit values to just those de􏰄ned, whereas those constantscanbeusedwithint score,butthereisnothingpreventingusfromaccidentallyassigning other values to score. In Java enumerations can act mostly like classes, the main di􏰃erence being that all instances must be de􏰄ned in the class declaration.
See: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
We can’t rule out the possibility that the compiler or runtime optimizer will automatically convert the if-then-else chain into a switch for you.

A Note on the Topic of Regular Expressions
If you think you can avoid state machines by using regular expressions, think again. Regular expression are equivalent to a class of state machines called 􏰀non-deterministic 􏰄nite automata􏰁 (NFA). A common approach to making a regular expression engine is to compile an expression into a state machine at runtime. Those implementations may not be as e􏰅cient as a custom state machine written speci􏰄cally for your task.
Experience explicitly writing state machines can also be bene􏰄cial to thinking through writing regular expressions.

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com