Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Classes //at top of file to allow use of JOptionPane: import javax.swing.JOptionPane; //inside class public String promptUser(String sQuestion){ JOptionPane oQuestion = new JOptionPane(); String sAnswer

  1. Classes
    1. //at top of file to allow use of JOptionPane:

      import javax.swing.JOptionPane;

      //inside class

      public String promptUser(String sQuestion){

      JOptionPane oQuestion = new JOptionPane();

      String sAnswer = oQuestion.showInputDialog(sQuestion);

      return sAnswer;

      }

      1. Method: public method called sleepRandomTime.
        1. NOTE: This whole method is given to you in this weeks module, so you can download it there.
        2. Input: String for thread name.
        3. Output: void.
        4. Description:
          1. Get random int from SecureRandom class between 0 and 5, and then add 3 to it.
          2. Print out how many seconds the thread is going to sleep for.
          3. Call below sleep method passing in the random int (which will range between 3 and 7).
      2. Method: public method called sleep.
        1. Input: int for seconds.
        2. Output: void.
        3. Description:
          1. Make a try-catch statement
            1. Inside the try, sleep the thread, which means you must multiply the long variable by 1000.
            2. No need to do anything in catch statement, just as demoed in class.
      1. Util
        1. This will have helper functions that all the classes can use.
        2. Method: Public method called getMerkleRoot.
          1. Inputs:
            1. ArrayList type for the list of words from MerkleThread.
          2. Description:
            1. Not much description here since we did this in class.
            2. Creates 7 MerkleNode objects and slowly builds tree to get merkle root.
        3. Method: Private method called populateMerkleNode.
          1. Inputs:
            1. Three MerkleNode types: first is to be populated, second is to be the left node of the first node, and third is to be the right node of the first node.
            2. Desctiption:
            3. Not much description because was reviewed in class.
              1. Basically sets hash, left, and right node values.
        4. Method: generateHash
          1. NOTE: This method is given to you on Canvas in this weeks module and in class.
        5. Method: public method called promptUser.
          1. Input: String of question to ask user.
          2. Output: String answer from user.
          3. Description: Use JOptionPane class to get user input so that the user doesnt have to type while all the other text is being printed out.
            1. Code provided here for getting user input in case you havent used JOptionPane before:
      1. MerkleNode
        1. Three instance variables:
          1. sHash is a String.
          2. oLeft is a MerkleNode.
          3. oRight is a MerkleNode.
    2. System.exit(0);

      1. Else if MerkleManager.iStrikes equals 3 then print out 3 strikes: you lost! or something like that and exit the app as shown in line of code above.
      2. After the if-else statement, then call sleep on util object and sleep for 1 second.
        1. (if you dont do this, the endless loop never allows updates on that thread to see MerkleManagers changing values.)
      1. MonitorThread
        1. Method: run method just as in the other Thread classes.
          1. Create endless loop as youve done above.
            1. If MerkleManager.sMerkleRoot is not null then
              1. If the above merkle root equals the initial user-entered merkle root (which you can access the same way as above since theyre both static on MerkleManager):
                1. Then print out You win: followed by the merkle root (which is the above static variable on MerkleManager) and exit the app.
              2. Else if theyre not equal, then tell the user and that the user lost and exit the app.
                1. To exit:
      1. RogueThread
        1. This class must implement Runnable.
        2. Method: A public run method.
          1. No inputs or outputs.
          2. Instantiate a Util object.
          3. Create a neverending loop again just like youve done before.
            1. Inside loop, do all the following:
              1. Call sleepRandomTime on util variable.
              2. Call grabWord similar to how described in MerkleThread.
              3. If sNewWord is not null:
                1. Increase iStrikes static variable on MerkleManager by 1.
                2. Print out to screen that rogue grabbed a word and mention STRIKE!
    3. String sNewWord = MerkleManager.grabWord();

      1. Then if sNewWord is not null:
        1. Print out that Merkle grabbed a word.
        2. Add word to lstWords like this: lstWords.add(sNewWord);
        3. Check if lstWords.size() is equal to iMerkleTreeInputs.
        4. And if true, then set MerkleManager.sMerkleRoot to the merkle root generated by the getMerkleRoot method on util class.
      1. MerkleThread
        1. This class must implement Runnable.
        2. Instance variables:
          1. Public & static & volatile ArrayList that holds all the grabbed words.
            1. Example:
              1. public static volatile ArrayList lstWords;
          2. Private int called iMerkleTreeInputs for how many words to wait for before creating a merkle tree.
            1. Set it equal to 4.
        3. Method: A public run method that is triggered when start is called from MerkleManager.
          1. No inputs or outputs.
          2. Instantiate a Util class object (Util class is defined lower down).
          3. Instantiate the ArrayList instance variable.
            1. Example:
              1. lstWords = new ArrayList();
            2. Create a neverending while loop like this:
              1. while(true){
              2. In loop, do all the following:
                1. Call sleepRandomTime on util variable.
                2. Cal grabWord on MerkleManager.
                  1. Example of how to call static method:
    4. H6: Merkle root

      / \

      H4 H5

      / \ / \

      H0 H1 H2 H3

      | | | |

      word1 word2 word3 word4

          1. if your words were word1, word2, word3, word4, then you would enter word1 into the above web page and get the hash (H0),
          2. Then do the same with the others.
          3. Then combine each pair of hashes, so the hash of word1 (H0) together with the hash of word2 (H1), and get the hash of that long string (H4). Youll do that twice, once for the first two (H0 & H1) and once for the second two (H2 & H3). So you now have H4 & H5.
          4. Finally, then combine those resulting two hashes (H4 & H5) into one string in the web page entry to produce another hash this is your merkle root hash.
          5. Copy this merkle root from the web page and paste into your user entry when prompted in the consol
        1. Start 3 separate threads: Instantiate and start new threads for
          1. MerkleThread, RogueThead, MonitorThread.
        2. Begin UI question Loop:
          1. Just use while(true) to make an eternal loop (because the monitor will eventually close the app).
          2. Ask user for a word and put it into instance variable for users word.
      1. Method: A public & static & synchronized method called grabWord.
        1. No inputs but returns a String.
        2. Puts instance variable of users word into a temp String variable and then makes the instance variable null.
        3. Then returns the temp variable.
      1. NOTE: Use https://www.xorbin.com/tools/sha256-hash-calculator to figure out merkle root of the four words you expect to use just like we do in the code:
        1. EXAMPLE:
      1. MerkleManager
        1. Instance variables:
          1. Public & static & volatile String for Users entered word.
          2. Public & static String for user entered expected merkle root.
          3. Public & static String for merkle root set to null.
          4. Public & static int for strikes set to 0.
        2. Method: A public "manage" method and this will be called by the MerkleManager_Test class's main method.
          1. No inputs or outputs.
          2. Use Util class (that youll create later on below) to prompt user for the expected merkle root (The merkle root of the four words the user will enter later as specified down below in the UI Menu Loop).
            1. Put user entered text into the instance variable to hold for end of game comparison.
      1. MerkleManager_Test
        1. Will have main method and will instantiate instance of MerkleManager and call manage function... that's it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions

Question

Will you be able to pay your bills?

Answered: 1 week ago

Question

Define Scientific Management

Answered: 1 week ago

Question

Explain budgetary Control

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago