Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Two words, word1 and word2, are similar if and only if all the characters in word1 are in word2; and all the characters in
Two words, word1 and word2, are similar if and only if all the characters in word1 are in word2; and all the characters in word2 are in word1; including the same number of duplication. For example, abba and baba are similar; but aaba and baba are not similar. Obviously if the two words are of different lengths, they are not similar. We use the following algorithm to check two words which are similar: For each word, we associate a marker to each of the characters. For example, if the word is abba, we add a marker to each character; for example, an hyphen; now the word abba becoming a-b-b-a-. To check two words which are similar, we get a character from word1, if that character is in word2, we "mark" the character; for example, change hyphen (-) to another special symbol for the two words; for example, an asterisk (*). If all the characters are marked in the two words, then the two words are similar. We propose the following UML diagram to describe a word or two words: CharInfo -char ch -char marker +Charlnfo(char ch, char marker) 1..1 +Charinfo(Charinfo ci) +char getChar( +char getMarker() +void setinfo(char ch, char marker) +void displayInfo() In the above UML diagram: 1..1 TwoWords -Charlnfo] word1 -Charlnfo word2 +TwoWords (Charlnfo@ word1, Charinfo] word2) -boolean charinWord(char ch, Charinfo word) + boolean areSimilar The class Charlnfo has two instance variables, a character ch and a character marker The class TwoWords has two instance variables, word1 and word1 which are two arrays of Charlnfo. This class has a constructor, a method to check if a character is inside a word and a method to check if the two words are similar. During the processing, you "mark" the characters. Let us talk about the main class: -static Charlnfoll getCIArray(String word) -static void displayInfo(Charlnfol ciArray) + static void main(String args) You construct a data file called e3.txt consists of the following information: (you can copy and paste them) In the main method aaabbb ababab bababa abc I will use the above sample data file to test your design. E_3 bbbaaa bababb ababab abcdef - You read the two words - Convert each word to an array of Charlnfo - Construct an object of the class TwoWords and check if the two words are the similar - The displayInfo method displays the array of Charinfo. Upon execution of your program, you see the following analysis and result: Given the following words. info a-a-a-b-b-b- b-b-b-a-a-a- Analysis Given a*a*a*b*b*b* b*b*b*a*a*a* ==> Similar the following words' info a-b-a-b-a-b- b-a-b-a-b-b- Analysis a*b*a*b*a-b- b*a*b*a*b-b- ==>Not Similar Analysis Given the following words info Given the following words info a-b-c- b-a-b-a-b-a- a-b-a-b-a-b- a-b-c-d-e-f- b*a*b*a*b*a* a*b*a*b*a*b* ==> Similar Analysis a-b-c- a-b-c-d-e-f- ==>Not Similar Save all the classes (remove the public from all classes so that I can compile) in a Java file called YourName_E_3.java and upload this Java file.
Step by Step Solution
★★★★★
3.42 Rating (158 Votes )
There are 3 Steps involved in it
Step: 1
import javaioFile import javaioFileNotFoundException ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started