Jannis Fingerhut
2 years ago
4 changed files with 225 additions and 10 deletions
-
BINgruppenprojekt/.DS_Store
-
10gruppenprojekt/Aaaa.java
-
158gruppenprojekt/Main.java
-
67gruppenprojekt/TestKlasee.txt
@ -1,10 +0,0 @@ |
|||||
package gruppenprojekt; |
|
||||
|
|
||||
public class Aaaa { |
|
||||
|
|
||||
public static void main(String[] args) { |
|
||||
// TODO Auto-generated method stub |
|
||||
System.out.println("Hello World"); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -0,0 +1,158 @@ |
|||||
|
import java.util.Random; |
||||
|
import java.util.Scanner; |
||||
|
|
||||
|
public class Main { |
||||
|
|
||||
|
public static void Karteikarten() { |
||||
|
try (Scanner input = new Scanner(System.in)) { |
||||
|
String[][] flashcards = {{"Was ist die Hauptstadt von Deutschland?", "Berlin"}, |
||||
|
{"Welches ist der größtes Planet in unserem Sonnensystem?", "Jupiter"}, |
||||
|
{"Was hat die Mona Lisa gemalt?", "Leonardo da Vinci"}, |
||||
|
//Sonstige Fragen |
||||
|
}; |
||||
|
|
||||
|
int score = 0; |
||||
|
for (String[] flashcard : flashcards) { |
||||
|
System.out.println(flashcard[0]); |
||||
|
String answer = input.nextLine(); |
||||
|
if (answer.equalsIgnoreCase(flashcard[1])) { |
||||
|
System.out.println("Korrekt!"); |
||||
|
score++; |
||||
|
} else { |
||||
|
System.out.println("Leider falsch. Die richtige Antwort wäre: " + flashcard[1]); |
||||
|
} |
||||
|
} |
||||
|
System.out.println("Das Spiel ist zu Ende. Dein Punktestand ist " + score + " von insgesamt " + flashcards.length); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 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!"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Binärinverter |
||||
|
|
||||
|
public static void Binaerrechner() { |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
System.out.println("Willkommen bei dem kleinen konsolenbasierten Lernprogramm!\n Du hast 3 Spielmodi:\n"); |
||||
|
System.out.println("1. Karteikarten (1)\n"); |
||||
|
System.out.println("2. Quizz(2)\n"); |
||||
|
System.out.println("3. Binaer-Inverter(3)\n"); |
||||
|
|
||||
|
System.out.println("Gebe eine Zahl ein:"); |
||||
|
|
||||
|
|
||||
|
try (Scanner scanner = new Scanner(System.in)) { |
||||
|
int wahl = scanner.nextInt(); |
||||
|
switch (wahl) { |
||||
|
case 1: |
||||
|
Karteikarten(); |
||||
|
break; |
||||
|
|
||||
|
case 2: |
||||
|
Quizz(); |
||||
|
break; |
||||
|
|
||||
|
case 3: |
||||
|
Binaerrechner(); |
||||
|
break; |
||||
|
default: |
||||
|
System.out.println("Ungültige Eingabe, versuche es erneut!"); |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
System.out.println("Wir hoffen, dir hat das Programm gefallen.\n" + |
||||
|
"Du kannst es selber um weitere Funktionen erweitern, wenn du möchtest!"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,67 @@ |
|||||
|
import static org.junit.Assert.assertEquals; |
||||
|
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.PrintStream; |
||||
|
import java.util.Random; |
||||
|
|
||||
|
import org.junit.Test; |
||||
|
|
||||
|
public class TestKlasee { |
||||
|
|
||||
|
|
||||
|
@Test |
||||
|
public void testKarteikarten() { |
||||
|
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
||||
|
System.setOut(new PrintStream(outContent)); |
||||
|
|
||||
|
Programm.Karteikarten(); |
||||
|
String expectedOutput = "Was ist die Hauptstadt von Deutschland?\n" + |
||||
|
"Berlin\n" + |
||||
|
"Korrekt!\n" + |
||||
|
"Welches ist der größtes Planet in unserem Sonnensystem?\n" + |
||||
|
"Jupiter\n" + |
||||
|
"Korrekt!\n" + |
||||
|
"Was hat die Mona Lisa gemalt?\n" + |
||||
|
"Leonardo da Vinci\n" + |
||||
|
"Korrekt!\n" + |
||||
|
"Das Spiel ist zu Ende. Dein Punktestand ist 3 von insgesamt 3\n"; |
||||
|
|
||||
|
assertEquals(expectedOutput, outContent.toString()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testQuizz() { |
||||
|
Random rand = new Random(); |
||||
|
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
||||
|
System.setOut(new PrintStream(outContent)); |
||||
|
|
||||
|
Programm.Quizz(); |
||||
|
String expectedOutput = "Wer hat die Formel E=mc² entwickelt?\n" + |
||||
|
"A) Isaac Newton\n" + |
||||
|
"B) Albert Einstein\n" + |
||||
|
"C) Galileo Galilei\n" + |
||||
|
"D) Stephen Hawking\n" + |
||||
|
"B\n" + |
||||
|
"Richtig!\n"; |
||||
|
|
||||
|
assertEquals(expectedOutput, outContent.toString()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testBinaerrechner() { |
||||
|
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
||||
|
System.setOut(new PrintStream(outContent)); |
||||
|
|
||||
|
Programm.Binaerrechner(); |
||||
|
String expectedOutput = "Gebe den ersten Binärcode ein: 11\n" + |
||||
|
"Gebe den zweiten Binärcode ein: 10\n" + |
||||
|
"Gebe die gewünschte Operation ein (+, -, *, /): +\n" + |
||||
|
"Das Ergebnis ist: 101\n"; |
||||
|
|
||||
|
assertEquals(expectedOutput, outContent.toString()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue