Question

Complate this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

Complate this in java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class WordDetective {

/**
* Reorders the letters in word.
*
* Algorithm:
* Copy the word into an array of characters.
* Repeat 4 times:
* Randomly select 2 indexes in the word array using
* nextInt( word length) of the randGen parameter.
* Swap the letters at the selected indexes in the word array.
* Create a string from the array of characters.
*
* @param word The letters to be reordered.
* @param randGen A random number generator.
* @return The letters that have been randomly ordered.
*/
public static String reorderLetters(String word, Random randGen) {
return null; //TODO
}   
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class WordDetective {

   /**
   * Reorders the letters in word.
   *
   * Algorithm:
   * Copy the word into an array of characters.
   * Repeat 4 times:
   * Randomly select 2 indexes in the word array using
   * nextInt( word length) of the randGen parameter.
   * Swap the letters at the selected indexes in the word array.
   * Create a string from the array of characters.
   *
   * @param word The letters to be reordered.
   * @param randGen A random number generator.
   * @return The letters that have been randomly ordered.
   */
   public static String reorderLetters(String word, Random randGen) {
       char[] arr = word.toCharArray();
       int idx1, idx2;
       char temp;
      
       for(int i = 1; i <= 4; i++){
           idx1 = randGen.nextInt(word.length());
           idx2 = randGen.nextInt(word.length());
           temp = arr[idx1];
           arr[idx1] = arr[idx2];
           arr[idx2] = temp;
       }
       return new String(arr);
   }
}

  

Add a comment
Know the answer?
Add Answer to:
Complate this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT