Praktikum 2: neue Test, erster Versuch an moreThan
This commit is contained in:
parent
7d0008e5e6
commit
a313547060
|
@ -1,8 +1,10 @@
|
|||
package de.joethei.hs.java2.praktikum.praktikum1;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class GrosseZahl {
|
||||
|
||||
private int Zahl[];
|
||||
private int zahl[];
|
||||
|
||||
public GrosseZahl(String d) {
|
||||
Boolean korrekteEingabe=true;
|
||||
|
@ -12,9 +14,9 @@ public class GrosseZahl {
|
|||
}
|
||||
}
|
||||
if(korrekteEingabe) {
|
||||
Zahl = new int[d.length()];
|
||||
zahl = new int[d.length()];
|
||||
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 {
|
||||
System.out.println("Incorrect Input!");
|
||||
|
@ -25,22 +27,12 @@ public class GrosseZahl {
|
|||
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) {
|
||||
if(this.Zahl.length < b.Zahl.length) return true;
|
||||
if(this.Zahl.length > b.Zahl.length) return false;
|
||||
//if(this.equals(b)) return false;
|
||||
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 true;
|
||||
if(this.zahl.length < b.zahl.length) return true;
|
||||
if(this.zahl.length > b.zahl.length) return false;
|
||||
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 true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -55,14 +47,14 @@ public class GrosseZahl {
|
|||
rueckgabe = new GrosseZahl(this.toString());
|
||||
operand = new GrosseZahl(b.toString());
|
||||
}
|
||||
for(int i=(rueckgabe.Zahl.length-1), w=(operand.Zahl.length-1); i>=0; i--, w--) {
|
||||
rueckgabe.Zahl[i] += uebertrag;
|
||||
for(int i = (rueckgabe.zahl.length-1), w = (operand.zahl.length-1); i>=0; i--, w--) {
|
||||
rueckgabe.zahl[i] += uebertrag;
|
||||
uebertrag = 0;
|
||||
if(w>=0) {
|
||||
rueckgabe.Zahl[i] += operand.Zahl[w];
|
||||
rueckgabe.zahl[i] += operand.zahl[w];
|
||||
}
|
||||
if(rueckgabe.Zahl[i] > 9) {
|
||||
rueckgabe.Zahl[i] -= 10;
|
||||
if(rueckgabe.zahl[i] > 9) {
|
||||
rueckgabe.zahl[i] -= 10;
|
||||
uebertrag = 1;
|
||||
}
|
||||
if(i==0 && uebertrag==1) {
|
||||
|
@ -83,21 +75,21 @@ public class GrosseZahl {
|
|||
}
|
||||
GrosseZahl rueckgabe = new GrosseZahl(this.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) {
|
||||
rueckgabe.Zahl[i] -= operand.Zahl[w];
|
||||
rueckgabe.zahl[i] -= operand.zahl[w];
|
||||
}
|
||||
rueckgabe.Zahl[i] -= uebertrag;
|
||||
rueckgabe.zahl[i] -= uebertrag;
|
||||
uebertrag = 0;
|
||||
if(rueckgabe.Zahl[i] < 0) {
|
||||
rueckgabe.Zahl[i] += 10;
|
||||
if(rueckgabe.zahl[i] < 0) {
|
||||
rueckgabe.zahl[i] += 10;
|
||||
uebertrag += 1;
|
||||
}
|
||||
}
|
||||
while(rueckgabe.Zahl[0] == 0) {
|
||||
while(rueckgabe.zahl[0] == 0) {
|
||||
x = new StringBuilder();
|
||||
for(int i=1; i<rueckgabe.Zahl.length; i++) {
|
||||
x.append(rueckgabe.Zahl[i]);
|
||||
for(int i = 1; i<rueckgabe.zahl.length; i++) {
|
||||
x.append(rueckgabe.zahl[i]);
|
||||
}
|
||||
rueckgabe = new GrosseZahl(x.toString());
|
||||
}
|
||||
|
@ -114,28 +106,27 @@ public class GrosseZahl {
|
|||
operand2 = this;
|
||||
operand = b;
|
||||
}
|
||||
for(int i=0; i<operand.Zahl.length; i++) {
|
||||
for(int i = 0; i<operand.zahl.length; i++) {
|
||||
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_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");
|
||||
}
|
||||
}
|
||||
return rueckgabe_b;
|
||||
}
|
||||
|
||||
/*
|
||||
Boolean equals(GrosseZahl b) {
|
||||
if(this.Zahl.length < b.Zahl.length || this.Zahl.length > b.Zahl.length) return false;
|
||||
for(int i=0; i<this.Zahl.length; i++) {
|
||||
if(this.Zahl[i] != b.Zahl[i]) return false;
|
||||
|
||||
private int length() {
|
||||
return zahl.length;
|
||||
}
|
||||
return true;
|
||||
|
||||
private int numberAt(int i) {
|
||||
return zahl[i];
|
||||
}
|
||||
*/
|
||||
|
||||
public GrosseZahl ggT(GrosseZahl b) {
|
||||
GrosseZahl operand1 = new GrosseZahl(this.toString());
|
||||
|
@ -145,13 +136,22 @@ public class GrosseZahl {
|
|||
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
|
||||
public boolean equals(Object o) {
|
||||
if(o instanceof GrosseZahl) {
|
||||
GrosseZahl tmp = (GrosseZahl) o;
|
||||
if(tmp.Zahl.length == this.Zahl.length) {
|
||||
for(int i = this.Zahl.length - 1; i >= 0; i--) {
|
||||
if(tmp.Zahl[i] != this.Zahl[i]) return false;
|
||||
if(tmp.zahl.length == this.zahl.length) {
|
||||
for(int i = this.zahl.length - 1; i >= 0; i--) {
|
||||
if(tmp.zahl[i] != this.zahl[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}else return false;
|
||||
|
|
|
@ -1,25 +1,30 @@
|
|||
package de.joethei.hs.java2.praktikum.praktikum2;
|
||||
|
||||
import com.sun.istack.internal.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CharCollection {
|
||||
private Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
public class CharCollection implements Cloneable{
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public CharCollection(String s) {
|
||||
public CharCollection(@NotNull String s) {
|
||||
for(char c : s.toCharArray()) {
|
||||
this.list.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
private CharCollection(List<Character> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.list.size();
|
||||
|
@ -70,6 +75,14 @@ public class CharCollection {
|
|||
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
|
||||
public String toString() {
|
||||
return list.toString().replace('[', '(').replace(']', ')');
|
||||
|
@ -79,9 +92,8 @@ public class CharCollection {
|
|||
public boolean equals(Object o) {
|
||||
if(o instanceof CharCollection) {
|
||||
CharCollection c = (CharCollection) o;
|
||||
return this.list.equals(c.list);
|
||||
return this.list.containsAll(c.list);
|
||||
}return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -20,6 +20,7 @@ public class CharCollectionTest {
|
|||
@Test
|
||||
public void count() {
|
||||
assertEquals(2, new CharCollection("Hallo").count('l'));
|
||||
assertEquals(1, new CharCollection("Hallo").count('a'));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -38,12 +39,18 @@ public class CharCollectionTest {
|
|||
assertEquals("(H, a, l, l, o, W, e, l, t)", new CharCollection("HalloWelt").toString());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@Test
|
||||
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
|
||||
public void except() {
|
||||
assertEquals(new CharCollection("Hao"), new CharCollection("Hallo").except("l"));
|
||||
|
|
Loading…
Reference in New Issue