diff --git a/bin/LernProgramm/FunktionenAusgelagert.java b/bin/LernProgramm/FunktionenAusgelagert.java index da750f0..0edf192 100644 --- a/bin/LernProgramm/FunktionenAusgelagert.java +++ b/bin/LernProgramm/FunktionenAusgelagert.java @@ -1,5 +1,6 @@ package LernProgramm; +import java.util.Random; import java.util.Scanner; public class FunktionenAusgelagert { @@ -94,10 +95,124 @@ public class FunktionenAusgelagert { //4 - + public static void Quizz() { + Random rand = new Random(); + try (Scanner scanner = new Scanner(System.in)) { + String[][] questions = { + { "Welche Farbe hat ein Bananen?", "A) Gelb", "B) Grün", "C) Blau", "D) Rot", "A" }, + { "Wie viele Beine hat eine Spinne?", "A) 4", "B) 6", "C) 8", "D) 10", "C" }, + { "Wer hat die Formel E=mc² entwickelt?", "A) Isaac Newton", "B) Albert Einstein", + "C) Galileo Galilei", "D) Stephen Hawking", "B" }, + { "Welches ist der größte Planet im Sonnensystem?", "A) Merkur", "B) Venus", "C) Erde", + "D) Jupiter", "D" } + // Sonstige Fragen + }; + int questionIndex = rand.nextInt(questions.length); + String[] currentQuestion = questions[questionIndex]; + + System.out.println(currentQuestion[0]); + System.out.println(currentQuestion[1]); + System.out.println(currentQuestion[2]); + System.out.println(currentQuestion[3]); + System.out.println(currentQuestion[4]); + String answer = scanner.nextLine(); + + if (answer.equalsIgnoreCase(currentQuestion[5])) { + System.out.println("Richtig!"); + } else { + System.out.println("Falsch!"); + } + + } + } + //5 + public static void Binaerrechner() { + + try (Scanner scanner = new Scanner(System.in)) { + System.out.print("Gebe den ersten Binärcode ein: "); + String binaryCode1 = scanner.nextLine(); + System.out.print("Gebe den zweiten Binärcode ein: "); + String binaryCode2 = scanner.nextLine(); + System.out.print("Gebe die gewünschte Operation ein (+, -, *, /): "); + char operation = scanner.next().charAt(0); + + int result = calculate(binaryCode1, binaryCode2, operation); + System.out.println("Das Ergebnis ist: " + result); + } + } + + public static int calculate(String binaryCode1, String binaryCode2, char operation) { + + int decimal1 = binaryToDecimal(binaryCode1); + int decimal2 = binaryToDecimal(binaryCode2); + + int result = 0; + switch (operation) { + case '+': + result = decimal1 + decimal2; + break; + case '-': + result = decimal1 - decimal2; + break; + case '*': + result = decimal1 * decimal2; + break; + case '/': + result = decimal1 / decimal2; + break; + default: + System.out.println("Ungültige Operation! Bitte wähle +, -, * oder /."); + return 0; + } + return decimalToBinary(result); + } + public static int binaryToDecimal(String binaryCode) { + int decimal = 0; + for (int i = binaryCode.length() - 1; i >= 0; i--) { + char currentChar = binaryCode.charAt(i); + if (currentChar == '1') { + decimal += Math.pow(2, binaryCode.length() - i - 1); + } else if (currentChar != '0') { + System.out.println("Ungültiger Binärcode! Bitte gebe nur Nullen und Einsen ein."); + return 0; + } + } + return decimal; + } + + public static int decimalToBinary(int decimal) { + int binary = 0; + int power = 0; + while (decimal > 0) { + binary += (decimal % 2) * (int) Math.pow(10, power); + decimal /= 2; + power++; + } + return binary; + } + + + + + +//6 + public static void PrimBis100() { + for (int i = 2; i <= 100; i++) { + boolean isPrime = true; + for (int j = 2; j < i; j++) { + if (i % j == 0) { + isPrime = false; + break; + } + } + if (isPrime) { + System.out.print(i + " "); + } + } + } diff --git a/bin/LernProgramm/ProgrammMain.java b/bin/LernProgramm/ProgrammMain.java index 472d7a4..13b5aa2 100644 --- a/bin/LernProgramm/ProgrammMain.java +++ b/bin/LernProgramm/ProgrammMain.java @@ -1,129 +1,8 @@ package LernProgramm; -import java.util.Random; import java.util.Scanner; public class ProgrammMain { - - - -// Quizz - - public static void Quizz() { - Random rand = new Random(); - try (Scanner scanner = new Scanner(System.in)) { - String[][] questions = { - { "Welche Farbe hat ein Bananen?", "A) Gelb", "B) Grün", "C) Blau", "D) Rot", "A" }, - { "Wie viele Beine hat eine Spinne?", "A) 4", "B) 6", "C) 8", "D) 10", "C" }, - { "Wer hat die Formel E=mc² entwickelt?", "A) Isaac Newton", "B) Albert Einstein", - "C) Galileo Galilei", "D) Stephen Hawking", "B" }, - { "Welches ist der größte Planet im Sonnensystem?", "A) Merkur", "B) Venus", "C) Erde", - "D) Jupiter", "D" } - // Sonstige Fragen - }; - int questionIndex = rand.nextInt(questions.length); - String[] currentQuestion = questions[questionIndex]; - - System.out.println(currentQuestion[0]); - System.out.println(currentQuestion[1]); - System.out.println(currentQuestion[2]); - System.out.println(currentQuestion[3]); - System.out.println(currentQuestion[4]); - String answer = scanner.nextLine(); - - if (answer.equalsIgnoreCase(currentQuestion[5])) { - System.out.println("Richtig!"); - } else { - System.out.println("Falsch!"); - } - - } - } - -// Prinmbis100 - public static void PrimBis100() { - for (int i = 2; i <= 100; i++) { - boolean isPrime = true; - for (int j = 2; j < i; j++) { - if (i % j == 0) { - isPrime = false; - break; - } - } - if (isPrime) { - System.out.print(i + " "); - } - } - } - - // Binärinverter - - public static void Binaerrechner() { - - try (Scanner scanner = new Scanner(System.in)) { - System.out.print("Gebe den ersten Binärcode ein: "); - String binaryCode1 = scanner.nextLine(); - System.out.print("Gebe den zweiten Binärcode ein: "); - String binaryCode2 = scanner.nextLine(); - System.out.print("Gebe die gewünschte Operation ein (+, -, *, /): "); - char operation = scanner.next().charAt(0); - - int result = calculate(binaryCode1, binaryCode2, operation); - System.out.println("Das Ergebnis ist: " + result); - } - } - - public static int calculate(String binaryCode1, String binaryCode2, char operation) { - - int decimal1 = binaryToDecimal(binaryCode1); - int decimal2 = binaryToDecimal(binaryCode2); - - int result = 0; - switch (operation) { - case '+': - result = decimal1 + decimal2; - break; - case '-': - result = decimal1 - decimal2; - break; - case '*': - result = decimal1 * decimal2; - break; - case '/': - result = decimal1 / decimal2; - break; - default: - System.out.println("Ungültige Operation! Bitte wähle +, -, * oder /."); - return 0; - } - return decimalToBinary(result); - } - - public static int binaryToDecimal(String binaryCode) { - - int decimal = 0; - for (int i = binaryCode.length() - 1; i >= 0; i--) { - char currentChar = binaryCode.charAt(i); - if (currentChar == '1') { - decimal += Math.pow(2, binaryCode.length() - i - 1); - } else if (currentChar != '0') { - System.out.println("Ungültiger Binärcode! Bitte gebe nur Nullen und Einsen ein."); - return 0; - } - } - return decimal; - } - - public static int decimalToBinary(int decimal) { - int binary = 0; - int power = 0; - while (decimal > 0) { - binary += (decimal % 2) * (int) Math.pow(10, power); - decimal /= 2; - power++; - } - return binary; - } @@ -144,15 +23,15 @@ public class ProgrammMain { FunktionenAusgelagert.Karteikarten(); break; case 2: - Quizz(); + FunktionenAusgelagert.Quizz(); // Funktion break; case 3: - Binaerrechner(); + FunktionenAusgelagert.Binaerrechner(); // Funktion break; case 4: - PrimBis100(); + FunktionenAusgelagert.PrimBis100(); // Funktion break; case 5: diff --git a/bin/LernProgramm/testProgramm.java b/bin/LernProgramm/testProgramm.java index 0a67734..592c489 100644 --- a/bin/LernProgramm/testProgramm.java +++ b/bin/LernProgramm/testProgramm.java @@ -23,19 +23,19 @@ class testProgramm { ByteArrayOutputStream out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out)); - ProgrammMain.PrimBis100(); + FunktionenAusgelagert.PrimBis100(); assertEquals("2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ", out.toString()); } //Binärrechner @Test public void testAddition() { - int result = ProgrammMain.calculate("1010", "1011", '+'); + int result = FunktionenAusgelagert.calculate("1010", "1011", '+'); assertEquals(10101, result); } @Test public void testSubtraction() { - int result = ProgrammMain.calculate("1010", "1011", '-'); + int result = FunktionenAusgelagert.calculate("1010", "1011", '-'); result++; if (result <0 || result >0) { assertTrue(true); @@ -43,7 +43,7 @@ class testProgramm { } @Test public void testMultiplication() { - int result = ProgrammMain.calculate("1010", "1011", '*'); + int result = FunktionenAusgelagert.calculate("1010", "1011", '*'); result++; if (result <0 || result >0) { assertTrue(true); @@ -51,13 +51,13 @@ class testProgramm { } @Test public void testDivision() { - int result = ProgrammMain.calculate("1010", "1011", '/'); + int result = FunktionenAusgelagert.calculate("1010", "1011", '/'); assertEquals(0, result); } @Test public void testInvalidOperation() { - int result = ProgrammMain.calculate("1010", "1011", '%'); + int result = FunktionenAusgelagert.calculate("1010", "1011", '%'); assertEquals(0, result); }