Initial commit for 4.3
parent
7cdfc0e9fb
commit
b35f991d5d
@ -0,0 +1,11 @@
|
|||||||
|
package _4._3;
|
||||||
|
|
||||||
|
public interface Comparable {
|
||||||
|
/**
|
||||||
|
* Compared this and other
|
||||||
|
* Returns -1 if this less than other,
|
||||||
|
* 0 if this equal to other,
|
||||||
|
* 1 if this greater than other
|
||||||
|
*/
|
||||||
|
int compareTo(Comparable other);
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package _4._3;
|
||||||
|
|
||||||
|
public class ComparableInteger extends Integer implements Comparable {
|
||||||
|
public ComparableInteger(int value) {
|
||||||
|
super(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compared this and other
|
||||||
|
* Returns -1 if this < other,
|
||||||
|
* 0 if this == other,
|
||||||
|
* 1 if this > other
|
||||||
|
*
|
||||||
|
* @throws NullPointerException if other == null
|
||||||
|
* @throws ClassCastException if !(other instanceof Integer)
|
||||||
|
* @see Comparable#compareTo(Comparable)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int compareTo(Comparable other) {
|
||||||
|
int o = ((Integer) other).value;
|
||||||
|
int t = this.value;
|
||||||
|
return t < o ? -1 : t == o ? 0 : 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
package _4._3;
|
||||||
|
|
||||||
|
public class Integer {
|
||||||
|
protected int value;
|
||||||
|
|
||||||
|
public Integer(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package _4._3;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
/**
|
||||||
|
* Returns the smallest element
|
||||||
|
*
|
||||||
|
* @throws NullPointerException if elements == null
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if elements.length == 0
|
||||||
|
*/
|
||||||
|
public static Comparable getMinimum(Comparable[] elements) throws NullPointerException, IndexOutOfBoundsException {
|
||||||
|
Comparable min = elements[0]; // assume the first element is the smallest
|
||||||
|
for(int i = 1; i < elements.length; i++) {
|
||||||
|
Comparable candidate = elements[i];
|
||||||
|
if(min.compareTo(candidate) > 0) {
|
||||||
|
min = candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue