Commit 6502560a by Paktalin

Lesson 7 Exercise 1

parent 7f46051f
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/AgileJava_Exercises.iml" filepath="$PROJECT_DIR$/AgileJava_Exercises.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package com.example.paktalin.agilejava_exercises;
class Factorial {
static int getWhileFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
int increment = 1;
factorial = increment;
while (increment <= number) {
factorial *= increment;
increment++;
}
}
return factorial;
}
static int getDoWhileFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
int i = 1;
factorial = 1;
do {
factorial *= i;
i++;
} while (i <= number);
}
return factorial;
}
static int getForFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
}
return factorial;
}
static int getBreakFactorial(int number) {
int factorial;
if (number == 0 || number == 1)
factorial = 1;
else {
int i = 1;
factorial = 1;
while (true) {
if (i > number)
break;
factorial *= i;
i++;
}
}
return factorial;
}
}
...@@ -27,6 +27,8 @@ public class AllTests extends TestSuite { ...@@ -27,6 +27,8 @@ public class AllTests extends TestSuite {
suite.addTestSuite(RookTest.class); suite.addTestSuite(RookTest.class);
suite.addTestSuite(KnightTest.class); suite.addTestSuite(KnightTest.class);
suite.addTestSuite(FactorialTest.class);
return suite; return suite;
} }
} }
package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase;
public class FactorialTest extends TestCase {
public void testWhile() {
int factorial = Factorial.getWhileFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getWhileFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getWhileFactorial(3);
assertEquals(getFactorial(3), factorial);
}
public void testDoWhile() {
int factorial = Factorial.getDoWhileFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getDoWhileFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getDoWhileFactorial(3);
assertEquals(getFactorial(3), factorial);
}
public void testFor() {
int factorial = Factorial.getForFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getForFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getForFactorial(3);
assertEquals(getFactorial(3), factorial);
}
public void testBreak() {
int factorial = Factorial.getBreakFactorial(5);
assertEquals(getFactorial(5), factorial);
factorial = Factorial.getBreakFactorial(4);
assertEquals(getFactorial(4), factorial);
factorial = Factorial.getBreakFactorial(3);
assertEquals(getFactorial(3), factorial);
}
private int getFactorial(int number) {
int[] factorials = {1, 1, 2, 6, 24, 120};
return factorials[number];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment