-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparableInterface.java
More file actions
73 lines (56 loc) · 1.44 KB
/
ComparableInterface.java
File metadata and controls
73 lines (56 loc) · 1.44 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
import java.util.Arrays;
// Implementing Parameterized Comparable Interface
class Box implements Comparable<Box>{
private double length;
private double width;
private double height;
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
Box(double l, double b, double h){
length=l;
width=b;
height=h;
}
public double area(){
return 2*(length*width+width*height+height*length);
}
@Override
public int compareTo(Box o) {
return (int)(this.area()-o.area());
}
}
public class ComparableInterface {
public static void main(String[] args) {
System.out.println("I am in main");
int[] data = {10,-5,56,78,11,89,23};
String[] names = {"Divyam","abc","bcd","def","ghi"};
Box [] b = new Box[5];
b[0] = new Box(10,6,5);
b[1] = new Box(10,20,5);
b[2] = new Box(5,20,25);
b[3] = new Box(40,30,45);
b[4] = new Box(100,16,8);
Arrays.sort(data);
for (int i:data
) {
// System.out.println(i);
}
Arrays.sort(names);
for (String str:names
) {
// System.out.println(str);
}
Arrays.sort(b);
for (Box b1:b
) {
System.out.println(b1.area());
}
}
}