From b35f991d5df59b67ac73fce5a0eac311eeb98d61 Mon Sep 17 00:00:00 2001 From: Selebrator Date: Sun, 5 May 2019 12:19:11 +0200 Subject: [PATCH] Initial commit for 4.3 --- src/main/java/_4/_3/Comparable.java | 11 ++++++++++ src/main/java/_4/_3/ComparableInteger.java | 24 ++++++++++++++++++++++ src/main/java/_4/_3/Integer.java | 13 ++++++++++++ src/main/java/_4/_3/Utils.java | 20 ++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 src/main/java/_4/_3/Comparable.java create mode 100644 src/main/java/_4/_3/ComparableInteger.java create mode 100644 src/main/java/_4/_3/Integer.java create mode 100644 src/main/java/_4/_3/Utils.java diff --git a/src/main/java/_4/_3/Comparable.java b/src/main/java/_4/_3/Comparable.java new file mode 100644 index 0000000..63dd957 --- /dev/null +++ b/src/main/java/_4/_3/Comparable.java @@ -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); +} diff --git a/src/main/java/_4/_3/ComparableInteger.java b/src/main/java/_4/_3/ComparableInteger.java new file mode 100644 index 0000000..bfd915f --- /dev/null +++ b/src/main/java/_4/_3/ComparableInteger.java @@ -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; + } +} diff --git a/src/main/java/_4/_3/Integer.java b/src/main/java/_4/_3/Integer.java new file mode 100644 index 0000000..6ccfb52 --- /dev/null +++ b/src/main/java/_4/_3/Integer.java @@ -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; + } +} diff --git a/src/main/java/_4/_3/Utils.java b/src/main/java/_4/_3/Utils.java new file mode 100644 index 0000000..c3b7841 --- /dev/null +++ b/src/main/java/_4/_3/Utils.java @@ -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; + } +}