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

(java) (1)First we need to compute an ordered form of each word. In this case, t

ID: 3769391 • Letter: #

Question

(java)

(1)First we need to compute an ordered form of each word.  In this case, the ordered form of a word contains the same letters as the original, but in sorted order.  So compose a static method

public static String orderedForm(String word)

so that orderedForm("computer") returns "cemoprtu", and orderedForm("poor") returns "oopr".  For this first step, put this method in a little program OrderedWord.java that lets the user input one word and outputs its ordered form:

java OrderedWord

Enter word: computer

cemoprtu

To implement orderedForm, program a loop that reads the individual characters in the string parameter and puts them in an array of char or ArrayList. Thus orderedForm("poor") makes an array of 4 chars 'p', 'o', 'o', 'r' or an ArrayList with the corresponding Characters. Then sort that array or ArrayList.   It will be easiest to use one of the JDK sort methods.  For arrays you will want to use Arrays.sort(ca) (assuming that ca is your array of char), you will need to import java.util.Arrays if you use this.  For ArrayLists you will want to use Collections.sort(cal) (assuming that cal is your ArrayList of Character), and you will need to import java.util.Collections if you use this.  Finally, use the sorted characters to build a new String by a loop of s = s + c, where s is a String, and c is a char or Character.  There is a much faster way of doing this without loops, but please use this way of doing it instead.

The program OrderedWord needs a main method as well as the orderedForm method.  All it does is use a Scanner to get the word from the user and call orderedForm to convert it, and print out the resulting ordered form.  Only do this once (get word from user, output ordered form of word) and then have the program end.

(2)

Write a narrative that describes how you went about designing and implementing these modifications, how you tested your program (how test cases were chosen and why) and a (cut and pasted) result for these tests. Put this in a Word, pdf, text, or rich text file called memo, with appropriate extension (.doc, .docx, .pdf, .txt, .rtf).  Describe if you used the test case during development or you used the final words.txt from the start, or what? Did you find it helpful that the orderedForm computation was separately developed? Report on the final results from the test runs on the full files.

Extra credit: For part 4, in addition to simply counting the words that appear in hamlet.txt but do not appear in words.txt, print these words and how often they occurred in hamlet.txt to a file called HamletWordsNotInDictionary.txt.  This file should have the following format:

bodkin: 12

grabe: 5

If, for example, the word "bodkin" appeared in hamlet.txt, but not in words.txt, and it appeared 12 times in hamlet, and the word "grabe" appeared 5 times in hamlet.txt, but not in words.txt, etc.

For this you may want to sort the hamletWords ArrayList, and then for each entry in it, if it is not in the words list and it did not appear as the previous entry in the hamletWords list, start a new line in the file with this word, and then continue iterating over (loop through) hamletWords until you see a different word, counting how many times you saw the same word, and then output that number to the file (how many times the word occurred in hamlet.txt).

More extra credit: Write a program to determine and report on word length frequencies for hamlet.txt and words.txt.  Word lengths are the number of characters each word takes.  For example, "cat" has length 3, "bobsled" has length 7.  By frequency I mean how many times words of a specific length appear.  How many words of length 2 appear in hamlet?  How many words of length 5 appear in words.txt?  How many different words of length 4 appear in hamlet?  Your program should determine all the word length frequencies (for each word length that exists in words, hamlet, and the list of distinct words in hamlet, determine how many times it appears in each of these).  It should also determine the average word length based on these frequencies for hamlet, the list of distinct words in hamlet, and the list of words in words.txt.  This program should be called WordLengthFrequencies.java, and it should print all its results to the monitor.

You will build the list of words in words.txt, the list of all words in hamlet.txt, and the list of distinct words in hamlet.txt, as you did in prior parts of the assignment (as ArrayLists).  You can approach the problem in several different ways, how you do it is up to you.  One way would be to make separate ArrayLists of Integer where the index in the ArrayList is the word length and the value at that index is the frequency.  Another would be to make a separate class called LengthFrequencies, or the like, that contains 2 int fields: length and frequency, and make an ArrayList<LengthFrequencies>.  Another way would be to make a compareTo method for a new class that sorts based on the length of its String field and then put all the words into instances of this class and make an ArrayLIst of these, sort them, and use teh list sorted by word length.  No matter how you do it, the output should look as follows (as an example):

