jannisfingerhut
2 years ago
3 changed files with 340 additions and 0 deletions
-
30bin/LernProgramm/FunktionenAusgelagert.java
-
207bin/LernProgramm/ProgrammMain.java
-
103bin/LernProgramm/testProgramm.java
@ -0,0 +1,30 @@ |
|||||
|
package LernProgramm; |
||||
|
|
||||
|
import java.util.Scanner; |
||||
|
|
||||
|
public class FunktionenAusgelagert { |
||||
|
public static void schaltjahr() { |
||||
|
|
||||
|
System.out.println("Welches Jahr möchtest du untersuchen?"); |
||||
|
|
||||
|
try (Scanner scanner2 = new Scanner(System.in)) { |
||||
|
String jahr = scanner2.nextLine(); |
||||
|
int jahr1 = Integer.parseInt(jahr); |
||||
|
|
||||
|
// Bestimmen+Ausgabe |
||||
|
|
||||
|
if (jahr1 % 400 == 0) |
||||
|
System.out.println("Schaltjahr!"); |
||||
|
else if (jahr1 % 100 == 0) |
||||
|
System.out.println("Kein Schaltjahr!"); |
||||
|
else if (jahr1 % 4 == 0) |
||||
|
System.out.println("Schaltjahr!"); |
||||
|
else |
||||
|
System.out.println("Kein Schaltjahr!"); |
||||
|
} catch (NumberFormatException e) { |
||||
|
// TODO Auto-generated catch block |
||||
|
e.printStackTrace(); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,207 @@ |
|||||
|
package LernProgramm; |
||||
|
|
||||
|
import java.util.Random; |
||||
|
import java.util.Scanner; |
||||
|
|
||||
|
public class ProgrammMain { |
||||
|
// Aufzurufende Funktionen |
||||
|
|
||||
|
// Karteikarten |
||||
|
|
||||
|
public static void Karteikarten() { |
||||
|
|
||||
|
try (Scanner input = new Scanner(System.in)) { |
||||
|
|
||||
|
String[][] karteikarten = { { "Was ist die Hauptstadt von Deutschland?", "Berlin" }, |
||||
|
{ "Welches ist der größtes Planet in unserem Sonnensystem?", "Jupiter" }, |
||||
|
{ "Wer hat die Mona Lisa gemalt?", "Leonardo da Vinci" }, |
||||
|
{ "Wer ist der Bundeskanzler von Deutschland?", "Olaf Scholz" }, |
||||
|
|
||||
|
// Sonstige Fragen |
||||
|
}; |
||||
|
|
||||
|
int counter = 0; |
||||
|
for (String[] karteikarte : karteikarten) { |
||||
|
System.out.println(karteikarte[0]); |
||||
|
String answer = input.nextLine(); |
||||
|
if (answer.equalsIgnoreCase(karteikarte[1])) { |
||||
|
System.out.println("Korrekt!"); |
||||
|
counter++; |
||||
|
} else { |
||||
|
System.out.println("Leider falsch. Die richtige Antwort wäre: " + karteikarte[1]); |
||||
|
} |
||||
|
} |
||||
|
System.out.println("Dein Punktestand ist " + counter + " von insgesamt " + karteikarten.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!"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
System.out.println("Willkommen bei diesem kleinen konsolenbasierten Spiel!\n"); |
||||
|
System.out.println("Du hast 3 Spielmodi!\n"); |
||||
|
System.out.println("1. Karteikarten\n"); |
||||
|
System.out.println("2. Quizz\n"); |
||||
|
System.out.println("3. Binaer-Inverter\n"); |
||||
|
System.out.println("4. PrimZahlenAusgabe\n"); |
||||
|
|
||||
|
try (Scanner einleser = new Scanner(System.in)) { |
||||
|
int wahl = einleser.nextInt(); |
||||
|
switch (wahl) { |
||||
|
case 1: |
||||
|
Karteikarten(); |
||||
|
break; |
||||
|
case 2: |
||||
|
Quizz(); |
||||
|
// Funktion |
||||
|
break; |
||||
|
case 3: |
||||
|
Binaerrechner(); |
||||
|
// Funktion |
||||
|
break; |
||||
|
case 4: |
||||
|
PrimBis100(); |
||||
|
// Funktion |
||||
|
break; |
||||
|
case 5: |
||||
|
|
||||
|
System.out.println("Diese Funktion wird derzeit entwickelt! Hab bitte etwas Geduld oder sei kreativ und erstelle dir selber eine Funktion!"); |
||||
|
// Funktion |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
System.out.println("Ungültige Eingabe, versuche es bitte erneut!\n"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
System.out.println("Programm beendet\n"); |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,103 @@ |
|||||
|
package LernProgramm; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
|
||||
|
import java.io.ByteArrayInputStream; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.PrintStream; |
||||
|
|
||||
|
import org.junit.jupiter.api.Test; |
||||
|
|
||||
|
|
||||
|
class testProgramm { |
||||
|
|
||||
|
@Test |
||||
|
void test() { |
||||
|
assertTrue(true); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testPrimBis100() { |
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
System.setOut(new PrintStream(out)); |
||||
|
|
||||
|
ProgrammMain.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()); |
||||
|
} |
||||
|
@Test |
||||
|
public void testAddition() { |
||||
|
int result = ProgrammMain.calculate("1010", "1011", '+'); |
||||
|
assertEquals(10101, result); |
||||
|
} |
||||
|
@Test |
||||
|
public void testSubtraction() { |
||||
|
int result = ProgrammMain.calculate("1010", "1011", '-'); |
||||
|
result++; |
||||
|
if (result <0 || result >0) { |
||||
|
assertTrue(true); |
||||
|
} |
||||
|
} |
||||
|
@Test |
||||
|
public void testMultiplication() { |
||||
|
int result = ProgrammMain.calculate("1010", "1011", '*'); |
||||
|
result++; |
||||
|
if (result <0 || result >0) { |
||||
|
assertTrue(true); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDivision() { |
||||
|
int result = ProgrammMain.calculate("1010", "1011", '/'); |
||||
|
assertEquals(0, result); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testInvalidOperation() { |
||||
|
int result = ProgrammMain.calculate("1010", "1011", '%'); |
||||
|
assertEquals(0, result); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testAddition1() { |
||||
|
|
||||
|
double result = 2 + 3.5; |
||||
|
assertEquals(5.5, result, 0); |
||||
|
} |
||||
|
@Test |
||||
|
public void testSubtraction1() { |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
double result = 5-3.5; |
||||
|
assertEquals(1.5, result, 0); |
||||
|
} |
||||
|
@Test |
||||
|
public void testMultiplication1() { |
||||
|
double result = 5*3.5; |
||||
|
assertEquals(17.5, result, 0); |
||||
|
} |
||||
|
@Test |
||||
|
public void testDivision1() { |
||||
|
double result = 15/10; |
||||
|
assertEquals(1, result, 0); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
@Test |
||||
|
public void testSchaltjahr() { |
||||
|
ByteArrayInputStream in = new ByteArrayInputStream("2000\n".getBytes()); |
||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||
|
System.setIn(in); |
||||
|
System.setOut(new PrintStream(out)); |
||||
|
|
||||
|
FunktionenAusgelagert.schaltjahr(); |
||||
|
|
||||
|
assertEquals("Welches Jahr möchtest du untersuchen?\nSchaltjahr!\n", out.toString()); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue