Description of program:

You are to write a program name anagram.java that prompt the user for a user input file, reads the input words and computes anagram of words. An anagram of a word is a permutation of the letters in that word; for example, stop is an anagram of tops.

As indicated, the input to the program is a list of words from a user input file . The output is a list containing the same words, but with anagrams displayed on the same line both on the screen and the output file. The output file must be named output.txt

The input file, may look like the following: You must prompt the user for the name of the input file.

Pans naps
Pots opt
this that
Sit it’s
snap
and so on.

Executing a program, example:

C:>java anagram ​ OR​ C:java -jar anagram.jar [Enter]
Enter the name of the input file: input.dat ​ [Enter]
Pans snap naps
Sit it's
opt
Pots
this
that

Here are some requirements for the program:

  • When determining if two words are anagrams, the program must treat upper and lower case letters as equivalent (thus Pans and snap are anagrams) and ignore punctuation marks (its and Sit are anagrams). However, the program must display words with their original capitalization and punctuation as shown above.
  • The word is assumed to be any series of nonblank characters; words may be separated by any number of white-space characters. Any number of words may appear on a line, including none. You may assume that no word is more than 12 characters long. And maximum number of words would be 50.
  • The program must work even if the input file is empty. If this is the case print a message saying that the input file is empty and then terminate the program.
  • The program must test the number of characters per word. If a word consist of more than 12 characters, the program should ignore that word and continue. That word would also be ignored in the total number of words of 50.
  • The program must also test the number of words. If there are more than 50 words, print a message saying that there are more than 50 words in the input file and then terminate the program Algorithm: Use efficient algorithms. The first insight is to create a signature for each word by sorting the letters in the word after removing punctuations.
Original Word Signature
Pans anps
Pots opts
opt opt
Sit ist
it’s ist
snap anps

Upper case are converted to lower case and non-alphabetic characters (eg. /<>[{ ) are removed before the signature is computed. Punctuation marks are also ignored when computing signatures.

Creating signatures for words makes it easy to check whether two words are anagrams. However, we still have the problem of (apparently) having to compare every input word against every other input word. Actually, all we need to do is sort the lines by their signatures.

Original Word Signature
Pans anps
snap anps
Sit ist
it’s ist
Pots opts
opt opt

We next make a pass over from top to bottom, printing the original words. The words that have the same signature are anagrams, so we print them on the same line:

Pans snap
Pots Sit
it’s opt

Implementation: Use as many generic algorithm as possible so that the size of the program can be reduced.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.