-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyInteger.java
More file actions
89 lines (80 loc) · 2.43 KB
/
Copy pathMyInteger.java
File metadata and controls
89 lines (80 loc) · 2.43 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
package DataStructures;
import DataStructures.Hash.Hashable;
/**
* Wrapper class for use with generic data structures.
* Mimics Integer.
* In Java 1.2, you can use Integer if Comparable is needed.
* @author Mark Allen Weiss
*/
public final class MyInteger implements Comparable, Hashable
{
/**
* Construct the MyInteger object with initial value 0.
*/
public MyInteger( )
{
this( 0 );
}
/**
* Construct the MyInteger object.
* @param x the initial value.
*/
public MyInteger( int x )
{
value = x;
}
/**
* Gets the stored int value.
* @return the stored value.
*/
public int intValue( )
{
return value;
}
/**
* Implements the toString method.
* @return the String representation.
*/
public String toString( )
{
return Integer.toString( value );
}
/**
* Implements the compareTo method.
* @param rhs the other MyInteger object.
* @return 0 if two objects are equal;
* less than zero if this object is smaller;
* greater than zero if this object is larger.
* @exception ClassCastException if rhs is not
* a MyInteger.
*/
public int compareTo( Comparable rhs )
{
return value < ((MyInteger)rhs).value ? -1 :
value == ((MyInteger)rhs).value ? 0 : 1;
}
/**
* Implements the equals method.
* @param rhs the second MyInteger.
* @return true if the objects are equal, false otherwise.
* @exception ClassCastException if rhs is not
* a MyInteger.
*/
public boolean equals( Object rhs )
{
return rhs != null && value == ((MyInteger)rhs).value;
}
/**
* Implements the hash method.
* @param tableSize the hash table size.
* @return a number between 0 and tableSize-1.
*/
public int hash( int tableSize )
{
if( value < 0 )
return -value % tableSize;
else
return value % tableSize;
}
private int value;
}