-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordObject.java
More file actions
66 lines (54 loc) · 1.59 KB
/
WordObject.java
File metadata and controls
66 lines (54 loc) · 1.59 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
/*
* Programmer: Dan Hopp
* Date: 27-FEB-2020
* Description: Class for the Word Object(s). Contains:
String for the word
Count for the word
Default constructor
Constructor with a pass-a-value
Getter methods
A method to up the word's count by 1
A default sort order by using the word's count
Print template for the export file's text
How-to's, help, and guidance for sorting objects within an ArrayList were
taken from:
https://docs.oracle.com/javase/8/docs/api/
https://howtodoinjava.com/sort/sort-arraylist-objects-comparable-comparator/
*/
package lab3;
class WordObject implements Comparable<WordObject>{
private final String WORD;
private int count;
//Default constructor
public WordObject(){
WORD = "";
count = 1;
}
//Constructor with pass-a-value
public WordObject(String newWord){
WORD = newWord;
count = 1;
}
//Getter methods
public String getWord() {
return WORD;
}
public int getCount() {
return count;
}
//Up the word's count by 1
public void addToWordCount(){
count = count + 1;
}
//Setting up a default sort order by using the word's count
@Override
public int compareTo(WordObject obj) {
//Flipping comparing operands so the ordering will be in desc order
return obj.count - this.count;
}
//The print template for the export file's text
@Override
public String toString(){
return WORD + " " + count;
}
}