cleaned up

This commit is contained in:
2020-11-13 17:45:25 +01:00
parent 3edae31699
commit ff7d904cab
53 changed files with 259 additions and 182 deletions
+29
View File
@@ -0,0 +1,29 @@
package Livecoding;
public class Methoden {
public static void main(String[]args){
sop("Zinseszins: ");
//sop(Double.toString(zinseszins(50,2,5)),true);
sop(runden(zinseszins(50,2,5),2),true);
//sop(Double.toString(zinsesZinsmitZinsanstieg(500, 30)));
sop(runden(zinsesZinsmitZinsanstieg(500, 30),2),true);
}
private static void sop(String input){
System.out.print(input);
}
private static void sop(String input, boolean newline){
System.out.println(input);
}
private static double zinseszins(double startguthaben, double zinsSatz, int anlageJahre){
double res;
res = startguthaben*Math.pow(1+zinsSatz/100, anlageJahre);
return res;
}
private static int zinsesZinsmitZinsanstieg(int K, int p){
return (K*(p+1))/1;
}
public static String runden(double wert, int dezi){
return String.format("%." + dezi + "f",wert);
}
}
+72
View File
@@ -0,0 +1,72 @@
package Livecoding;
public class Spicker {
public static void main(String[] args){
// impliziter cast
int intZahl = 1;
double dZahl = intZahl;
// expliziter Cast
dZahl = 1.9;
intZahl = (int) dZahl;
boolean istGut = true;
int zahl = 0;
if(!istGut){
System.out.println("A");
} else if(istGut && zahl == 0){
System.out.print("C");
} else {
System.out.println("B");
}
//... weiter im Text
// Switch
int a = 1;
switch(a){
case 1:
System.out.print("Hallo");
break;
case 2:
System.out.print("Ciao");
break;
default:
System.out.print("Nix passiert");
}
// Schleifen
int b = 0, c = 0, d=0;
while(b<010) {b++; }
do{c++;}while(c<=10);
for(int i1= 0; i1<=10; i1++) {d++;}
// Methoden
methodenOhneReturn(b);
int f = methodeMitReturn(2,5);
// arrays:
int[] meinArray = new int[] {3,5,3,2};
int[] meinArray2 = new int[4];
// meinArray[0] = 3; //...
for(int i=0; i<meinArray.length;i++){
System.out.println(meinArray[1]);
}
// Variante 2: foreach -Schleife
for(int element:meinArray)
System.out.println(element);
}
// Methoden
public static void methodenOhneReturn(int a){
System.out.print(a);
}
public static int methodeMitReturn( int a, int b){
return a+b;
}
}
@@ -0,0 +1,7 @@
package Uebungen.Uebung1;
public class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
+37
View File
@@ -0,0 +1,37 @@
package Uebungen.Uebung1;
public class Sum {
public static void main(String[] args){
// Ganze Zahl, bis zu der summiert wird
int boundary;
// Summe der bisher addierten ganzen Zahlen
int sum;
// Ganze Zahl
int counter;
// Festlegen der Startwerte
boundary = 10;
sum = 0;
counter = 1;
for(int i = 0 ; i <= boundary;i++){
System.out.print("-------- boundary set to ");
System.out.print(i);
System.out.print(" -------\n");
// Berechnen der Summe sum über alle Zahlen von 1 bis boundary
while(counter <= i){
sum = sum + counter;
counter = counter + 1;
}
System.out.print("Die Summe der Zahlen von 1 bis ");
System.out.print(i);
System.out.print(" ist ");
System.out.print(sum);
System.out.print("\n---------- fin ---------\n\n");
}
}
}
@@ -0,0 +1,23 @@
package Uebungen.Uebung2;
public class Variablen{
public static void main(String[]args){
byte bVar1 = 5;
short sVar1 = 400;
int iVar1 = -35676;
long lVar1 = 100000000L;
float fVar1 = 0.123f;
double dVar1 = 0.123;
boolean istPrim1 = false;
char cVar1 = 'x';
System.out.println();
System.out.println(bVar1);
System.out.println(sVar1);
System.out.println(iVar1);
System.out.println(lVar1);
System.out.println(fVar1);
System.out.println(dVar1);
System.out.println(cVar1);
System.out.println(istPrim1);
}
}
@@ -0,0 +1,19 @@
package Uebungen.Uebung2;
public class Verdienstrechner{
public static void main(String[] args){
float stunden = 7;
float stundenlohn = 12.0f;
float verdienst = stunden*stundenlohn;
System.out.print("Stundenlohn: ");
System.out.println(stundenlohn);
System.out.println("Stundenlohn: " + stundenlohn);
System.out.println("Stunden insgesamt: " + stunden);
System.out.println("Ich habe " + verdienst + " verdient.");
}
}
@@ -0,0 +1,16 @@
package Uebungen.Uebung3;
import java.util.Scanner;
public class Kreis {
public static void main(String[] args){
double radius, umfang, flaeche;
System.out.print("Radius eingeben: ");
Scanner scanner = new Scanner(System.in);
radius = Double.parseDouble(scanner.nextLine());
umfang = radius*2*Math.PI;
flaeche = Math.pow(radius,2)*Math.PI;
System.out.println("Umfang" + umfang);
System.out.println("Flaeche: " + flaeche);
scanner.close();
}
}
@@ -0,0 +1,20 @@
package Uebungen.Uebung3;
import java.util.Scanner;
public class SatellitenZeit {
public static void main(String[] args) {
long d, h, m, s;
long dR, hR, mR;
Scanner scanner = new Scanner(System.in);
long sekundenIn = Long.parseLong(scanner.nextLine());
d = sekundenIn / (60*60*24);
dR = sekundenIn % (60*60*24);
h = dR / (60*60);
hR = dR % (60*60);
m = hR / 60;
mR = hR % 60;
s = mR;
System.out.println("Tage: " + d + " Stunden: " + h + " Minuten: " + m + " Sekunden: " + s);
scanner.close();
}
}
@@ -0,0 +1,21 @@
package Uebungen.Uebung3;
import java.util.Scanner;
public class Steuer {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
double bruttoBetrag, steuerSatz, nettoBetrag, mWst;
System.out.print("Brutto Betrag eingeben: ");
bruttoBetrag = Double.parseDouble(scanner.nextLine());
System.out.print("Mehrwertsteuersatz eingeben: ");
steuerSatz = Double.parseDouble(scanner.nextLine());
nettoBetrag = bruttoBetrag / (1.0+steuerSatz);
mWst = bruttoBetrag -nettoBetrag;
System.out.println("Netto-Betrag: " + nettoBetrag);
System.out.println("Mehrwertsteuer: " + mWst);
scanner.close();
}
}
@@ -0,0 +1,11 @@
package Uebungen.Uebung3;
import java.util.Scanner;
public class Temperatur {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
double tempIn = Double.parseDouble(scanner.nextLine());
double tempOut = 9.0/5.0 * tempIn + 32.0;
System.out.println(tempIn + " Grad Celsius entsprechen " + tempOut + " Grad Fahrenheit");
scanner.close();
}
}
@@ -0,0 +1,29 @@
package Uebungen.Uebung4;
import java.util.Scanner;
public class Maximum {
public static void main(String[] args){
System.out.print("Bitte geben Sie die Anzahl der Zufallszahlen an: ");
Scanner scanner = new Scanner(System.in);
int arraySize = Integer.parseInt(scanner.next());
double[] valueStore = new double[arraySize];
for (int i=0; i<arraySize;i++){
valueStore[i] = Math.random();
System.out.println("#" + i + " : " + valueStore[i]);
}
System.out.println("Die größte Zahl hat den index: " + max(valueStore));
}
public static int max(double input[]){
int index=0;
double largestNumber = 0;
for(int i=0; i<input.length;i++){
if(largestNumber<input[i]){
largestNumber = input[i];
index = i;
}
}
return index;
}
}
@@ -0,0 +1,34 @@
package Uebungen.Uebung4;
import java.util.Arrays;
public class Schnittmenge {
public static void main(String[] args){
//Scanner scanner = new Scanner(System.in);
int[] set1a = {0, 1, 2, 3, 4, 5};
int[] set1b = {3, 4, 5, 6, 7, 8};
int[] set2a = {0, 1, 2, 3};
int[] set2b = {4, 5, 6};
int[] set3a = {0, 1, 2};
int[] set3b = {0, 1, 2};
System.out.println(Arrays.toString(schnittmenge(set1a, set1b)));
System.out.println(Arrays.toString(schnittmenge(set2a, set2b)));
System.out.println(Arrays.toString(schnittmenge(set3a, set3b)));
}
public static int[] schnittmenge(int[] arrA,int[] arrB){
int resArrSize = 0;
for(int numberA:arrA){
for(int numberB:arrB){
if(numberA==numberB) resArrSize++;
}
}
int[] resultArr = new int[resArrSize];
for(int numberA:arrA){
for(int numberB:arrB){
if(numberA==numberB) resultArr[--resArrSize] = numberA;
}
}
return resultArr;
}
}
@@ -0,0 +1,38 @@
package Uebungen.Uebung4;
import java.util.Scanner;
public class Wechselgeld {
public static void main(String... args){
Scanner scanner = new Scanner(System.in);
System.out.print("Bitte Preis engeben: ");
float preis = Float.parseFloat(scanner.nextLine());
System.out.print("Bitte Betrag engeben: ");
float zahlung = Float.parseFloat(scanner.nextLine());
if(wechselgeld(preis,zahlung)) System.out.println("Vielen Dank!");
else System.out.println("Das reicht leider nicht.");
}
public static boolean wechselgeld(float sollWert, float istWert){
float[] scheine = {100f, 50f, 20f, 10f, 50f};
float[] muenzen = {2f, 1f, 0.50f, 0.20f, 0.10f, 0.05f, 0.02f, 0.01f};
int[] anzScheine = new int[scheine.length];
int[] anzMuenzen = new int[muenzen.length];
if(istWert<sollWert) return false;
else{
float ruekGeld = istWert - sollWert;
System.out.println("_______________ Scheine _____________");
for(int i=0; i<scheine.length; i++){
anzScheine[i] = (int) (ruekGeld / scheine[i]);
ruekGeld -= scheine[i]*(float)anzScheine[i];
if(anzScheine[i]>0) System.out.println(anzScheine[i] + " x " + scheine[i] + " EUR");
}
System.out.println("_______________ Muenzen _____________");
for(int i=0; i<muenzen.length; i++){
anzMuenzen[i] = (int) (ruekGeld / muenzen[i]);
ruekGeld -= muenzen[i]*(float)anzMuenzen[i];
if(anzMuenzen[i]>0) System.out.println(anzMuenzen[i] + " x " + (int)(muenzen[i]*100) + " ct");
}
return true;
}
}
}
@@ -0,0 +1,57 @@
package Uebungen.Uebung5;
import java.util.Scanner;
public class Aufgabe1 {
public static void main(String[] args){
// initiate scanner for input
Scanner scanner = new Scanner(System.in);
// primes till 100 for testing
//int[] primes = new int[] { 2, 3,7, 13, 17, 23, 31, 37, 43, 53, 59, 67, 73, 79, 89 };
// select number range
while(true){
userInterface();
System.out.print("\n\n Check anoter range? [y/N]: ");
if(scanner.nextLine().charAt(0)!='y') break;
System.out.println();
}
}
public static void userInterface(){
Scanner scanner = new Scanner(System.in);
int rangeMin=0, rangeMax=0;
do{
System.out.print("\nEnter lowest number from range: ");
rangeMin = Integer.parseInt(scanner.nextLine());
if(rangeMin<2) System.out.print("[WARNING] Number must be greater than \"1\"!\n");
}while(rangeMin<2);
do{
System.out.print("\nEnter greatest number from range: ");
rangeMax = Integer.parseInt(scanner.nextLine());
if(rangeMax<rangeMin) System.out.print("[WARNING] Number must be at least equal or greater than \"" + rangeMin + "\"!\n");
}while(rangeMax<rangeMin);
int numAmount = rangeMax - rangeMin;
int[] numbers = new int[numAmount+1];
System.out.print("Adding numbers => ");
for(int i=0; i<=numAmount;i++){
numbers[i] = i+rangeMin;
//System.out.print("number["+i+"]="+(i+rangeMin)+" ");
}
System.out.println();
System.out.print("Prime numbers => ");
for(int number:numbers){
if(isPrime(number)) System.out.print(number + ", ");
}
}
public static boolean isPrime(int number){
boolean res=true; // output variable
// test through all divisors form 2 to number-1
for(int i=2;i<number;i++){
if(number%i==0){
res=false;
//System.out.print("("+ number%i + ")" ); // Debug
break;
}
}
return res;
}
}
@@ -0,0 +1,69 @@
package Uebungen.Uebung5;
import java.util.Scanner;
import java.lang.Character;
import java.util.Arrays;
public class Aufgabe2 {
public static void main(String[] args){
// initiate scanner for input
Scanner scanner = new Scanner(System.in);
while(true){
userInterface();
System.out.print("\n\n Check anoter string? [y/N]: ");
if(scanner.nextLine().charAt(0)!='y') break;
System.out.println();
}
}
public static void userInterface() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter desired string: ");
char[] stringAsCharArr = scanner.nextLine().toCharArray();
char[] usedCharArr = usedChars2(stringAsCharArr);
for(char character:usedCharArr) System.out.println(" \""+ character + "\""); // Debug
// System.out.println(stringAsCharArr.hashCode()); // Get the hashcode of the array
}
public static char[] usedChars(char[] charArray){
char[] workingArray = new char[charArray.length];
int position = 0; // current position
for(char currChar:charArray) {
currChar=Character.toLowerCase(currChar); //normalize
boolean exists=false;
for(char targetChar:workingArray) if(targetChar==currChar) exists=true;
if(!exists){
workingArray[position]=currChar;
position++;
}
}
char[] targestArray = new char[position];
for(int i=0;i<position;i++) targestArray[i] = workingArray[i];
Arrays.sort(targestArray);
return targestArray;
}
// Improved function: less code, less demanding
public static char[] usedChars2(char[] workingArray){
// workingArray gets an new hashcode after modifying, so its okay using it
// now we remove all characters that occur multiple times
// we also count how many chars we got
int counter=0;
for(int i=0; i<workingArray.length;i++){
if((int)workingArray[i]!=0) {
counter++; // found a new one -> increment counter
// now remove all similar chars from that point till the end of the array
for (int j = i + 1; j < workingArray.length; j++) {
if (workingArray[i] == workingArray[j]) workingArray[j] = (char) 0; // set the char to "Null character"
}
}
}
char[] targetArray = new char[counter]; // create new
int targetPosition = 0; // initiate counter for the Target Array
for(char currChar:workingArray){ // seek through all chars
if((int)currChar!=0){ // if its not a null character it is a new char
targetArray[targetPosition] = currChar; // add the current char
targetPosition++; // we added a new char, so we need to increment for the next one
}
}
return targetArray;
}
}