Committing my attempt on task number 1
Not finished, not even sure If useful at allgeneric-observer
parent
02e4f164a2
commit
d7d35d0c41
@ -0,0 +1,52 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
package _5._1;
|
||||||
|
|
||||||
|
|
||||||
|
import provided.IO;
|
||||||
|
|
||||||
|
public class UtilTest {
|
||||||
|
|
||||||
|
// Testprogramm
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String eingabe = IO.readString("Zahl: ");
|
||||||
|
int zahl = Util.toInt(eingabe);
|
||||||
|
try {
|
||||||
|
System.out.println(zahl + " hoch " + zahl + " = "
|
||||||
|
+ Util.power(zahl, zahl));
|
||||||
|
}catch(IllegalArgumentException exc){
|
||||||
|
System.out.println("A exception has occurred: Division by zero");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
System.out.println(Util.minimum(new int[]{1, 6, 4, 7, -3, 2}));
|
||||||
|
System.out.println(Util.minimum(new int[0]));
|
||||||
|
System.out.println(Util.minimum(null));
|
||||||
|
} catch(IllegalArgumentException exc){
|
||||||
|
System.out.println("Array has not enough elements: <2");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue