From cca07f0bd6e845ef9f9ba6b5d0c30c4c27708616 Mon Sep 17 00:00:00 2001 From: Selebrator Date: Tue, 14 May 2019 00:06:30 +0200 Subject: [PATCH] 5.1 does something now --- src/main/java/_5/_1/EmptyArrayException.java | 10 ++ src/main/java/_5/_1/EmptyStringException.java | 10 ++ .../java/_5/_1/IllegalDigitException.java | 10 ++ .../java/_5/_1/IllegalNumberException.java | 10 ++ .../java/_5/_1/NumberOverflowException.java | 10 ++ .../java/_5/_1/UndefinedValueException.java | 10 ++ src/main/java/_5/_1/Util.java | 52 ---------- src/main/java/_5/_1/UtilTest.java | 98 +++++++++++++++++-- 8 files changed, 148 insertions(+), 62 deletions(-) create mode 100644 src/main/java/_5/_1/EmptyArrayException.java create mode 100644 src/main/java/_5/_1/EmptyStringException.java create mode 100644 src/main/java/_5/_1/IllegalDigitException.java create mode 100644 src/main/java/_5/_1/IllegalNumberException.java create mode 100644 src/main/java/_5/_1/NumberOverflowException.java create mode 100644 src/main/java/_5/_1/UndefinedValueException.java delete mode 100644 src/main/java/_5/_1/Util.java diff --git a/src/main/java/_5/_1/EmptyArrayException.java b/src/main/java/_5/_1/EmptyArrayException.java new file mode 100644 index 0000000..60d33ec --- /dev/null +++ b/src/main/java/_5/_1/EmptyArrayException.java @@ -0,0 +1,10 @@ +package _5._1; + +public class EmptyArrayException extends RuntimeException { + public EmptyArrayException() { + } + + public EmptyArrayException(String message) { + super(message); + } +} diff --git a/src/main/java/_5/_1/EmptyStringException.java b/src/main/java/_5/_1/EmptyStringException.java new file mode 100644 index 0000000..d315301 --- /dev/null +++ b/src/main/java/_5/_1/EmptyStringException.java @@ -0,0 +1,10 @@ +package _5._1; + +public class EmptyStringException extends RuntimeException { + public EmptyStringException() { + } + + public EmptyStringException(String message) { + super(message); + } +} diff --git a/src/main/java/_5/_1/IllegalDigitException.java b/src/main/java/_5/_1/IllegalDigitException.java new file mode 100644 index 0000000..3c6cb3b --- /dev/null +++ b/src/main/java/_5/_1/IllegalDigitException.java @@ -0,0 +1,10 @@ +package _5._1; + +public class IllegalDigitException extends RuntimeException { + public IllegalDigitException() { + } + + public IllegalDigitException(String message) { + super(message); + } +} diff --git a/src/main/java/_5/_1/IllegalNumberException.java b/src/main/java/_5/_1/IllegalNumberException.java new file mode 100644 index 0000000..8e172d9 --- /dev/null +++ b/src/main/java/_5/_1/IllegalNumberException.java @@ -0,0 +1,10 @@ +package _5._1; + +public class IllegalNumberException extends RuntimeException { + public IllegalNumberException() { + } + + public IllegalNumberException(String message) { + super(message); + } +} diff --git a/src/main/java/_5/_1/NumberOverflowException.java b/src/main/java/_5/_1/NumberOverflowException.java new file mode 100644 index 0000000..015c4f8 --- /dev/null +++ b/src/main/java/_5/_1/NumberOverflowException.java @@ -0,0 +1,10 @@ +package _5._1; + +public class NumberOverflowException extends RuntimeException { + public NumberOverflowException() { + } + + public NumberOverflowException(String message) { + super(message); + } +} diff --git a/src/main/java/_5/_1/UndefinedValueException.java b/src/main/java/_5/_1/UndefinedValueException.java new file mode 100644 index 0000000..f6d329e --- /dev/null +++ b/src/main/java/_5/_1/UndefinedValueException.java @@ -0,0 +1,10 @@ +package _5._1; + +public class UndefinedValueException extends RuntimeException { + public UndefinedValueException() { + } + + public UndefinedValueException(String message) { + super(message); + } +} diff --git a/src/main/java/_5/_1/Util.java b/src/main/java/_5/_1/Util.java deleted file mode 100644 index 02fdc9b..0000000 --- a/src/main/java/_5/_1/Util.java +++ /dev/null @@ -1,52 +0,0 @@ -package _5._1; - -class Util { - - // liefert die kleinste Zahl des uebergebenen Arrays - public static int minimum(int[] values) throws IllegalArgumentException { - if (values.length <= 1 || values == null) { - throw new IllegalArgumentException("The given array has not enough elements (<=1) or is null"); - } - int min = values[0]; - for (int i = 1; i < values.length; i++) { - if (values[i] < min) { - min = values[i]; - } - } - return min; - } - - // konvertiert den uebergebenen String in einen int-Wert - public static int toInt(String str) { - int result = 0, factor = 1; - char ch = str.charAt(0); - switch (ch) { - case '-': - factor = -1; - break; - case '+': - factor = 1; - break; - default: - result = ch - '0'; - } - for (int i = 1; i < str.length(); i++) { - ch = str.charAt(i); - int ziffer = ch - '0'; - result = result * 10 + ziffer; - } - return factor * result; - } - - // liefert die Potenz von zahl mit exp, - // also zahl "hoch" exp (number to the power of exp) - public static long power(long number, int exp) throws IllegalArgumentException { - if(number == 0L && exp < 0){ - throw new IllegalArgumentException("Division by zero"); - } - if (exp == 0) { - return 1L; - } - return number * Util.power(number, exp - 1); - } -} \ No newline at end of file diff --git a/src/main/java/_5/_1/UtilTest.java b/src/main/java/_5/_1/UtilTest.java index 6e1a7c0..92e8433 100644 --- a/src/main/java/_5/_1/UtilTest.java +++ b/src/main/java/_5/_1/UtilTest.java @@ -1,30 +1,108 @@ package _5._1; - import provided.IO; +class Util { + + // liefert die kleinste Zahl des uebergebenen Arrays + public static int minimum(int[] values) throws NullPointerException, EmptyArrayException { + //NPE finde ich hilfreicher als alles andere weil ich dann weiß das irgendow null übergeben wurde. + if(values.length == 0) { + throw new EmptyArrayException("Cannot get the minimum of nothing"); + } + int min = values[0]; + for(int i = 1; i < values.length; i++) { + if(values[i] < min) { + min = values[i]; + } + } + return min; + } + + // konvertiert den uebergebenen String in einen int-Wert + public static int toInt(String str) throws NullPointerException, + EmptyStringException, + IllegalNumberException, + IllegalDigitException, + NumberOverflowException { + //NPE finde ich hilfreicher als alles andere weil ich dann weiß das irgendow null übergeben wurde. + if(str.isEmpty()) { + throw new EmptyStringException(); + } + int result = 0, factor = 1; + char ch = str.charAt(0); + if((ch == '+' || ch == '-') && str.length() == 1) { + throw new IllegalNumberException(ch + " is not a number"); + } + int i = 1; + switch(ch) { + case '-': + factor = -1; + break; + case '+': + factor = 1; + break; + default: + i = 0; + } + for(; i < str.length(); i++) { + ch = str.charAt(i); + if(!('0' <= ch && ch <= '9')) { + throw new IllegalDigitException(ch + " is not a digit in base 10"); + } + int ziffer = ch - '0'; + int newResult = result * 10 + ziffer; + if(newResult < result) { + throw new NumberOverflowException(); + } + result = newResult; + } + return factor * result; + } + + // liefert die Potenz von zahl mit exp, + // also zahl "hoch" exp (number to the power of exp) + public static long power(long number, int exp) throws UnsupportedOperationException, UndefinedValueException { + if(exp < 0) { + throw new UnsupportedOperationException("not implemented for negative exponents"); + } + if(exp == 0) { + if(number == 0) { + throw new UndefinedValueException("0^0 not defined"); + } + return 1L; + } + return number * Util.power(number, exp - 1); + } +} + public class UtilTest { // Testprogramm public static void main(String[] args) { - String eingabe = IO.readString("Zahl: "); - int zahl = Util.toInt(eingabe); try { + String eingabe = IO.readString("Zahl: "); + int zahl = Util.toInt(eingabe); System.out.println(zahl + " hoch " + zahl + " = " + Util.power(zahl, zahl)); - }catch(IllegalArgumentException exc){ - System.out.println("A exception has occurred: Division by zero"); + } catch(NullPointerException | EmptyStringException | IllegalNumberException | IllegalDigitException | NumberOverflowException | UnsupportedOperationException | UndefinedValueException e) { + err(e); } + System.out.println(Util.minimum(new int[]{ 1, 6, 4, 7, -3, 2 })); try { - System.out.println(Util.minimum(new int[]{1, 6, 4, 7, -3, 2})); System.out.println(Util.minimum(new int[0])); + } catch(EmptyArrayException e) { + err(e); + } + try { System.out.println(Util.minimum(null)); - } catch(IllegalArgumentException exc){ - System.out.println("Array has not enough elements: <2"); + } catch(NullPointerException e) { + err(e); } + } - + private static void err(Exception e) { + System.err.println(e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : "")); } } -