Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise Listed are 6 separate methods you need to write. Each one has the docum

ID: 3554655 • Letter: E

Question

Exercise
Listed are 6 separate methods you need to write. Each one has the documentation that gives
the method name and describes what the method needs to do. It also lists the parameters needed and return
type.

For each one, you need to write the entire method. You must use the method name listed and follow the
documentation so it has the proper parameters and return value.

As you are doing this, you should test each method individually by calling it in the main method. You will
need to create a .txt file to test with. You can put however many words in it you want to test with.

The final program you turn in should have an empty main method along with the 6 completed methods
listed.


Method #1
/**
* This method counts the number of words in a given file and returns that number.
*
* @methodName getNumberOfWordsInFile
* @param fileName
* @return wordCount
* @throws FileNotFoundException
*/


Method #2
/**
* This method accepts a file name.
* First it gets the number of words in the file by calling the getNumberOfWordsInFile method.
* Then it creates a String array based on how many words are in the file.
* Next, it fills the array with all of the words from the file.
* Then it returns the array.
*
* @methodName getWordsFromFile
* @param fileName
* @return wordArray
* @throws FileNotFoundException
*/


Method #3
/**
* This method returns a random string from a given String array
*
* @methodName getRandomWord
* @param wordArray
* @return randomWord
*/
Method #4
/**
* This method accepts an int and creates a char array of that size
* Then it sets each element in the array to an underscore '_'
* Then it returns the char array.
*
* @methodName setDefaultCharArray
* @param size
* @return charArray
*/


Method #5
/**
* This method takes the given char array and uses a loop to
* print out each element all on the same line.
*
* @methodName printCharArray
* @param charArray
*/


Method #6
/**
* This method takes the given char array and checks if any of the elements
* are equal to an underscore '_'
* It returns true if the char array contains an underscore '_'
* and false if it does not.
*
* @methodName isWordComplete
* @param charArray
* @return isComplete
*/

Explanation / Answer

Hi,

PFB the Program.

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

public class WordPrgm {
   public static void main(String[] args) throws Exception {

   }

   /**
   * This method counts the number of words in a given file and returns that
   * number.
   *
   * @methodName getNumberOfWordsInFile
   * @param fileName
   * @return wordCount
   * @throws FileNotFoundException
   */
   int getNumberOfWordsInFile(String fileName) throws FileNotFoundException {
       int wordCount = 0;

       File file = new File("word.txt");
       Scanner sc = new Scanner(new FileInputStream(file));
       int count = 0;
       while (sc.hasNext()) {
           sc.next();
           wordCount++;
       }

       return wordCount;
   }

   /**
   * This method accepts a file name. First it gets the number of words in the
   * file by calling the getNumberOfWordsInFile method. Then it creates a
   * String array based on how many words are in the file. Next, it fills the
   * array with all of the words from the file. Then it returns the array.
   *
   * @methodName getWordsFromFile
   * @param fileName
   * @return wordArray
   * @throws FileNotFoundException
   */

   String[] getWordsFromFile(String fileName) throws FileNotFoundException {
       int wordCount = getNumberOfWordsInFile("word.txt");
       String[] wordArray = new String[wordCount];
       File file = new File("word.txt");
       Scanner sc = new Scanner(new FileInputStream(file));
       int i = 0;
       while (sc.hasNext()) {
           wordArray[i] = sc.next();
           i++;
       }

       return wordArray;
   }

   /**
   * This method returns a random string from a given String array
   *
   * @methodName getRandomWord
   * @param wordArray
   * @return randomWord
   */

   String getRandomWord(String[] wordArray) {
       String randomWord = null;
       Random rd = new Random();
       try {
           int count = getNumberOfWordsInFile("word.txt");
           int randNo = rd.nextInt(count - 1);
           randomWord = wordArray[randNo];
           while (count - 1 < randNo) {

               if (count - 1 < randNo) {
                   randomWord = wordArray[randNo];
                   break;
               } else
                   randNo = rd.nextInt(count - 1);
           }
       } catch (Exception e) {
           e.printStackTrace();

       }

       return randomWord;
   }

   /**
   * This method accepts an int and creates a char array of that size Then it
   * sets each element in the array to an underscore '_' Then it returns the
   * char array.
   *
   * @methodName setDefaultCharArray
   * @param size
   * @return charArray
   */

   char[] setDefaultCharArray(int size) {
       char[] charArray = new char[size];
       for (int i = 0; i < charArray.length; i++)
           charArray[i] = '_';
       return charArray;
   }

   /**
   * This method takes the given char array and uses a loop to print out each
   * element all on the same line.
   *
   * @methodName printCharArray
   * @param charArray
   */

   void printCharArray(char[] charArray) {
       for (int i = 0; i < charArray.length; i++)
           System.out.print(charArray[i]);
   }

   /**
   * This method takes the given char array and checks if any of the elements
   * are equal to an underscore '_' It returns true if the char array contains
   * an underscore '_' and false if it does not.
   *
   * @methodName isWordComplete
   * @param charArray
   * @return isComplete
   */

   boolean isWordComplete(char[] charArray) {
       boolean isComplete = true;
       for (int i = 0; i < charArray.length; i++) {
           if (charArray[i] != '_') {
               isComplete = false;
               break;
           }
       }
       return isComplete;
   }
}


Hope it is helpful.

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote