-
-
Notifications
You must be signed in to change notification settings - Fork 5
Added examples and adv. subdirectories #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9e30ab7
0225037
27663f6
8d4141f
022ec37
434ea82
deea919
a060bac
9ec2862
61d313e
d7220b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Susan | ||
Jeremiah | ||
Josh | ||
Kati | ||
Sher | ||
Lin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SUSAN | ||
JEREMIAH | ||
JOSH | ||
KATI | ||
SHER | ||
LIN |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
35 | ||
94 | ||
16 | ||
38 | ||
82 | ||
87 | ||
52 | ||
60 | ||
86 | ||
59 | ||
72 | ||
7 | ||
60 | ||
56 | ||
63 | ||
11 | ||
67 | ||
77 | ||
67 | ||
82 | ||
28 | ||
13 | ||
100 | ||
92 | ||
85 | ||
69 | ||
75 | ||
6 | ||
83 | ||
100 | ||
76 | ||
51 | ||
13 | ||
84 | ||
59 | ||
30 | ||
21 | ||
72 | ||
15 | ||
28 | ||
15 | ||
95 | ||
87 | ||
61 | ||
80 | ||
22 | ||
45 | ||
9 | ||
73 | ||
85 | ||
85 | ||
2 | ||
90 | ||
75 | ||
60 | ||
80 | ||
91 | ||
21 | ||
80 | ||
41 | ||
82 | ||
27 | ||
48 | ||
43 | ||
37 | ||
52 | ||
78 | ||
21 | ||
54 | ||
20 | ||
15 | ||
49 | ||
42 | ||
89 | ||
63 | ||
24 | ||
10 | ||
62 | ||
44 | ||
63 | ||
51 | ||
34 | ||
36 | ||
21 | ||
2 | ||
49 | ||
64 | ||
77 | ||
34 | ||
80 | ||
7 | ||
83 | ||
51 | ||
46 | ||
11 | ||
25 | ||
59 | ||
96 | ||
21 | ||
65 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
35 | ||
94 | ||
16 | ||
38 | ||
82 | ||
87 | ||
52 | ||
60 | ||
86 | ||
59 | ||
72 | ||
7 | ||
60 | ||
56 | ||
63 | ||
11 | ||
67 | ||
77 | ||
67 | ||
82 | ||
28 | ||
13 | ||
100 | ||
92 | ||
85 | ||
69 | ||
75 | ||
6 | ||
83 | ||
100 | ||
76 | ||
51 | ||
13 | ||
84 | ||
59 | ||
30 | ||
21 | ||
72 | ||
15 | ||
28 | ||
15 | ||
95 | ||
87 | ||
61 | ||
80 | ||
22 | ||
45 | ||
9 | ||
73 | ||
85 | ||
85 | ||
2 | ||
90 | ||
75 | ||
60 | ||
80 | ||
91 | ||
21 | ||
80 | ||
41 | ||
82 | ||
27 | ||
48 | ||
43 | ||
37 | ||
52 | ||
78 | ||
21 | ||
54 | ||
20 | ||
15 | ||
49 | ||
42 | ||
89 | ||
63 | ||
24 | ||
10 | ||
62 | ||
44 | ||
63 | ||
51 | ||
34 | ||
36 | ||
21 | ||
2 | ||
49 | ||
64 | ||
77 | ||
34 | ||
80 | ||
7 | ||
83 | ||
51 | ||
46 | ||
11 | ||
25 | ||
59 | ||
96 | ||
21 | ||
65 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.codefortomorrow.advanced.chapter13.examples; | ||
|
||
// Convert the iterative gcd algorithm to a recursive algorithm. | ||
|
||
public class IterToRecursive { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("With iterative function:"); | ||
|
||
int a = 72, b = 20; | ||
while (a != b) { | ||
if (a > b) a -= b; else if (b > a) b -= a; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
} | ||
System.out.println("GCD:" + a); | ||
|
||
System.out.println("With recursive function:"); | ||
System.out.println("GCD:" + gcd(72, 20)); | ||
} | ||
|
||
// Recursive function | ||
public static int power(int base, int exp) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
// Base case: exp = 0, so the result is 1. | ||
if (exp == 0) return 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
||
// Multiply once by the previous power. | ||
return power(base, exp - 1) * base; | ||
} | ||
|
||
public static int gcd(int a, int b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
// Base case | ||
if (a == b) return a; else if (a > b) return gcd( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
a - b, | ||
b | ||
); else return gcd(a, b - a); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.codefortomorrow.advanced.chapter13.practice; | ||
|
||
/* | ||
Implement the iterative code as a recursive | ||
function to clarify what the algorithm does. | ||
*/ | ||
|
||
public class NegativeSum { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Iterative:"); | ||
int a = 50, b = 40; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
while (a + b > 0) { | ||
if (Math.pow(a, 2) + Math.pow(b, 2) > 10000) { | ||
a *= 8; | ||
b *= -4; | ||
} else { | ||
a *= 3; | ||
b /= 2; | ||
} | ||
} | ||
System.out.println("a + b: " + (a + b)); | ||
|
||
System.out.println("Recursive:"); | ||
System.out.println(negSum(50, 40)); | ||
} | ||
|
||
public static int negSum(int a, int b) { | ||
// Insert code here and change return statement. | ||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.codefortomorrow.advanced.chapter13.practice; | ||
|
||
/* | ||
Implement the iterative code as a recursive | ||
function to clarify what the algorithm does. | ||
*/ | ||
|
||
public class NegativeSum { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Iterative:"); | ||
int a = 50, b = 40; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
while (a + b > 0) { | ||
if (Math.pow(a, 2) + Math.pow(b, 2) > 10000) { | ||
a *= 8; | ||
b *= -4; | ||
} else { | ||
a *= 3; | ||
b /= 2; | ||
} | ||
} | ||
System.out.println("a + b: " + (a + b)); | ||
|
||
System.out.println("Recursive:"); | ||
System.out.println(negSum(50, 40)); | ||
} | ||
|
||
public static int negSum(int a, int b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
if (a + b <= 0) return a + b; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
if (Math.pow(a, 2) + Math.pow(b, 2) > 10000) return negSum( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
a * 8, | ||
b * -4 | ||
); else return negSum(a * 3, b / 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.codefortomorrow.advanced.chapter14.examples; | ||
|
||
import java.io.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
||
public class FileIO { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
||
public static void main(String[] args) throws IOException { | ||
FileInputStream in = null; | ||
FileOutputStream out = null; | ||
File file = new File("names.txt"); | ||
|
||
try { | ||
in = new FileInputStream(file); | ||
out = new FileOutputStream("names_upper.txt"); | ||
int c; | ||
while ((c = in.read()) != -1) out.write( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
Character.toUpperCase((char) c) | ||
); | ||
} catch (IOException e) { | ||
System.out.println("An IO Exception occurred."); | ||
e.printStackTrace(); // Prints error output stream. | ||
} finally { | ||
if (in != null) in.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
if (out != null) out.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.codefortomorrow.advanced.chapter14.examples; | ||
|
||
public class MyException extends Exception { | ||
|
||
public MyException() {} | ||
|
||
public MyException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.codefortomorrow.advanced.chapter14.examples; | ||
|
||
public class Throw { | ||
|
||
public static void main(String[] args) { | ||
try { | ||
System.out.println(divide(7, 2)); | ||
} catch (ArithmeticException e) { | ||
System.out.println("There was an arithmetic exception."); | ||
} | ||
} | ||
|
||
public static int divide(int a, int b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
int result = a / b; | ||
if (result != (double) a / (double) b) throw new ArithmeticException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
System.out.println("Got to end of divide()"); | ||
return a / b; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[reviewdog] reported by reviewdog 🐶
Each variable declaration must be in its own statement.