Skip to content

Commit 7561161

Browse files
committed
Exercise solutions for chapter 6 plus tests
1 parent cf6b3fe commit 7561161

12 files changed

Lines changed: 237 additions & 0 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class IsDivisible {
2+
public static void main(String[] args) {
3+
System.out.println(isDivisible(10, 5));
4+
}
5+
public static boolean isDivisible(int n, int m) {
6+
if(n % m == 0) {
7+
return true;
8+
}
9+
return false;
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import junit.framework.TestCase;
2+
3+
/**
4+
* A JUnit test case class.
5+
* Every method starting with the word "test" will be called when running
6+
* the test with JUnit.
7+
*/
8+
public class IsDivisibleTest extends TestCase {
9+
10+
/**
11+
* A test method.
12+
* (Replace "X" with a name describing the test. You may write as
13+
* many "testSomething" methods in this class as you wish, and each
14+
* one will be called when running JUnit over this class.)
15+
*/
16+
public void testisDivisible() {
17+
assertTrue(true, Series.isDivisible(10, 5));
18+
}
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class IsTriangle {
2+
public static void main(String[] args) {
3+
boolean result = isTriangle(1, 2, 3);
4+
System.out.println(result);
5+
}
6+
public static boolean isTriangle(int a, int b, int c) {
7+
if(a > b + c) {
8+
return false;
9+
}
10+
else if(b > a + c) {
11+
return false;
12+
}
13+
else if(c > a + b) {
14+
return false;
15+
}
16+
else {
17+
return true;
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import junit.framework.TestCase;
2+
3+
/**
4+
* A JUnit test case class.
5+
* Every method starting with the word "test" will be called when running
6+
* the test with JUnit.
7+
*/
8+
public class IsTriangleTest extends TestCase {
9+
10+
/**
11+
* A test method.
12+
* (Replace "X" with a name describing the test. You may write as
13+
* many "testSomething" methods in this class as you wish, and each
14+
* one will be called when running JUnit over this class.)
15+
*/
16+
public void testisTriangle() {
17+
assertEquals(true, Series.isTriangle(1,2,3));
18+
}
19+
20+
}

ch06/Exercise6.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Exercise6 {
2+
public static void main(String[] args) {
3+
System.out.println(prod(1,4));
4+
}
5+
public static int prod(int m, int n) {
6+
if (m == n) {
7+
return n;
8+
} else {
9+
int recurse = prod(m, n - 1);
10+
int result = n * recurse;
11+
return result;
12+
//return n * (prod(m, n-1);
13+
}
14+
}
15+
}
16+

ch06/Exercise6_5.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Exercise6_5 {
2+
public static void main(String[] args) {
3+
boolean flag1 = isHoopy(202);
4+
boolean flag2 = isFrabjuous(202);
5+
System.out.println(flag1);
6+
System.out.println(flag2);
7+
8+
if(flag1 && flag2) {
9+
System.out.println("ping!");
10+
}
11+
if(flag1 || flag2) {
12+
System.out.println("pong!");
13+
}
14+
}
15+
16+
public static boolean isHoopy(int x) {
17+
boolean hoopyFlag;
18+
if(x % 2 == 0) {
19+
hoopyFlag = true;
20+
} else {
21+
hoopyFlag = false;
22+
}
23+
return hoopyFlag;
24+
}
25+
26+
public static boolean isFrabjuous(int x ) {
27+
boolean frabjuousFlag;
28+
if(x > 0) {
29+
frabjuousFlag = true;
30+
} else {
31+
frabjuousFlag = false;
32+
}
33+
return frabjuousFlag;
34+
}
35+
}
36+
37+
// Results are: true; true; ping!; pong!;

ch06/Exercise6_8.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Exercise6_8 {
2+
public static void main(String[] args) {
3+
System.out.println(ack(3,2));
4+
}
5+
public static int ack(int m, int n) {
6+
if(m == 0) {
7+
return n + 1;
8+
}
9+
if(m > 0 && n == 0) {
10+
return ack(m - 1, 1);
11+
}
12+
if(m > 0 && n > 0) {
13+
return ack(m -1, ack(m, n - 1));
14+
//return 0 if n or m is less than 0;
15+
} else {
16+
return 0;
17+
}
18+
}
19+
20+
}

ch06/Exercise6_9.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Exercise6_9 {
2+
public static void main(String[] args) {
3+
System.out.println(power(2.0, 80));
4+
}
5+
public static double power(double x, int n) {
6+
// double dn = (double)n;
7+
// Original Exercise
8+
// return x * Math.pow(x, (n - 1));
9+
10+
// Optional Exercise
11+
double temp = Math.pow(x, (n/2));
12+
return Math.pow(temp, 2);
13+
}
14+
}
15+

ch06/Fibonacci.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Fibonacci {
2+
public static void main(String[] args) {
3+
System.out.println(fibonacci(3));
4+
}
5+
public static int fibonacci(int n) {
6+
if(n == 1 || n == 2) {
7+
return 1;
8+
}
9+
return fibonacci(n - 1) + fibonacci(n - 2);
10+
}
11+
}

ch06/FibonacciTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import junit.framework.TestCase;
2+
3+
/**
4+
* A JUnit test case class.
5+
* Every method starting with the word "test" will be called when running
6+
* the test with JUnit.
7+
*/
8+
public class FibonacciTest extends TestCase {
9+
10+
/**
11+
* A test method.
12+
* (Replace "X" with a name describing the test. You may write as
13+
* many "testSomething" methods in this class as you wish, and each
14+
* one will be called when running JUnit over this class.)
15+
*/
16+
public void testFibonacci() {
17+
assertEquals(1, Series.fibonacci(1));
18+
assertEquals(1, Series.fibonacci(2));
19+
assertEquals(2, Series.fibonacci(3));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)