Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class FixedMessageSequenceProtocol {
- static int currentPopulationRequest ;
- public FixedMessageSequenceProtocol (int step) {
- this.currentPopulationRequest = step ; //adding one because, we want to move to the next state
- }
- private static final int WAITING = 0;
- private static final int SENTPERMISSION = 1;
- private static final int SENTCLUE = 2;
- private static final int ANOTHER = 3;
- private static final int NUMSTATES = 50;
- private int state = WAITING;
- private int currentState = getCurrentState(); // get this from the Constructor;
- private String[] clues = { "What is Alabama population",
- "What is Alaska population",
- "What is Arizona population", //2
- "What is Arkansas population",
- "What is California population",
- "What is Colorado population",
- "What is Connecticutt population",
- "What is Delaware population",
- "What is Florida population", //8
- "What is Georgia population" ,
- "What is Hawaii population",
- "What is Idaho population",
- "What is Illinois population",
- "What is Indiana population" ,
- "What is Iowa population",
- "What is Kansas population",
- "What is Kentucky population",
- "What is Louisiana population",
- "What is Maine population",
- "What is Maryland population",
- "What is Massachusetts population",
- "What is Michigan population",
- "What is Minnesota population",
- "What is Mississippi population",
- "What is Missouri population",
- "What is Montana population",
- "What is Nebraska population",
- "What is Nevada population",
- "What is New Hampshire population",
- "What is New Jersey population",
- "What is New Mexico population",
- "What is New York population",
- "What is North Carolina population",
- "What is North Dakota population",
- "What is Ohio population",
- "What is Oklahoma population",
- "What is Oregon population",
- "What is Pennsylvania population",
- "What is Rhode Island population",
- "What is South Carolina population",
- "What is South Dakota population",
- "What is Tennessee population",
- "What is Texas population",
- "What is Utah population",
- "What is Vermont population",
- "What is Virginia population",
- "What is Washington population",
- "What is West Virginia population",
- "What is Wisconsin population",
- "What is Wyoming population"
- };
- private String[] answers = { "Alabama has a pop. of 4,889,232",
- "Alaska has a pop. of 736,232",
- "Arizona has a pop. of 6,731,232",
- "Arkansas has a pop. of 2,966,112",
- "California has a pop. of 38,800,232",
- "Colorado has a pop. of 5,359,232",
- "Connecticutt has a pop. of 3,574,097",
- "Delaware has a pop. of 897,934",
- "Florida has a pop. of 18,801,310",
- "Georgia has a pop. of 9,687,653",
- "Hawaii has a pop. of 1,360,301",
- "Idaho has a pop. of 1,567,582",
- "Illinois has a pop. of 12,830,632",
- "Indiana has a pop. of 6,483,802",
- "Iowa has a pop. of 3,083,226",
- "Kansas has a pop. of 2,883,802",
- "Kentucky has a pop. of 4,333,802",
- "Louisiana has a pop. of 4,533,802",
- "Maine has a pop. of 1,333,802",
- "Maryland has a pop. of 5,773,802",
- "Massachusetts has a pop. of 6,547,802",
- "Michigan has a pop. of 9,883,112",
- "Minnesota has a pop. of 5,307,621",
- "Mississippi has a pop. of 2,967,702",
- "Missouri has a pop. of 5,988,412",
- "Montana has a pop. of 989,102",
- "Nebraska has a pop. of 1,827,102",
- "Nevada has a pop. of 2,700,402",
- "New Hampshire has a pop. of 1,316,802",
- "New Jersey has a pop. of 8,791,442",
- "New Mexico has a pop. of 2,059,332",
- "New York has a pop. of 19,378,282",
- "North Carolina has a pop. of 9,547,802",
- "North Dakota has a pop. of 670,802",
- "Ohio has a pop. of 11,547,332",
- "Oklahoma has a pop. of 3,707,802",
- "Oregon has a pop. of 4,547,802", // check
- "Pennsylvania has a pop. of 7,247,802",
- "Rhode Island has a pop. of 1,547,802",
- "South Carolina has a pop. of 1,147,802",
- "South Dakota has a pop. of 547,802",
- "Tennessee has a pop. of 5,547,802",
- "Texas has a pop. of 22,547,802",
- "Utah has a pop. of 2,547,802",
- "Vermont has a pop. of 2,007,802",
- "Virginia has a pop. of 10,547,802",
- "Washington has a pop. of 7,547,802",
- "West Virginia has a pop. of 2,117,802",
- "Wisconsin has a pop. of 4,222,112",
- "Wyoming has a pop. of 747,802"
- };
- public String processInput(String theInput) {
- String theOutput = null;
- if (state == WAITING) {
- theOutput = "May I have permission to ask you to send a state-population request?";
- state = SENTPERMISSION ;
- } else if (state == SENTPERMISSION) {
- if (theInput.equalsIgnoreCase("Permission granted.")) {
- theOutput = "Provide a state-population query.";
- state = SENTCLUE;
- } else {
- theOutput = "You're supposed to say \"Permission granted. \"! " +
- "Try again. Request to send a state population";
- }
- } else if (state == SENTCLUE) {
- if (theInput.equalsIgnoreCase(clues[currentPopulationRequest])) {
- theOutput = answers[currentPopulationRequest] + " Want another? (y/n)";
- currentPopulationRequest++;
- state = ANOTHER ;
- } else {
- theOutput = "You're supposed to say \"" +
- clues[currentPopulationRequest] +
- "! Try again. Request to send a state population";
- state = SENTPERMISSION;
- }
- } else if (state == ANOTHER) {
- if (theInput.equalsIgnoreCase("y")) {
- theOutput = "Request to send a state population";
- if (currentPopulationRequest == NUMSTATES)
- currentPopulationRequest = 0;
- else
- currentState++;
- state = SENTPERMISSION;
- } else {
- theOutput = "Bye.";
- state = WAITING;
- }
- }
- return theOutput;
- }
- public int getCurrentState(){
- return currentPopulationRequest;
- }
- }// end class FixedMessageSequenceProtocol
Add Comment
Please, Sign In to add comment