From 1551b96b95669c9e8d068e043a4d78d5e2e088d2 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 28 Aug 2021 00:19:56 +0200 Subject: [PATCH 1/4] Monte Carlo [cpp]: Hardcode sample count This removes the need for user input --- contents/monte_carlo_integration/code/c++/monte_carlo.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contents/monte_carlo_integration/code/c++/monte_carlo.cpp b/contents/monte_carlo_integration/code/c++/monte_carlo.cpp index beff97170..f38d83f45 100644 --- a/contents/monte_carlo_integration/code/c++/monte_carlo.cpp +++ b/contents/monte_carlo_integration/code/c++/monte_carlo.cpp @@ -39,10 +39,7 @@ double monte_carlo_pi(unsigned samples) { int main() { unsigned samples; - std::cout << "Enter samples to use: "; - std::cin >> samples; - - double pi_estimate = monte_carlo_pi(samples); + double pi_estimate = monte_carlo_pi(10000000); std::cout << "Pi = " << pi_estimate << '\n'; std::cout << "Percent error is: " << 100 * std::abs(pi_estimate - PI) / PI << " %\n"; } From 11610b5976aa78f47338ccfde212f02de8769b40 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 28 Aug 2021 00:27:46 +0200 Subject: [PATCH 2/4] Verlet Integration [java]: Move VerletValue class into Verlet class --- contents/verlet_integration/code/java/Verlet.java | 12 ++++++++++++ .../verlet_integration/code/java/VerletValues.java | 9 --------- contents/verlet_integration/verlet_integration.md | 7 +++---- 3 files changed, 15 insertions(+), 13 deletions(-) delete mode 100644 contents/verlet_integration/code/java/VerletValues.java diff --git a/contents/verlet_integration/code/java/Verlet.java b/contents/verlet_integration/code/java/Verlet.java index b7494d65f..a7bb94447 100644 --- a/contents/verlet_integration/code/java/Verlet.java +++ b/contents/verlet_integration/code/java/Verlet.java @@ -1,4 +1,16 @@ public class Verlet { + + private static class VerletValues { + public double time; + public double vel; + + public VerletValues(double time, double vel) { + this.time = time; + this.vel = vel; + } + } + + static double verlet(double pos, double acc, double dt) { // Note that we are using a temp variable for the previous position diff --git a/contents/verlet_integration/code/java/VerletValues.java b/contents/verlet_integration/code/java/VerletValues.java deleted file mode 100644 index 8dc5ceb84..000000000 --- a/contents/verlet_integration/code/java/VerletValues.java +++ /dev/null @@ -1,9 +0,0 @@ -public class VerletValues { - public double time; - public double vel; - - public VerletValues(double time, double vel) { - this.time = time; - this.vel = vel; - } -} diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 06fe319c6..402a7ba1b 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -37,7 +37,7 @@ Here is what it looks like in code: {% sample lang="c" %} [import:3-14, lang:"c"](code/c/verlet.c) {% sample lang="java" %} -[import:2-17, lang:"java"](code/java/Verlet.java) +[import:14-29, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import:1-10, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} @@ -95,7 +95,7 @@ However, the error for this is $$\mathcal{O}(\Delta t)$$, which is quite poor, b {% sample lang="c" %} [import:16-31, lang:"c"](code/c/verlet.c) {% sample lang="java" %} -[import:19-37, lang:"java"](code/java/Verlet.java) +[import:31-49, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import:12-23, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} @@ -167,7 +167,7 @@ Here is the velocity Verlet method in code: {% sample lang="c" %} [import:33-43, lang:"c"](code/c/verlet.c) {% sample lang="java" %} -[import:39-51, lang:"java"](code/java/Verlet.java) +[import:51-63, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import:25-34, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} @@ -225,7 +225,6 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w {% sample lang="c" %} [import, lang:"c"](code/c/verlet.c) {% sample lang="java" %} -[import, lang:"java"](code/java/VerletValues.java) [import, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import, lang:"python"](code/python/verlet.py) From 4c9a7746a7efd208bb95d76f2b2c83611353f75e Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 28 Aug 2021 00:32:19 +0200 Subject: [PATCH 3/4] Tree Traversal [java]: Move main method into Tree class --- .../tree_traversal/code/java/MainClass.java | 31 ------------------- contents/tree_traversal/code/java/Tree.java | 30 ++++++++++++++++++ contents/tree_traversal/tree_traversal.md | 2 -- 3 files changed, 30 insertions(+), 33 deletions(-) delete mode 100644 contents/tree_traversal/code/java/MainClass.java diff --git a/contents/tree_traversal/code/java/MainClass.java b/contents/tree_traversal/code/java/MainClass.java deleted file mode 100644 index 59dd20c09..000000000 --- a/contents/tree_traversal/code/java/MainClass.java +++ /dev/null @@ -1,31 +0,0 @@ -//submitted by xam4lor -public class MainClass { - public static void main(String[] args) { - System.out.println("Creating Tree"); - Tree tree = new Tree(3, 3); - - System.out.println("Using recursive DFS :"); - tree.dfsRecursive(); - - System.out.println("Using stack-based DFS :"); - tree.dfsStack(); - - System.out.println("Using queue-based BFS :"); - tree.bfsQueue(); - - System.out.println("Using post-order recursive DFS :"); - tree.dfsRecursivePostOrder(); - - - // Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work. - System.out.println("Using in-order binary recursive DFS : (fail)"); - tree.dfsRecursiveInOrderBinary(); - - tree = new Tree(3, 2); - System.out.println("Using in-order binary recursive DFS : (succeed)"); - tree.dfsRecursiveInOrderBinary(); - - - System.out.println(""); - } -} diff --git a/contents/tree_traversal/code/java/Tree.java b/contents/tree_traversal/code/java/Tree.java index 595df47b0..1dee7d9d1 100644 --- a/contents/tree_traversal/code/java/Tree.java +++ b/contents/tree_traversal/code/java/Tree.java @@ -124,4 +124,34 @@ public int compareTo(Node other) { return Integer.compare(this.id, other.id); } } + + public static void main(String[] args) { + System.out.println("Creating Tree"); + Tree tree = new Tree(3, 3); + + System.out.println("Using recursive DFS :"); + tree.dfsRecursive(); + + System.out.println("Using stack-based DFS :"); + tree.dfsStack(); + + System.out.println("Using queue-based BFS :"); + tree.bfsQueue(); + + System.out.println("Using post-order recursive DFS :"); + tree.dfsRecursivePostOrder(); + + + // Uncommenting the following 2 lines will result in an exception thrown because at least one Node of the Tree has more than 2 children and therefor a DFSRecursiveInorderBinary doesn't work. + System.out.println("Using in-order binary recursive DFS : (fail)"); + tree.dfsRecursiveInOrderBinary(); + + tree = new Tree(3, 2); + System.out.println("Using in-order binary recursive DFS : (succeed)"); + tree.dfsRecursiveInOrderBinary(); + + + System.out.println(""); + } + } diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index 3cc5a2fbb..e03f09652 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/tree_traversal/tree_traversal.md @@ -330,8 +330,6 @@ Here is a video describing tree traversal: {% sample lang="java" %} ##### Tree.java [import, lang:"java"](code/java/Tree.java) -##### MainClass.java -[import, lang:"java"](code/java/MainClass.java) {% sample lang="js" %} [import, lang:"javascript"](code/javascript/tree.js) {% sample lang="py" %} From 38193ae619bee2c22efc9f00c65b43176c8b2aa9 Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Sat, 28 Aug 2021 00:35:33 +0200 Subject: [PATCH 4/4] Huffman Encoding [java]: Move Huffman class to the top of the file Executing a single java file without compilation (in java >= 11) only works if the first class it finds contains the main method. --- .../huffman_encoding/code/java/huffman.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/contents/huffman_encoding/code/java/huffman.java b/contents/huffman_encoding/code/java/huffman.java index 63ceff43a..1c67ff4e1 100644 --- a/contents/huffman_encoding/code/java/huffman.java +++ b/contents/huffman_encoding/code/java/huffman.java @@ -1,5 +1,15 @@ import java.util.*; +class Huffman { + public static void main(String[] args) { + HuffmanTree huffmanTree = new HuffmanTree("bibbity_bobbity"); + huffmanTree.createTree(); + String encoded = huffmanTree.encode(); + System.out.println("Encoded String: " + encoded); + System.out.println("Decoded String: " + huffmanTree.decode(encoded)); + } +} + class TreeNode { String letter = ""; int frequency = 0; @@ -101,13 +111,3 @@ public String decode(String encoded) { return decoded.toString(); } } - -class Huffman { - public static void main(String[] args) { - HuffmanTree huffmanTree = new HuffmanTree("bibbity_bobbity"); - huffmanTree.createTree(); - String encoded = huffmanTree.encode(); - System.out.println("Encoded String: " + encoded); - System.out.println("Decoded String: " + huffmanTree.decode(encoded)); - } -}