-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashingAWidget_3G_Girish.java
112 lines (98 loc) · 2.39 KB
/
HashingAWidget_3G_Girish.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//Name: Kiran Girish Date: 03/29/15
import java.util.*;
public class HashingAWidget_3G_Girish
{
public static void main(String[] args)
{
Set<Widget> tSet = new TreeSet<Widget>();
Set<Widget> hSet = new HashSet<Widget>();
Widget a = new Widget(2,3); //same or different?
Widget b = new Widget(2,3);
Widget c = new Widget(2,3);
// c = b;
tSet.add(a);
tSet.add(b);
tSet.add(c);
hSet.add(a);
hSet.add(b);
hSet.add(c);
System.out.println(a.hashCode()+ " "+b.hashCode() + " " + c.hashCode());
System.out.println("TreeSet: " + tSet);
System.out.println("HashSet: " + hSet);
}
}
/***************************************
Modify the Widget class so that it hashes on its
values, not on its address. Be sure that compareTo(),
equals(Object) and hashCode() agree with each other.
****************************************/
class Widget implements Comparable<Widget>
{
private int myPounds, myOunces;
public Widget()
{
myPounds = myOunces = 0;
}
public Widget(int x)
{
myPounds = x;
myOunces = 0;
}
public Widget(int x, int y)
{
myPounds = x;
myOunces = y;
}
public Widget(Widget arg)
{
myPounds = arg.getPounds();
myOunces = arg.getOunces();
}
public int getPounds()
{
return myPounds;
}
public int getOunces()
{
return myOunces;
}
public void setPounds(int x)
{
myPounds = x;
}
public void setOunces(int x)
{
myOunces = x;
}
public int compareTo(Widget w)
{
if(this.getPounds() == w.getPounds())
{
return this.getOunces() - w.getOunces();
}
return this.getPounds() - w.getPounds();
}
public boolean equals(Widget arg)
{
if(this.compareTo(arg) == 0)
return true;
return false;
}
public String toString()
{
String wid = "Pounds: " + this.getPounds() + " Ounces: " + this.getOunces();
return wid;
}
public boolean equals(Object arg)
{
if(this.compareTo((Widget) arg) == 0)
{
return true;
}
return false;
}
public int hashCode()
{
return this.getPounds()*16 + this.getOunces();
}
}