added blatt 5/6

This commit is contained in:
Sven Vogel 2023-04-27 18:43:29 +02:00
parent 083c15bf8b
commit 5698534b55
9 changed files with 359 additions and 0 deletions

View File

@ -4,6 +4,7 @@
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" filepath="$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" filepath="$PROJECT_DIR$/AufgabenBlatt3/AufgabenBlatt3.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" filepath="$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" /> <module fileurl="file://$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" filepath="$PROJECT_DIR$/AufgabenBlatt4/AufgabenBlatt4.iml" />
<module fileurl="file://$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" filepath="$PROJECT_DIR$/AufgabenBlatt6/AufgabenBlatt6.iml" />
<module fileurl="file://$PROJECT_DIR$/GregorianischerKalender/GregorianischerKalender.iml" filepath="$PROJECT_DIR$/GregorianischerKalender/GregorianischerKalender.iml" /> <module fileurl="file://$PROJECT_DIR$/GregorianischerKalender/GregorianischerKalender.iml" filepath="$PROJECT_DIR$/GregorianischerKalender/GregorianischerKalender.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/Java-Programming.iml" filepath="$PROJECT_DIR$/.idea/Java-Programming.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/Java-Programming.iml" filepath="$PROJECT_DIR$/.idea/Java-Programming.iml" />
</modules> </modules>

29
AufgabenBlatt6/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="openjdk-19" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -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);
}
}

View File

@ -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 !");
}
}
}

View File

@ -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();
}
}

View File

@ -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));
}
}

View File

@ -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] + "€.");
}
}
}

View File

@ -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));
}
}