forked from IF-LK-2020/arrays2
Initial commit
This commit is contained in:
26
.gitignore
vendored
Normal file
26
.gitignore
vendored
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# ---> Java
|
||||||
|
# Compiled class file
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Log file
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# BlueJ files
|
||||||
|
*.ctxt
|
||||||
|
Dokumente*
|
||||||
|
|
||||||
|
# Mobile Tools for Java (J2ME)
|
||||||
|
.mtj.tmp/
|
||||||
|
|
||||||
|
# Package Files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.nar
|
||||||
|
*.ear
|
||||||
|
*.zip
|
||||||
|
*.tar.gz
|
||||||
|
*.rar
|
||||||
|
|
||||||
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
|
hs_err_pid*
|
||||||
|
|
||||||
175
Arrays.java
Normal file
175
Arrays.java
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Arrays {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ein Beispiel zu Objekt-Arrays.
|
||||||
|
*/
|
||||||
|
public void objectArrayExample() {
|
||||||
|
// Array mit Objektreferenzen erstellen
|
||||||
|
Wuerfel[] wuerfel = new Wuerfel[6];
|
||||||
|
wuerfel[4] = new Wuerfel("W4");
|
||||||
|
wuerfel[0] = new Wuerfel("W0");
|
||||||
|
wuerfel[1] = new Wuerfel("W1");
|
||||||
|
wuerfel[2] = wuerfel[4];
|
||||||
|
wuerfel[3] = wuerfel[1];
|
||||||
|
|
||||||
|
// Ausgabe der Elemente im Array
|
||||||
|
printArray(wuerfel);
|
||||||
|
|
||||||
|
// wuerfel[2] und wuerfel[4] zeigen auf dasselbe Objekt
|
||||||
|
wuerfel[2].werfen();
|
||||||
|
System.out.printf("Index %s: %s\n", 4, wuerfel[4].toString());
|
||||||
|
if( wuerfel[2] == wuerfel[4] ) {
|
||||||
|
System.out.println("Index 2 und Index 4 sind gleich");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
// Die Referenz in wuerfel[2] kann auf ein neues Objekt gesetzt werden
|
||||||
|
wuerfel[2] = new Wuerfel("W2");
|
||||||
|
|
||||||
|
// Eine Objektreferenz im Array kann auch gelöscht werden
|
||||||
|
// Das zugehörige Objekt bleibt erhalten, solange noch eine andere
|
||||||
|
// Referenz vorhanden ist (hier wuerfel[1]).
|
||||||
|
wuerfel[3] = null;
|
||||||
|
|
||||||
|
// Referenzen können beliebig neu gesetzt werden
|
||||||
|
Wuerfel w5 = new Wuerfel("W5");
|
||||||
|
wuerfel[5] = w5;
|
||||||
|
wuerfel[0] = w5;
|
||||||
|
|
||||||
|
// Ausgabe der Elemente im Array
|
||||||
|
printArray(wuerfel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wirft alle Würfel im Array neu.
|
||||||
|
* <p>
|
||||||
|
* Indizes ohne Referenz auf einen Würfel (also mit Inhalt <code>null</code>)
|
||||||
|
* werden übersprungen.
|
||||||
|
*
|
||||||
|
* @see Wuerfel#werfen()
|
||||||
|
*/
|
||||||
|
public void rollAll( Wuerfel[] pArray ) {
|
||||||
|
// TODO: implementieren
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnet die Summe aller Augenzahlen, die die Würfel
|
||||||
|
* im Array gerade anzeigen.
|
||||||
|
* <p>
|
||||||
|
* Indizes ohne Referenz auf einen Würfel (also mit Inhalt <code>null</code>)
|
||||||
|
* werden übersprungen.
|
||||||
|
*
|
||||||
|
* @see Wuerfel#getAugenzahl()
|
||||||
|
*/
|
||||||
|
public int diceSum( Wuerfel[] pArray ) {
|
||||||
|
// TODO: implementieren
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wirft alle Würfel im zweidimensionalen Array neu.
|
||||||
|
* <p>
|
||||||
|
* Indizes ohne Referenz auf einen Würfel (also mit Inhalt <code>null</code>)
|
||||||
|
* werden übersprungen.
|
||||||
|
*
|
||||||
|
* @see Wuerfel#werfen()
|
||||||
|
*/
|
||||||
|
public void twoDimRoll( Wuerfel[][] pArray ) {
|
||||||
|
// TODO: implementieren
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnet die Summe aller Augenzahlen, die die Würfel
|
||||||
|
* im zweidimensionalen Array gerade anzeigen.
|
||||||
|
* <p>
|
||||||
|
* Indizes ohne Referenz auf einen Würfel (also mit Inhalt <code>null</code>)
|
||||||
|
* werden übersprungen.
|
||||||
|
*
|
||||||
|
* @see Wuerfel#getAugenzahl()
|
||||||
|
*/
|
||||||
|
public int twoDimSum( Wuerfel[][] pArray ) {
|
||||||
|
// TODO: implementieren
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Berechnet in einem zweidimensionalen Array die Zeilensummen.
|
||||||
|
* Das bedeutet für jeden Index <code>i</code> in pArray wird die Summe der Elemente
|
||||||
|
* im Array <code>pArray[i]</code> berechnet. Die Summe wird im Ergebnisarray
|
||||||
|
* wieder im Index <code>i</code> gespeichert.
|
||||||
|
*
|
||||||
|
* @param pArray
|
||||||
|
* @return Ein eindimensionales Array mit den Zeilensummen
|
||||||
|
*/
|
||||||
|
public int[] rowSum( Wuerfel[][] pArray ) {
|
||||||
|
// TODO: implementieren
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt die Augenzahlen der Würfel im Array auf
|
||||||
|
* der Kommandozeile aus.
|
||||||
|
* @param pArray Ein Array mit Wuerfel-Referenzen
|
||||||
|
*/
|
||||||
|
public void printArray( Wuerfel[] pArray ) {
|
||||||
|
for( int i = 0; i < pArray.length; i++ ) {
|
||||||
|
if( pArray[i] != null ) { // prüfen, ob es ein Objekt gibt
|
||||||
|
System.out.printf("Index %s: %s\n", i, pArray[i].toString());
|
||||||
|
} else {
|
||||||
|
System.out.printf("Index %s: null\n", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt die Augenzahlen der Würfel im zweidimensionalen Array auf
|
||||||
|
* der Kommandozeile aus.
|
||||||
|
* @param pArray Ein zweidimensionales Array mit Wuerfel-Referenzen
|
||||||
|
*/
|
||||||
|
public void printArray( Wuerfel[][] pArray ) {
|
||||||
|
for( int i = 0; i < pArray.length; i++ ) {
|
||||||
|
if( pArray[i] != null ) { // prüfen, ob es ein Objekt gibt
|
||||||
|
Wuerfel[] innerArray = pArray[i];
|
||||||
|
for( int j = 0; j < innerArray.length; j++ ) {
|
||||||
|
if( innerArray[j] != null ) {
|
||||||
|
System.out.printf("Index %s,%s: %s\n", i, j, pArray[i].toString());
|
||||||
|
} else {
|
||||||
|
System.out.printf("Index %s,%s: null\n", i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.printf("Index %s: null\n", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt ein Array mit <var>pNumberOfElements</var>
|
||||||
|
* {@link Wuerfel}n.
|
||||||
|
*/
|
||||||
|
public Wuerfel[] generateArray( int pNumberOfElements ) {
|
||||||
|
Wuerfel[] dice = new Wuerfel[pNumberOfElements];
|
||||||
|
for( int i = 0; i < dice.length; i++ ) {
|
||||||
|
dice[i] = new Wuerfel("W" + i);
|
||||||
|
}
|
||||||
|
return dice;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt ein Array das wiederum <var>pNumberOfRows</var> Arrays enthält, die
|
||||||
|
* jeweils <var>pNumberOfCols</var> {@link Wuerfel} enthalten.
|
||||||
|
*/
|
||||||
|
public Wuerfel[][] generateArray( int pNumberOfRows, int pNumberOfCols ) {
|
||||||
|
Wuerfel[][] dice = new Wuerfel[pNumberOfRows][pNumberOfCols];
|
||||||
|
for( int i = 0; i < dice.length; i++ ) {
|
||||||
|
dice[i] = generateArray(pNumberOfCols);
|
||||||
|
}
|
||||||
|
return dice;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
187
ArraysTest.java
Normal file
187
ArraysTest.java
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class ArraysTest {
|
||||||
|
|
||||||
|
private static int n = 0;
|
||||||
|
|
||||||
|
private class MockupWuerfel extends Wuerfel {
|
||||||
|
|
||||||
|
public int mockupValue;
|
||||||
|
|
||||||
|
public int rolls = 0;
|
||||||
|
|
||||||
|
public MockupWuerfel(int pValue) {
|
||||||
|
super("W" + (n++));
|
||||||
|
mockupValue = pValue;
|
||||||
|
rolls = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAugenzahl() {
|
||||||
|
return mockupValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void werfen() {
|
||||||
|
rolls = rolls + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Arrays arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt das Testgerüst fuer den Test.
|
||||||
|
*
|
||||||
|
* Wird vor jeder Testfall-Methode aufgerufen.
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
arrays = new Arrays();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDiceSum() {
|
||||||
|
Random rand = new Random();
|
||||||
|
|
||||||
|
Wuerfel[] dice;
|
||||||
|
|
||||||
|
dice = new Wuerfel[]{
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(6),
|
||||||
|
new MockupWuerfel(4),
|
||||||
|
};
|
||||||
|
assertEquals("Falsche Summe berechnet.", 11, arrays.diceSum(dice));
|
||||||
|
|
||||||
|
for( int i = 0; i < 10; i++ ) {
|
||||||
|
int sum = 0;
|
||||||
|
dice = new Wuerfel[rand.nextInt(18)+2];
|
||||||
|
for( int j = 0; j < dice.length; j++ ) {
|
||||||
|
int w = rand.nextInt(6)+1;
|
||||||
|
dice[j] = new MockupWuerfel(w);
|
||||||
|
sum += w;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("Falsche Summe berechnet.", sum, arrays.diceSum(dice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRollAll() {
|
||||||
|
Wuerfel[] dice = new Wuerfel[50];
|
||||||
|
for( int i = 0; i < dice.length; i++ ) {
|
||||||
|
dice[i] = new MockupWuerfel(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
arrays.rollAll(dice);
|
||||||
|
|
||||||
|
for( int i = 0; i < dice.length; i++ ) {
|
||||||
|
MockupWuerfel die = (MockupWuerfel)dice[i];
|
||||||
|
assertTrue( "Der Würfel "+die.getName()+" wurde nicht geworfen.", die.rolls == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTwoDimSum() {
|
||||||
|
Random rand = new Random();
|
||||||
|
|
||||||
|
Wuerfel[][] dice;
|
||||||
|
|
||||||
|
dice = new Wuerfel[][]{
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(6),
|
||||||
|
new MockupWuerfel(4)
|
||||||
|
},
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(5),
|
||||||
|
new MockupWuerfel(5),
|
||||||
|
new MockupWuerfel(5)
|
||||||
|
},
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(0)
|
||||||
|
},
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(-3),
|
||||||
|
new MockupWuerfel(-2),
|
||||||
|
new MockupWuerfel(-4)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertEquals("Falsche Summe berechnet.", 19, arrays.twoDimSum(dice));
|
||||||
|
|
||||||
|
for( int i = 0; i < 10; i++ ) {
|
||||||
|
int sum = 0;
|
||||||
|
dice = new Wuerfel[rand.nextInt(18)+2][rand.nextInt(18)+2];
|
||||||
|
for( int j = 0; j < dice.length; j++ ) {
|
||||||
|
for( int k = 0; k < dice[j].length; k++ ) {
|
||||||
|
int w = rand.nextInt(6)+1;
|
||||||
|
dice[j][k] = new MockupWuerfel(w);
|
||||||
|
sum += w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("Falsche Summe berechnet.", sum, arrays.twoDimSum(dice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTwoDimRoll() {
|
||||||
|
Wuerfel[][] dice = new Wuerfel[50][45];
|
||||||
|
for( int i = 0; i < dice.length; i++ ) {
|
||||||
|
for( int j = 0; j < dice[i].length; j++ ) {
|
||||||
|
dice[i][j] = new MockupWuerfel(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
arrays.twoDimRoll(dice);
|
||||||
|
|
||||||
|
for( int i = 0; i < dice.length; i++ ) {
|
||||||
|
for( int j = 0; j < dice[i].length; j++ ) {
|
||||||
|
MockupWuerfel die = (MockupWuerfel)dice[i][j];
|
||||||
|
assertTrue( "Der Würfel "+die.getName()+" wurde nicht geworfen.", die.rolls == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRowSum() {
|
||||||
|
Wuerfel[][] dice;
|
||||||
|
|
||||||
|
dice = new Wuerfel[][]{
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(6),
|
||||||
|
new MockupWuerfel(4)
|
||||||
|
},
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(5),
|
||||||
|
new MockupWuerfel(5),
|
||||||
|
new MockupWuerfel(5)
|
||||||
|
},
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(1),
|
||||||
|
new MockupWuerfel(0)
|
||||||
|
},
|
||||||
|
new Wuerfel[]{
|
||||||
|
new MockupWuerfel(-3),
|
||||||
|
new MockupWuerfel(-2),
|
||||||
|
new MockupWuerfel(-4)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int[] sums = arrays.rowSum(dice);
|
||||||
|
assertEquals("Ergebnisarray hat die falsche Größe", 4, sums.length);
|
||||||
|
assertEquals("Erste Zeilensumme nicht korrekt", 11, sums[0]);
|
||||||
|
assertEquals("Zweite Zeilensumme nicht korrekt", 15, sums[1]);
|
||||||
|
assertEquals("Dritte Zeilensumme nicht korrekt", 2, sums[2]);
|
||||||
|
assertEquals("Vierte Zeilensumme nicht korrekt", -9, sums[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
44
Wuerfel.java
Normal file
44
Wuerfel.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ein normaler, sechsseitiger Würfel.
|
||||||
|
*/
|
||||||
|
public class Wuerfel {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private Random rand;
|
||||||
|
|
||||||
|
private int value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt einen Würfel mit einem Namen zur Identifikation.
|
||||||
|
* @param pName
|
||||||
|
*/
|
||||||
|
public Wuerfel( String pName ) {
|
||||||
|
name = pName;
|
||||||
|
rand = new Random();
|
||||||
|
werfen();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wirft den Würfel. Die geworfene Augenzahl
|
||||||
|
* kann dann mit {@link #getAugenzahl()} abgerufen
|
||||||
|
* werden.
|
||||||
|
*/
|
||||||
|
public void werfen() {
|
||||||
|
value = rand.nextInt(6)+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAugenzahl() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return name + "<" + value + ">";
|
||||||
|
}
|
||||||
|
}
|
||||||
55
package.bluej
Executable file
55
package.bluej
Executable file
@@ -0,0 +1,55 @@
|
|||||||
|
#BlueJ package file
|
||||||
|
dependency1.from=ArraysTest
|
||||||
|
dependency1.to=Wuerfel
|
||||||
|
dependency1.type=UsesDependency
|
||||||
|
dependency2.from=ArraysTest
|
||||||
|
dependency2.to=Arrays
|
||||||
|
dependency2.type=UsesDependency
|
||||||
|
dependency3.from=Arrays
|
||||||
|
dependency3.to=Wuerfel
|
||||||
|
dependency3.type=UsesDependency
|
||||||
|
editor.fx.0.height=0
|
||||||
|
editor.fx.0.width=0
|
||||||
|
editor.fx.0.x=0
|
||||||
|
editor.fx.0.y=0
|
||||||
|
objectbench.height=66
|
||||||
|
objectbench.width=1219
|
||||||
|
package.divider.horizontal=0.6
|
||||||
|
package.divider.vertical=0.8983286908077994
|
||||||
|
package.editor.height=638
|
||||||
|
package.editor.width=1105
|
||||||
|
package.editor.x=37
|
||||||
|
package.editor.y=23
|
||||||
|
package.frame.height=776
|
||||||
|
package.frame.width=1243
|
||||||
|
package.numDependencies=3
|
||||||
|
package.numTargets=3
|
||||||
|
package.showExtends=true
|
||||||
|
package.showUses=true
|
||||||
|
project.charset=UTF-8
|
||||||
|
readme.height=58
|
||||||
|
readme.name=@README
|
||||||
|
readme.width=47
|
||||||
|
readme.x=10
|
||||||
|
readme.y=10
|
||||||
|
target1.height=170
|
||||||
|
target1.name=ArraysTest
|
||||||
|
target1.showInterface=false
|
||||||
|
target1.type=UnitTestTargetJunit4
|
||||||
|
target1.width=140
|
||||||
|
target1.x=580
|
||||||
|
target1.y=70
|
||||||
|
target2.height=50
|
||||||
|
target2.name=Wuerfel
|
||||||
|
target2.showInterface=false
|
||||||
|
target2.type=ClassTarget
|
||||||
|
target2.width=80
|
||||||
|
target2.x=240
|
||||||
|
target2.y=490
|
||||||
|
target3.height=160
|
||||||
|
target3.name=Arrays
|
||||||
|
target3.showInterface=false
|
||||||
|
target3.type=ClassTarget
|
||||||
|
target3.width=180
|
||||||
|
target3.x=120
|
||||||
|
target3.y=110
|
||||||
Reference in New Issue
Block a user