Cleanup VL 3

This commit is contained in:
Matti 2024-05-10 20:30:02 +02:00
parent a812a10736
commit 438fdeef0c
6 changed files with 24 additions and 16 deletions

View File

@ -1,17 +1,16 @@
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code. // then press Enter. You can now see whitespace characters in your code.
import part3.aufg3.Anwendung;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// Press Alt+Eingabe with your caret at the highlighted text to see how int size;
// IntelliJ IDEA suggests fixing it. int MAX_LENGTH = 3;
System.out.println("Hello and welcome!"); int MIN_LENGTH = 10;
// Press Umschalt+F10 or click the green arrow button in the gutter to run the code. for (int i = 0; i <25; i++) {
for (int i = 1; i <= 5; i++) { size = (int) (Math.random() * MAX_LENGTH);
System.out.println(size);
// Press Umschalt+F9 to start debugging your code. We have set one breakpoint
// for you, but you can always add more by pressing Strg+F8.
System.out.println("i = " + i);
} }
} }
} }

View File

@ -45,10 +45,11 @@ public class Anwendung {
myStack.print(); myStack.print();
myStack.p_push(1); myStack.p_push(1);
myStack.p_push('B'); myStack.p_push('J');
myStack.p_push(true); myStack.p_push(true);
myStack.p_pop(); myStack.p_pop();
myStack.p_push("\"Ich Liebe Generische Typen\""); myStack.p_push("\"Ich Liebe Generische Typen\"");
} }

View File

@ -34,10 +34,11 @@ public class GenericStack<T> {
} }
public Object[] toArray(){ public Object[] toArray(){
Object[] retArray = new Object[this.firstFreeIndex];
if (this.firstFreeIndex<1){ if (this.firstFreeIndex<1){
return new Object[0]; return new Object[0];
} }
Object[] retArray = new Object[this.firstFreeIndex];
for (int i=0; i<firstFreeIndex; i++){ for (int i=0; i<firstFreeIndex; i++){
retArray[i] = this.myArray[i]; retArray[i] = this.myArray[i];
} }
@ -47,5 +48,4 @@ public class GenericStack<T> {
public void print(){ public void print(){
System.out.println(Arrays.toString(this.toArray())); System.out.println(Arrays.toString(this.toArray()));
} }
} }

View File

@ -34,10 +34,11 @@ public class IntStack {
} }
public int[] toArray(){ public int[] toArray(){
int[] retArray = new int[firstFreeIndex];
if (firstFreeIndex<1){ if (firstFreeIndex<1){
return new int[0]; return new int[0];
} }
int[] retArray = new int[firstFreeIndex];
for (int i=0; i<firstFreeIndex; i++){ for (int i=0; i<firstFreeIndex; i++){
retArray[i] = this.myArray[i]; retArray[i] = this.myArray[i];
} }

View File

@ -11,6 +11,7 @@ public class Schlenglein <T>{
this.maxSize = 100; this.maxSize = 100;
myArray = new Object[this.maxSize]; myArray = new Object[this.maxSize];
} }
public void add(T t){ public void add(T t){
this.myArray[this.numberOfElements] = t; this.myArray[this.numberOfElements] = t;
this.numberOfElements++; this.numberOfElements++;
@ -19,18 +20,24 @@ public class Schlenglein <T>{
public T get(){ public T get(){
T retValue = (T) this.myArray[0]; T retValue = (T) this.myArray[0];
for (int i=0; i<this.numberOfElements; i++){ for (int i=0; i<this.numberOfElements; i++){
this.myArray[i] = this.myArray[i+1]; this.myArray[i] = this.myArray[i+1];
} }
this.numberOfElements--; this.numberOfElements--;
attemptDynamicResize(); attemptDynamicResize();
return retValue; return retValue;
} }
public int size(){ public int size(){
return this.numberOfElements; return this.numberOfElements;
} }
public int getArrayLenght(){return this.maxSize;} public int getArrayLenght(){
return this.maxSize;
}
public void attemptDynamicResize(){ public void attemptDynamicResize(){
if (numberOfElements < 0.2*maxSize){ if (numberOfElements < 0.2*maxSize){
// Kleiner Machen // Kleiner Machen

View File

@ -30,7 +30,7 @@ public class Anwendung {
int size; int size;
do { do {
size = (int) (Math.random() * MAX_LENGTH); size = (int) (Math.random() * (MAX_LENGTH+1));
} while (size < MIN_LENGTH); } while (size < MIN_LENGTH);
String output = ""; String output = "";