-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWord.java
More file actions
91 lines (79 loc) · 2.65 KB
/
Word.java
File metadata and controls
91 lines (79 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Provides a simple word object that stores a word together with its letters
* sorted.
*
* @author Ahmer Javed Gondal
* @version 04-03-2023
* <pre>
* Takes in a string then converts it into a word object by alphabetically sorting the word's characters and then setting the "sorted" attribute to that new string.
* We'll override the equals and hashcode methods to allow us to really compare word objects. Additionally, there's an implemented anagramChecker method which compares the sorted
* string of this object to the passed sorted string of another object.
* </pre>
*/
import java.util.Arrays;
public class Word
{
public String word; // the word
public String sorted; // the word re-arranged so that its letters are sorted
/**
* Store the word and create a sorted version of the word.
*
* @param word the word to be stored
*/
public Word(String word)
{
char [] letters= word.toCharArray();
Arrays.sort(letters);
this.word= word;
this.sorted= new String(letters);
}
public String getSorted(){
return this.sorted;
}
/**
* Just show the regular word when object is printed.
*
* @return the regular word
*/
public String toString()
{
return this.word + "(" + sorted + ")";
}
/**
* Overrides the built-in equals method to allow us to the check whether objects are actually equivalent in terms of attributes.
* @param obj will be the object we'll be typecasting and checking for equivalence to this.
* @return a boolean indicating whether the object in the parameter is equivalent to this.object.
*/
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Word)) return false;
if(obj == this) return true;
Word other = (Word) obj;
return this.word.equals(other.word);
}
/**
* Overrides the built-in hashCode() method to allow us to the check whether objects are actually equivalent in terms of attributes.
* @return an int indicating the hash value of the string word.
*/
@Override
public int hashCode() {
return word.hashCode();
}
/**
* This method checks whether the passed word object is an anagram to this word object.
* @param w
* @return a boolean indicating whether the passed word is an anagram to THIS word.
*/
public boolean anagramCheck(Word w) {
return w.sorted.equals(this.sorted);
}
/**
* This method compares two strings and returns a value based on the size of the two (alphabetical order)
* @param obj the object we're comparing "this" to.
* @return an int indicating whether the external object is smaller or larger than "this" object.
*/
public int compareTo(Object obj) {
Word other = (Word) obj;
return this.sorted.compareToIgnoreCase(other.sorted) ;
}
}