Praktikum 2: neue Test, erster Versuch an moreThan

This commit is contained in:
Johannes Theiner 2018-04-12 14:39:26 +02:00
parent 7d0008e5e6
commit a313547060
3 changed files with 73 additions and 54 deletions

View File

@ -1,8 +1,10 @@
package de.joethei.hs.java2.praktikum.praktikum1; package de.joethei.hs.java2.praktikum.praktikum1;
import java.util.logging.Logger;
public class GrosseZahl { public class GrosseZahl {
private int Zahl[]; private int zahl[];
public GrosseZahl(String d) { public GrosseZahl(String d) {
Boolean korrekteEingabe=true; Boolean korrekteEingabe=true;
@ -12,9 +14,9 @@ public class GrosseZahl {
} }
} }
if(korrekteEingabe) { if(korrekteEingabe) {
Zahl = new int[d.length()]; zahl = new int[d.length()];
for(int w=0; w<d.length(); w++) { for(int w=0; w<d.length(); w++) {
Zahl[w] = Integer.parseInt(String.valueOf(d.charAt(w))); zahl[w] = Integer.parseInt(String.valueOf(d.charAt(w)));
} }
} else { } else {
System.out.println("Incorrect Input!"); System.out.println("Incorrect Input!");
@ -25,22 +27,12 @@ public class GrosseZahl {
this(Integer.toString(i)); this(Integer.toString(i));
} }
@Override
public String toString() {
StringBuilder rueckgabe = new StringBuilder();
for (int aZahl : Zahl) {
rueckgabe.append(Integer.toString(aZahl));
}
return rueckgabe.toString();
}
public boolean less(GrosseZahl b) { public boolean less(GrosseZahl b) {
if(this.Zahl.length < b.Zahl.length) return true; if(this.zahl.length < b.zahl.length) return true;
if(this.Zahl.length > b.Zahl.length) return false; if(this.zahl.length > b.zahl.length) return false;
//if(this.equals(b)) return false; for(int i = 0; i<this.zahl.length; i++) {
for(int i=0; i<this.Zahl.length; i++) { if(this.zahl[i] > b.zahl[i]) return false;
if(this.Zahl[i] > b.Zahl[i]) return false; if(this.zahl[i] < b.zahl[i]) return true;
if(this.Zahl[i] < b.Zahl[i]) return true;
} }
return false; return false;
} }
@ -55,14 +47,14 @@ public class GrosseZahl {
rueckgabe = new GrosseZahl(this.toString()); rueckgabe = new GrosseZahl(this.toString());
operand = new GrosseZahl(b.toString()); operand = new GrosseZahl(b.toString());
} }
for(int i=(rueckgabe.Zahl.length-1), w=(operand.Zahl.length-1); i>=0; i--, w--) { for(int i = (rueckgabe.zahl.length-1), w = (operand.zahl.length-1); i>=0; i--, w--) {
rueckgabe.Zahl[i] += uebertrag; rueckgabe.zahl[i] += uebertrag;
uebertrag = 0; uebertrag = 0;
if(w>=0) { if(w>=0) {
rueckgabe.Zahl[i] += operand.Zahl[w]; rueckgabe.zahl[i] += operand.zahl[w];
} }
if(rueckgabe.Zahl[i] > 9) { if(rueckgabe.zahl[i] > 9) {
rueckgabe.Zahl[i] -= 10; rueckgabe.zahl[i] -= 10;
uebertrag = 1; uebertrag = 1;
} }
if(i==0 && uebertrag==1) { if(i==0 && uebertrag==1) {
@ -83,21 +75,21 @@ public class GrosseZahl {
} }
GrosseZahl rueckgabe = new GrosseZahl(this.toString()); GrosseZahl rueckgabe = new GrosseZahl(this.toString());
GrosseZahl operand = new GrosseZahl(b.toString()); GrosseZahl operand = new GrosseZahl(b.toString());
for(int i=(rueckgabe.Zahl.length-1), w=(operand.Zahl.length-1); i>=0; i--, w--) { for(int i = (rueckgabe.zahl.length-1), w = (operand.zahl.length-1); i>=0; i--, w--) {
if(w>=0) { if(w>=0) {
rueckgabe.Zahl[i] -= operand.Zahl[w]; rueckgabe.zahl[i] -= operand.zahl[w];
} }
rueckgabe.Zahl[i] -= uebertrag; rueckgabe.zahl[i] -= uebertrag;
uebertrag = 0; uebertrag = 0;
if(rueckgabe.Zahl[i] < 0) { if(rueckgabe.zahl[i] < 0) {
rueckgabe.Zahl[i] += 10; rueckgabe.zahl[i] += 10;
uebertrag += 1; uebertrag += 1;
} }
} }
while(rueckgabe.Zahl[0] == 0) { while(rueckgabe.zahl[0] == 0) {
x = new StringBuilder(); x = new StringBuilder();
for(int i=1; i<rueckgabe.Zahl.length; i++) { for(int i = 1; i<rueckgabe.zahl.length; i++) {
x.append(rueckgabe.Zahl[i]); x.append(rueckgabe.zahl[i]);
} }
rueckgabe = new GrosseZahl(x.toString()); rueckgabe = new GrosseZahl(x.toString());
} }
@ -114,28 +106,27 @@ public class GrosseZahl {
operand2 = this; operand2 = this;
operand = b; operand = b;
} }
for(int i=0; i<operand.Zahl.length; i++) { for(int i = 0; i<operand.zahl.length; i++) {
rueckgabe = new GrosseZahl(0); rueckgabe = new GrosseZahl(0);
for(int w=0; w<operand.Zahl[i]; w++) { for(int w = 0; w<operand.zahl[i]; w++) {
rueckgabe = rueckgabe.add(operand2); rueckgabe = rueckgabe.add(operand2);
} }
rueckgabe_b = rueckgabe_b.add(rueckgabe); rueckgabe_b = rueckgabe_b.add(rueckgabe);
if(i != operand.Zahl.length-1) { if(i != operand.zahl.length-1) {
rueckgabe_b = new GrosseZahl(rueckgabe_b.toString()+"0"); rueckgabe_b = new GrosseZahl(rueckgabe_b.toString()+"0");
} }
} }
return rueckgabe_b; return rueckgabe_b;
} }
/*
Boolean equals(GrosseZahl b) { private int length() {
if(this.Zahl.length < b.Zahl.length || this.Zahl.length > b.Zahl.length) return false; return zahl.length;
for(int i=0; i<this.Zahl.length; i++) { }
if(this.Zahl[i] != b.Zahl[i]) return false;
} private int numberAt(int i) {
return true; return zahl[i];
} }
*/
public GrosseZahl ggT(GrosseZahl b) { public GrosseZahl ggT(GrosseZahl b) {
GrosseZahl operand1 = new GrosseZahl(this.toString()); GrosseZahl operand1 = new GrosseZahl(this.toString());
@ -145,13 +136,22 @@ public class GrosseZahl {
else return operand1.sub(operand2).ggT(operand2); else return operand1.sub(operand2).ggT(operand2);
} }
@Override
public String toString() {
StringBuilder rueckgabe = new StringBuilder();
for (int aZahl : zahl) {
rueckgabe.append(Integer.toString(aZahl));
}
return rueckgabe.toString();
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if(o instanceof GrosseZahl) { if(o instanceof GrosseZahl) {
GrosseZahl tmp = (GrosseZahl) o; GrosseZahl tmp = (GrosseZahl) o;
if(tmp.Zahl.length == this.Zahl.length) { if(tmp.zahl.length == this.zahl.length) {
for(int i = this.Zahl.length - 1; i >= 0; i--) { for(int i = this.zahl.length - 1; i >= 0; i--) {
if(tmp.Zahl[i] != this.Zahl[i]) return false; if(tmp.zahl[i] != this.zahl[i]) return false;
} }
return true; return true;
}else return false; }else return false;

View File

@ -1,25 +1,30 @@
package de.joethei.hs.java2.praktikum.praktikum2; package de.joethei.hs.java2.praktikum.praktikum2;
import com.sun.istack.internal.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
public class CharCollection { public class CharCollection implements Cloneable{
private Logger logger = Logger.getLogger(this.getClass().getName());
private ArrayList<Character> list = new ArrayList<>(); private List<Character> list = new ArrayList<>();
public CharCollection(char... cc) { public CharCollection(@NotNull char... cc) {
for(char c:cc) this.list.add(c); for(char c:cc) this.list.add(c);
} }
public CharCollection(String s) { public CharCollection(@NotNull String s) {
for(char c : s.toCharArray()) { for(char c : s.toCharArray()) {
this.list.add(c); this.list.add(c);
} }
} }
private CharCollection(List<Character> list) {
this.list = list;
}
public int size() { public int size() {
return this.list.size(); return this.list.size();
@ -70,6 +75,14 @@ public class CharCollection {
return character; return character;
} }
public CharCollection moreThan(int n) {
CharCollection result = new CharCollection(list);
for (int i = 0; i < result.size(); i++) {
if(result.count(result.list.get(i)) < n) result.list.remove(i);
}
return result;
}
@Override @Override
public String toString() { public String toString() {
return list.toString().replace('[', '(').replace(']', ')'); return list.toString().replace('[', '(').replace(']', ')');
@ -79,9 +92,8 @@ public class CharCollection {
public boolean equals(Object o) { public boolean equals(Object o) {
if(o instanceof CharCollection) { if(o instanceof CharCollection) {
CharCollection c = (CharCollection) o; CharCollection c = (CharCollection) o;
return this.list.equals(c.list); return this.list.containsAll(c.list);
}return false; }return false;
} }
} }

View File

@ -20,6 +20,7 @@ public class CharCollectionTest {
@Test @Test
public void count() { public void count() {
assertEquals(2, new CharCollection("Hallo").count('l')); assertEquals(2, new CharCollection("Hallo").count('l'));
assertEquals(1, new CharCollection("Hallo").count('a'));
} }
@Test @Test
@ -38,12 +39,18 @@ public class CharCollectionTest {
assertEquals("(H, a, l, l, o, W, e, l, t)", new CharCollection("HalloWelt").toString()); assertEquals("(H, a, l, l, o, W, e, l, t)", new CharCollection("HalloWelt").toString());
} }
/*
@Test @Test
public void moreThan() { public void moreThan() {
assertEquals(new CharCollection("ll"), new CharCollection("Hallo").moreThan(2)); assertEquals(new CharCollection("lll"), new CharCollection("Hallol").moreThan(2));
} }
@Test
public void equals() {
assertEquals(new CharCollection("llo"), new CharCollection("lol"));
}
/*
@Test @Test
public void except() { public void except() {
assertEquals(new CharCollection("Hao"), new CharCollection("Hallo").except("l")); assertEquals(new CharCollection("Hao"), new CharCollection("Hallo").except("l"));