-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
39 lines (32 loc) · 971 Bytes
/
Counter.java
File metadata and controls
39 lines (32 loc) · 971 Bytes
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
public class Counter implements Comparable{
Object _val;
int _freq;
//***************************************************************************
//Constructor
//***************************************************************************
public Counter(Object val, int freq){
_val = val;
_freq = freq;
}
//***************************************************************************
//Compares the freq of an object with standard compareTo rules
//***************************************************************************
public int compareTo(Object rhs){
if (!(rhs instanceof Counter))
throw new IllegalArgumentException();
Counter x = (Counter)rhs;
if (_freq == x._freq)
return 0;
else if (_freq > x._freq) return 1;
else return -1;
}
public void inc(){
_freq++;
}
public void dec(){
_freq--;
}
public String toString(){
return _val + ":" + _freq;
}
}