Aufgaben Vorlesung 2 vollständig
This commit is contained in:
parent
b8396881f7
commit
fd52022f7c
49
Semester2/Vorlesung2.java
Normal file
49
Semester2/Vorlesung2.java
Normal file
@ -0,0 +1,49 @@
|
||||
package Semester2;
|
||||
|
||||
|
||||
public class Vorlesung2 {
|
||||
|
||||
private int value;
|
||||
private Vorlesung2 nextElement;
|
||||
private Vorlesung2 previousElement;
|
||||
|
||||
public Vorlesung2(int value, Vorlesung2 nextElement, Vorlesung2 previousElement) {
|
||||
this.value = value;
|
||||
this.nextElement = nextElement;
|
||||
this.previousElement = previousElement;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vorlesung2 stack = new Vorlesung2(0, null, null);
|
||||
stack.push(7);
|
||||
stack.push(3);
|
||||
stack.push(8);
|
||||
stack.push(2);
|
||||
System.out.println(stack.pop());
|
||||
System.out.println(stack.pop());
|
||||
stack.push(7);
|
||||
System.out.println(stack.pop());
|
||||
System.out.println(stack.pop());
|
||||
}
|
||||
|
||||
|
||||
public void push(int i) {
|
||||
if (this.nextElement != null) {
|
||||
this.nextElement.push(i);
|
||||
} else {
|
||||
this.nextElement = new Vorlesung2(i, null, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int pop() {
|
||||
if (this.nextElement != null) {
|
||||
return nextElement.pop();
|
||||
} else {
|
||||
if (this.previousElement != null) {
|
||||
this.previousElement.nextElement = null;
|
||||
}
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
}
|
73
Semester2/Vorlesung2part2.java
Normal file
73
Semester2/Vorlesung2part2.java
Normal file
@ -0,0 +1,73 @@
|
||||
package Semester2;
|
||||
|
||||
public class Vorlesung2part2<T> {
|
||||
private Object[] myArray;
|
||||
private int numberOfElements;
|
||||
private int maxSize;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vorlesung2part2 testObjekt = new Vorlesung2part2<>();
|
||||
for (int i = 0; i < 300; i++) {
|
||||
if (i < 150) {
|
||||
testObjekt.add(new Vorlesung2part2<>());
|
||||
} else {
|
||||
testObjekt.get();
|
||||
}
|
||||
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Vorlesung2part2() {
|
||||
this.numberOfElements = 0;
|
||||
this.maxSize = 100;
|
||||
myArray = new Object[this.maxSize];
|
||||
}
|
||||
|
||||
|
||||
public void add(T t) {
|
||||
this.myArray[this.numberOfElements] = t;
|
||||
this.numberOfElements++;
|
||||
this.check();
|
||||
}
|
||||
|
||||
|
||||
public T get() {
|
||||
T retValue = (T) this.myArray[0];
|
||||
for (int i=0; i<this.numberOfElements; i++) {
|
||||
this.myArray[i] = this.myArray[i+1];
|
||||
}
|
||||
this.numberOfElements--;
|
||||
this.check();
|
||||
return retValue;
|
||||
}
|
||||
|
||||
|
||||
public int size() {
|
||||
return this.numberOfElements;
|
||||
}
|
||||
|
||||
|
||||
public void check() {
|
||||
Object[] temp = myArray;
|
||||
if ((float)this.numberOfElements/(float)this.maxSize > 0.8) {
|
||||
this.maxSize *= 2;
|
||||
temp = myArray;
|
||||
} else if (((float)this.numberOfElements/(float)this.maxSize < 0.2) && (this.maxSize >= 40)) {
|
||||
this.maxSize /= 2;
|
||||
temp = myArray;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
myArray = new Object[this.maxSize];
|
||||
for (int i = 0; i < this.numberOfElements; i++) {
|
||||
myArray[i] = temp[i];
|
||||
System.out.println(myArray[i]);
|
||||
}
|
||||
|
||||
System.out.println("New size: " + this.maxSize);
|
||||
}
|
||||
}
|
48
Semester2/Vorlesung2part3.java
Normal file
48
Semester2/Vorlesung2part3.java
Normal file
@ -0,0 +1,48 @@
|
||||
package Semester2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
|
||||
public class Vorlesung2part3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
LinkedList<String> linkedList = new LinkedList<>();
|
||||
ArrayList<String> arrayList = new ArrayList<>();
|
||||
|
||||
int[] anzahl = {10000, 20000, 50000, 100000, 200000, 500000,
|
||||
1000000, 2000000, 5000000, 10000000, 20000000, 50000000};
|
||||
|
||||
|
||||
for (int i: anzahl) {
|
||||
long timeStartLinked = System.currentTimeMillis();
|
||||
for (int j = 0; j < i; j++) {
|
||||
linkedList.add(generiereZeichenkette());
|
||||
}
|
||||
long timeEndLinked = System.currentTimeMillis();
|
||||
long elapsedTimeLinked = timeEndLinked - timeStartLinked;
|
||||
System.out.println("Linked List time for " + i + " Data: " + elapsedTimeLinked);
|
||||
|
||||
long timeStartArray = System.currentTimeMillis();
|
||||
for (int j = 0; j < i; j++) {
|
||||
arrayList.add(generiereZeichenkette());
|
||||
}
|
||||
long timeEndArray = System.currentTimeMillis();
|
||||
long elapsedTimeArray = timeEndArray - timeStartArray;
|
||||
System.out.println("Array List time for " + i + " Data: " + elapsedTimeArray);
|
||||
System.out.println("\n");
|
||||
|
||||
linkedList.clear();
|
||||
arrayList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private static String generiereZeichenkette() {
|
||||
Random random = new Random();
|
||||
String wort = "";
|
||||
for (int i = 0; i < random.nextInt(15, 20); i++) {
|
||||
wort += (char) random.nextInt(65, 122);
|
||||
}
|
||||
return wort;
|
||||
}
|
||||
}
|
@ -1,151 +0,0 @@
|
||||
package Semester2;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class test {
|
||||
|
||||
// [6 Kundennummer, 20 Nachname, 20 Vorname, 20 Straße, 5 Hausnummer, 5 PLZ, 20 Ort, int Umsatz]
|
||||
|
||||
private ArrayList<String> nachnamen = erstelleArrays(0);
|
||||
private ArrayList<String> vornamen = erstelleArrays(1);
|
||||
private ArrayList<String> strassen = erstelleArrays(2);
|
||||
private ArrayList<String> plzort = erstelleArrays(3);
|
||||
|
||||
public test() {
|
||||
this.nachnamen = erstelleArrays(0);
|
||||
this.vornamen = erstelleArrays(1);
|
||||
this.strassen = erstelleArrays(2);
|
||||
this.plzort = erstelleArrays(3);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vorlesung1 liste = new Vorlesung1();
|
||||
try {
|
||||
BufferedReader eingabe = new BufferedReader(new InputStreamReader(System.in));
|
||||
String laenge;
|
||||
System.out.println("Wie viele Testsätze: ");
|
||||
laenge = eingabe.readLine();
|
||||
String was;
|
||||
System.out.println("Als CSV?");
|
||||
was = eingabe.readLine();
|
||||
|
||||
if (was.equals("1")) {
|
||||
liste.mitTrennzeichen(Integer.parseInt(laenge));
|
||||
} else {
|
||||
liste.mitLeerzeichen(Integer.parseInt(laenge));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static ArrayList<String> erstelleArrays(int index) {
|
||||
ArrayList<String> rueckgabe = new ArrayList<>();
|
||||
String zeile;
|
||||
String verzeichnis = System.getProperty("user.dir"); // Aktuelles Arbeitsverzeichnis
|
||||
String[] dateien = new String[]{
|
||||
verzeichnis + File.separator + "12_Dateien_Fuer_Aufgabe1" + File.separator + "Nachnamen.txt",
|
||||
verzeichnis + File.separator + "12_Dateien_Fuer_Aufgabe1" + File.separator + "Vornamen.txt",
|
||||
verzeichnis + File.separator + "12_Dateien_Fuer_Aufgabe1" + File.separator + "Strassen.txt",
|
||||
verzeichnis + File.separator + "12_Dateien_Fuer_Aufgabe1" + File.separator + "PLZOrt.txt"
|
||||
};
|
||||
try {
|
||||
BufferedReader leseQuelle = new LineNumberReader(new FileReader(dateien[index]));
|
||||
while ((zeile = leseQuelle.readLine()) != null) {
|
||||
rueckgabe.add(zeile);
|
||||
}
|
||||
leseQuelle.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return rueckgabe;
|
||||
}
|
||||
|
||||
|
||||
private static String fuelleMitLeerzeichen(ArrayList<String> liste, int laenge) {
|
||||
Random random = new Random();
|
||||
String neu = liste.get(random.nextInt(liste.size()));
|
||||
if (neu.length() < laenge) {
|
||||
neu = neu + " ".repeat(laenge - neu.length());
|
||||
}
|
||||
return neu;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void mitTrennzeichen(int anzahl) {
|
||||
try {
|
||||
String verzeichnis = System.getProperty("user.dir"); // Aktuelles Arbeitsverzeichnis
|
||||
BufferedWriter schreibeDatei = new BufferedWriter(new FileWriter(verzeichnis + File.separator + "12_Dateien_Fuer_Aufgabe1" + File.separator + "trennzeichen.txt"));
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < anzahl; i++) {
|
||||
StringBuilder fertigerEintrag = new StringBuilder();
|
||||
// Kundennummer
|
||||
fertigerEintrag.append(String.format("%06d", i));
|
||||
fertigerEintrag.append("#");
|
||||
// Nachname
|
||||
fertigerEintrag.append(nachnamen.get(random.nextInt(nachnamen.size())));
|
||||
fertigerEintrag.append("#");
|
||||
// Vorname
|
||||
fertigerEintrag.append(vornamen.get(random.nextInt(vornamen.size())));
|
||||
fertigerEintrag.append("#");
|
||||
// Straße
|
||||
fertigerEintrag.append(strassen.get(random.nextInt(strassen.size())));
|
||||
fertigerEintrag.append("#");
|
||||
// Hausnummer
|
||||
fertigerEintrag.append(String.format("%05d", random.nextInt(99999)));
|
||||
fertigerEintrag.append("#");
|
||||
// PLZ Ort
|
||||
fertigerEintrag.append(plzort.get(random.nextInt(plzort.size())));
|
||||
fertigerEintrag.append("#");
|
||||
// Umsatz
|
||||
fertigerEintrag.append(String.format("%04d", random.nextInt(500) + 1500));
|
||||
fertigerEintrag.append("#");
|
||||
|
||||
schreibeDatei.write(fertigerEintrag.toString());
|
||||
schreibeDatei.newLine();
|
||||
}
|
||||
schreibeDatei.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void mitLeerzeichen(int anzahl) {
|
||||
try {
|
||||
String verzeichnis = System.getProperty("user.dir"); // Aktuelles Arbeitsverzeichnis
|
||||
BufferedWriter schreibeDatei = new BufferedWriter(new FileWriter(verzeichnis + File.separator + "12_Dateien_Fuer_Aufgabe1" + File.separator + "festeBreite.txt"));
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < anzahl; i++) {
|
||||
StringBuilder fertigerEintrag = new StringBuilder();
|
||||
// Kundennummer
|
||||
fertigerEintrag.append(String.format("%06d", i));
|
||||
// Nachname
|
||||
fertigerEintrag.append(fuelleMitLeerzeichen(nachnamen, 20));
|
||||
// Vorname
|
||||
fertigerEintrag.append(fuelleMitLeerzeichen(vornamen, 20));
|
||||
// Straße
|
||||
fertigerEintrag.append(fuelleMitLeerzeichen(strassen, 20));
|
||||
// Hausnummer
|
||||
fertigerEintrag.append(String.format("%05d", random.nextInt(99999)));
|
||||
// PLZ Ort
|
||||
fertigerEintrag.append(fuelleMitLeerzeichen(plzort, 25));
|
||||
// Umsatz
|
||||
fertigerEintrag.append(String.format("%04d", random.nextInt(500) + 1500));
|
||||
|
||||
schreibeDatei.write(fertigerEintrag.toString());
|
||||
schreibeDatei.newLine();
|
||||
}
|
||||
schreibeDatei.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user