forked from IF-LK-2020/oop2
Initial commit
This commit is contained in:
parent
c523acbb1e
commit
9bd413d1a8
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* Ein einfacher Rechner.
|
||||
*/
|
||||
public class Calculator {
|
||||
|
||||
/**
|
||||
* Bildet die Summe aller gerader Zahlen die kleiner oder gleich
|
||||
* <var>pUpperBound</var> sind. Die Eingabe <code>addEvenNumbers(4)</code>
|
||||
* ergibt also <code>2 + 4 = 6</code>.
|
||||
*
|
||||
* TODO: Benutze den Debugger, um die beiden Fehler in der Methode zu finden.
|
||||
*
|
||||
* @param pUpperBound Obere Grenze der Summe
|
||||
* @return
|
||||
*/
|
||||
public int addEvenNumbers( int pUpperBound ) {
|
||||
int sum = 0, i = 1;
|
||||
while( i < pUpperBound ) {
|
||||
// Nur gerade Zahlen addieren
|
||||
if( i%2 == 0 ) {
|
||||
sum += i;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wendet den euklidischen Algorithmus zur Suche des größten gemeinsamen
|
||||
* Teilers bei zwei positiven ganzen Zahlen an.
|
||||
*
|
||||
* TODO: Benutze den Debugger, um den Fehler in der Methode zu finden.
|
||||
*
|
||||
* @link https://de.wikipedia.org/wiki/Euklidischer_Algorithmus
|
||||
* @param pNumberA
|
||||
* @param pNumberB
|
||||
* @return
|
||||
*/
|
||||
public int ggT( int pNumberA, int pNumberB ) {
|
||||
if( pNumberA < 0 || pNumberB < 0 ) {
|
||||
return 0;
|
||||
} else if( pNumberA == 0 ) {
|
||||
return pNumberB;
|
||||
} else {
|
||||
while( pNumberB > 0 ) {
|
||||
if( pNumberA < pNumberB ) {
|
||||
pNumberA -= pNumberB;
|
||||
} else {
|
||||
pNumberB -= pNumberA;
|
||||
}
|
||||
}
|
||||
return pNumberA;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Berechnet das kleinste gemeinsame Vielfache zweier positiver Ganzzahlen.
|
||||
* @link https://de.wikipedia.org/wiki/Kleinstes_gemeinsames_Vielfaches#Berechnung_über_den_größten_gemeinsamen_Teiler_(ggT)
|
||||
* @param pNumberA
|
||||
* @param pNumberB
|
||||
* @return
|
||||
*/
|
||||
public int kgV( int pNumberA, int pNumberB ) {
|
||||
return 0;
|
||||
|
||||
// TODO:
|
||||
// Implementiere die Methode wie unter dem Link oben beschreiben,
|
||||
// indem du das kgV mit Hilfe des ggT berechnest.
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test für die Klasse Calculator.
|
||||
*/
|
||||
public class CalculatorTest {
|
||||
|
||||
@Test
|
||||
public void testAddEvenNumbers() {
|
||||
Calculator calc = new Calculator();
|
||||
|
||||
int[][] tests = new int[][]{
|
||||
new int[]{0, 0},
|
||||
new int[]{1, 0},
|
||||
new int[]{4, 6},
|
||||
new int[]{5, 6},
|
||||
new int[]{24, 156}
|
||||
};
|
||||
for( int i = 0; i < tests.length; i++ ) {
|
||||
int sum = calc.addEvenNumbers(tests[i][0]);
|
||||
assertEquals("addEvenNumbers(" + tests[i][0] + ")", tests[i][1], sum);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGgT() {
|
||||
Calculator calc = new Calculator();
|
||||
|
||||
int[][] tests = new int[][]{
|
||||
new int[]{0, 0, 0},
|
||||
new int[]{2, 4, 2},
|
||||
new int[]{4, 4, 4},
|
||||
new int[]{12, 44, 4},
|
||||
new int[]{44, 12, 4},
|
||||
new int[]{24, 18, 6},
|
||||
new int[]{18, 24, 6},
|
||||
new int[]{1, 18, 1},
|
||||
new int[]{24, 7, 1}
|
||||
};
|
||||
for( int i = 0; i < tests.length; i++ ) {
|
||||
int ggt = calc.ggT(tests[i][0], tests[i][1]);
|
||||
assertEquals("ggT(" + tests[i][0] + "," + tests[i][1] + ")", tests[i][2], ggt);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKgV() {
|
||||
Calculator calc = new Calculator();
|
||||
|
||||
int[][] tests = new int[][]{
|
||||
new int[]{4, 4, 4},
|
||||
new int[]{44, 12, 132},
|
||||
new int[]{12, 44, 132},
|
||||
new int[]{4, 12, 12},
|
||||
new int[]{24, 18, 72},
|
||||
new int[]{24, 7, 168}
|
||||
|
||||
};
|
||||
for( int i = 0; i < tests.length; i++ ) {
|
||||
int kgv = calc.kgV(tests[i][0], tests[i][1]);
|
||||
assertEquals("kgV(" + tests[i][0] + "," + tests[i][1] + ")", tests[i][2], kgv);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
Simuliert ein rudimentäres Zahlen Tippspiel.
|
||||
|
||||
Jeder Spieler wählt sechs Zahlen zwischen 1 und 10 und versucht
|
||||
so viele Zahlen wie möglich genau zu treffen.
|
||||
|
||||
Zum starten folgende Schritte ausführen:
|
||||
1. Ein Objekt der Klasse "TippSpiel" erzeugen und die Anzahl der Spieler angeben.
|
||||
2. Die Methode "erzeugeSpieler()" ausführen um die Spieler-Objekte zu erstellen.
|
||||
3. Die Methode "neuesSpiel()" ausführen um neue Tipps abzugeben.
|
||||
4. Die Methode "zahlenZiehen()" ausführen um Lottozahlen zu ziehen.
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
<excludePattern pattern="*.class" />
|
||||
<excludePattern pattern="*.ctxt" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="library" name="junit-4.5" level="project" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,25 @@
|
|||
#BlueJ package file
|
||||
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=1146
|
||||
package.divider.horizontal=0.6
|
||||
package.divider.vertical=0.8984700973574409
|
||||
package.editor.height=639
|
||||
package.editor.width=1032
|
||||
package.editor.x=100
|
||||
package.editor.y=23
|
||||
package.frame.height=777
|
||||
package.frame.width=1170
|
||||
package.numDependencies=0
|
||||
package.numTargets=0
|
||||
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
|
Loading…
Reference in New Issue