Word length frequencies for words.txt:

Average word length: 2.67

Words of length 1: 1

Words of length 2: 938

Words of length 3: 2745

Words of length 4: ...

...

...

...

Word length frequencies for all words in hamlet.txt:

Average word length: 3.2

Words of length 1: 234

Words of length 2: 494378

Words of length 3: 4937

Words of length 4: ...

...

...

...

Word length frequencies for distinct words in hamlet.txt:

Average word length: 4.321

Words of length 1: 1

Words of length 2:  3445

Words of length 3: 24242

Words of length 4: ...

...

...

...

Note that the average word length for each list is derived by taking (1 * the number of words of length 1) + (2 * the number of words of length 2) + (3 * the number of words of length 3) + ... and finally once you have this sum, dividing it by the total number of words in the list.

Explanation / Answer

OrderedWord.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
*
*/

/**
* @author Srinivas Palli
*
*/
public class OrderedWord {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       try {

           Scanner scanner = new Scanner(System.in);
           System.out.print("Enter word:");
           String word = scanner.next();
           String orderedString = orderedForm(word);
           System.out.print(orderedString);

       } catch (Exception e) {
           // TODO: handle exception
       }

   }

   /**
   * @param word
   * @return
   */
   public static String orderedForm(String word) {
       List<Character> arrayList = new ArrayList<Character>();
       char[] cArray = word.toCharArray();

       for (int i = 0; i < cArray.length; i++) {
           arrayList.add(cArray[i]);

       }
       Collections.sort(arrayList);

       String s = "";
       for (int i = 0; i < cArray.length; i++) {
           s += arrayList.get(i);

       }

       return s;

   }

}
Output:
Enter word:computer
cemoprtu

Enter word:cat
act

Enter word:rajesh
aehjrs

LengthFrequencies.java
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
*
*/

/**
* @author xinthe
*
*/
public class LengthFrequencies {

   /**
   * @param args
   * @throws Exception
   */
   public static void main(String[] args) throws Exception {
       Scanner scanner = null;
       try {

           scanner = new Scanner(new File("words.txt"));
           System.out.println("Word length frequencies for words.txt:");
           countFrequencyOfWords(scanner);

           scanner = new Scanner(new File("hamlet.txt"));
           System.out.println("Word length frequencies for hamlet.txt:");
           countFrequencyOfWords(scanner);

       } catch (Exception e) {
           // TODO: handle exception
       } finally {
           scanner.close();

       }

   }

   /**
   * @param scanner
   */
   public static void countFrequencyOfWords(Scanner scanner) {
       try {

           Map<Integer, Integer> wordLengthFrequency = new HashMap<Integer, Integer>();
           while (scanner.hasNext()) {
               String word = scanner.next();
               Integer frequency = wordLengthFrequency.get(word.length());
               // Add a new item with a frequency of 1, or increment the
               // frequency count
               wordLengthFrequency.put(word.length(), frequency == null ? 1
                       : frequency + 1);
           }

           // Iterate through the keys in ascending order to build the output

           wordLengthFrequency
                   .keySet()
                   .stream()
                   .sorted()
                   .forEach(
                           key -> {

                               System.out.println("Words of length " + key
                                       + ": " + wordLengthFrequency.get(key));

                           });

       } catch (Exception e) {
           // TODO: handle exception
       }

   }
}

words.txt:
hai this info1
hai this is info2
a info1 and info2

hamlet.xt:
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2
hai this info1
hai this is info2
a info1 and info2

Output:
Word length frequencies for words.txt:
Words of length 1: 1
Words of length 2: 1
Words of length 3: 3
Words of length 4: 2
Words of length 5: 4
Word length frequencies for hamlet.txt:
Words of length 1: 10
Words of length 2: 10
Words of length 3: 30
Words of length 4: 20
Words of length 5: 40

Note: i cannot understand what is Average word length: