Java-Programming/AufgabenBlatt6/src/Aufgabe6.java

147 lines
4.5 KiB
Java
Raw Normal View History

2023-04-27 16:43:29 +00:00
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();
}
}