From 5698534b5575b25d4a8a75a5d3f810ad4180c5a7 Mon Sep 17 00:00:00 2001 From: teridax Date: Thu, 27 Apr 2023 18:43:29 +0200 Subject: [PATCH] added blatt 5/6 --- .idea/modules.xml | 1 + AufgabenBlatt6/.gitignore | 29 ++++++ AufgabenBlatt6/AufgabenBlatt6.iml | 21 +++++ AufgabenBlatt6/src/Aufgabe3.java | 11 +++ AufgabenBlatt6/src/Aufgabe5.java | 14 +++ AufgabenBlatt6/src/Aufgabe6.java | 146 ++++++++++++++++++++++++++++++ AufgabenBlatt6/src/Aufgabe7.java | 53 +++++++++++ AufgabenBlatt6/src/Aufgabe8.java | 16 ++++ AufgabenBlatt6/src/Aufgabe9.java | 68 ++++++++++++++ 9 files changed, 359 insertions(+) create mode 100644 AufgabenBlatt6/.gitignore create mode 100644 AufgabenBlatt6/AufgabenBlatt6.iml create mode 100644 AufgabenBlatt6/src/Aufgabe3.java create mode 100644 AufgabenBlatt6/src/Aufgabe5.java create mode 100644 AufgabenBlatt6/src/Aufgabe6.java create mode 100644 AufgabenBlatt6/src/Aufgabe7.java create mode 100644 AufgabenBlatt6/src/Aufgabe8.java create mode 100644 AufgabenBlatt6/src/Aufgabe9.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 76cf03a..ac9e484 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,6 +4,7 @@ + diff --git a/AufgabenBlatt6/.gitignore b/AufgabenBlatt6/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/AufgabenBlatt6/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/AufgabenBlatt6/AufgabenBlatt6.iml b/AufgabenBlatt6/AufgabenBlatt6.iml new file mode 100644 index 0000000..1dc35ea --- /dev/null +++ b/AufgabenBlatt6/AufgabenBlatt6.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AufgabenBlatt6/src/Aufgabe3.java b/AufgabenBlatt6/src/Aufgabe3.java new file mode 100644 index 0000000..eb1088f --- /dev/null +++ b/AufgabenBlatt6/src/Aufgabe3.java @@ -0,0 +1,11 @@ +import java.util.Arrays; + +public class Aufgabe3 { + + public static void main(String[] args) { + int [] array = {1,2,3}; + int zahl = 20; + System.out.println("Array: " + Arrays.toString(array)); + System.out.println("Zahl: " + zahl); + } +} diff --git a/AufgabenBlatt6/src/Aufgabe5.java b/AufgabenBlatt6/src/Aufgabe5.java new file mode 100644 index 0000000..b62b4af --- /dev/null +++ b/AufgabenBlatt6/src/Aufgabe5.java @@ -0,0 +1,14 @@ +import java.util.Arrays; + +public class Aufgabe5 { + + public static void main(String[] args) { + int[] a = { 1, 2 }; + int[] b = { 1, 2 }; + if(Arrays.equals(a, b)){ + System.out.println("Gleich !"); + }else{ + System.out.println("Ungleich !"); + } + } +} diff --git a/AufgabenBlatt6/src/Aufgabe6.java b/AufgabenBlatt6/src/Aufgabe6.java new file mode 100644 index 0000000..2f8136b --- /dev/null +++ b/AufgabenBlatt6/src/Aufgabe6.java @@ -0,0 +1,146 @@ +import org.junit.Test; + +import java.util.Scanner; + +/** + * Generic test class for implementing and testing reading in arrays of integers + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe6 { + + /** + * Read 10 integers from System.in and print them to the screen when done + * This method continues to request more input even when the input is not an integer. + */ + private static void taskA() { + try(var scanner = new Scanner(System.in)) { + var array = new double[10]; + var counter = 0; + + while (true) { + try { + if (counter > 9) { + for (var value : array) { + System.out.println(value); + } + break; + } + + System.out.println("Enter a number: "); + array[counter++] = scanner.nextDouble(); + + } catch (Exception e) { + System.err.println("You did not enter a number"); + } + } + } + } + + /** + * Reads in an integer from System.in and creates an array of integers with the given size + * Read 10 integers from System.in and print them to the screen when done + * This method continues to request more input even when the input is not an integer. + */ + private static void taskB() { + try(var scanner = new Scanner(System.in)) { + + double[] array = null; + var counter = 0; + + // read something in until we allocated an array + while (array == null) { + try { + array = new double[scanner.nextInt()]; + } catch (Exception e) { + System.err.println("You did not enter a valid number"); + } + } + + // fill the array + while (true) { + try { + if (counter >= array.length - 1) { + for (var value : array) { + System.out.println(value); + } + break; + } + + System.out.println("Enter a number: "); + array[counter++] = scanner.nextDouble(); + + } catch (Exception e) { + System.err.println("You did not enter a number"); + } + } + } + } + + /** + * Read 5 integers from System.in and print the median of them + * This method continues to request more input even when the input is not an integer. + */ + private static void taskC() { + try(var scanner = new Scanner(System.in)) { + var array = new double[5]; + var counter = 0; + + while (true) { + try { + if (counter > 5) { + var sum = 0; + for (var value : array) { + sum += value; + } + System.out.println("median: " + (sum / 5.0)); + break; + } + + System.out.println("Enter a number: "); + array[counter++] = scanner.nextDouble(); + + } catch (Exception e) { + System.err.println("You did not enter a number"); + } + } + } + } + + /** + * Prints the content of two arrays as pairs to the screen + */ + private static void taskD() { + int x []= {9,8,7,6,5,4}; + int y []= {1,2,3,4,5,6}; + + for (int i = 0; i < x.length; i++) { + System.out.format("(%d, %d) ", x[i], y[i]); + } + + System.out.println(); + } + + /** + * Test various functions in this class + */ + @Test + public void test() { + // taskA(); + // taskB(); + // taskC(); + // taskD(); + } +} diff --git a/AufgabenBlatt6/src/Aufgabe7.java b/AufgabenBlatt6/src/Aufgabe7.java new file mode 100644 index 0000000..aba7e23 --- /dev/null +++ b/AufgabenBlatt6/src/Aufgabe7.java @@ -0,0 +1,53 @@ +import org.junit.Test; + +/** + * Generic test class for implementing and testing an algorithm to compare two arrays as array of pairs + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe7 { + + /** + * Test if the two arrays are filled with the same pair + * + * @param x the first value of each pair + * @param y the second value of each pair + * @return if the two arrays contain the same pair + */ + private static boolean eq(int[] x, int[] y) { + if (x.length != y.length) + return false; + + for (int i = 0; i < x.length; i++) { + var x2 = x[i]; + var y2 = y[i]; + + for (int j = 0; j < y.length; j++) { + if (x2 != x[j] || y2 != y[j]) + return false; + } + } + + return true; + } + + @Test + public void test() { + int x []= {9,8,7,6,5,4}; + int y []= {1,2,3,4,5,6}; + + System.out.println(eq(x, y)); + } +} diff --git a/AufgabenBlatt6/src/Aufgabe8.java b/AufgabenBlatt6/src/Aufgabe8.java new file mode 100644 index 0000000..dfc698e --- /dev/null +++ b/AufgabenBlatt6/src/Aufgabe8.java @@ -0,0 +1,16 @@ +public class Aufgabe8 { + + public static void main(String args[]) { + double wechselkurs = 0.7238; // USD -> EUR + double[] dollarwerte = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 }; // Array 1 initialisieren + double[] eurowerte = dollarwerte.clone(); // Array 2 mit gleicher Größe + // ^^^^^^^ + // Array muss kopiert werden + + for (int i = 0; i < dollarwerte.length; i++) { + eurowerte[i] = dollarwerte[i] * wechselkurs; // Eurowerte berechnen + // Ergebnisse ausgeben + System.out.println(dollarwerte[i] + "$ sind " + eurowerte[i] + "€."); + } + } +} diff --git a/AufgabenBlatt6/src/Aufgabe9.java b/AufgabenBlatt6/src/Aufgabe9.java new file mode 100644 index 0000000..8120e6f --- /dev/null +++ b/AufgabenBlatt6/src/Aufgabe9.java @@ -0,0 +1,68 @@ +import org.junit.Test; + +import java.util.Arrays; + +/** + * Generic test class for implementing and testing an algorithm to reverse arrays of strings + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +public class Aufgabe9 { + + /** + * Creates a new instance of the supplied strings and returns the reversed array + * @param strings the array of strings to reverse + * @return the reversed array instance + */ + private static String[] reverse1(String[] strings) { + var result = new String[strings.length]; + + for (int i = 0; i < result.length; i++) { + result[strings.length - i - 1] = strings[i]; + } + + return result; + } + + /** + * Reverse the supplied array + * @param strings the array of strings to reverse + */ + private static void reverse2(String[] strings) { + + for (int i = 0; i < strings.length / 2; i++) { + var tmp = strings[i]; + strings[i] = strings[strings.length - i - 1]; + strings[strings.length - i - 1] = tmp; + } + } + + @Test + public void test() { + var STRINGS = new String[] { + "abc", + "def", + "ghi", + "123", + "456", + "789" + }; + + System.out.println(Arrays.toString(reverse1(STRINGS)) + " " + Arrays.toString(STRINGS)); + + reverse2(STRINGS); + System.out.println(Arrays.toString(STRINGS)); + } +}