From e8567df90f38e5f2e15fbb245aa4ac45d37a3003 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 26 Feb 2017 09:06:49 -0500 Subject: Example formatting --- .../java/bjc/utils/examples/BinarySearchTest.java | 154 +++++++++------------ .../java/bjc/utils/examples/parsing/ShuntTest.java | 8 +- .../utils/examples/parsing/TreeConstructTest.java | 43 +++--- .../bjc/utils/examples/rangen/DiabloItemGen.java | 11 +- .../examples/rangen/RandomStringExamples.java | 51 +++++-- 5 files changed, 128 insertions(+), 139 deletions(-) (limited to 'BJC-Utils2/src') diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java index 1beaeec..3c6f124 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java @@ -14,41 +14,31 @@ import bjc.utils.funcdata.bst.TreeLinearizationMethod; public class BinarySearchTest { private static void display(BinarySearchTree tree, Scanner input) { - System.out.print( - "What order would you like the tree to be printed in (m for options): "); - + System.out.print("What order would you like the tree to be printed in (m for options): "); char command; while (true) { command = input.nextLine().charAt(0); - TreeLinearizationMethod method = null; switch (command) { - case 'm': - System.out.println("Possible tree printing methods: "); - System.out.println( - "\tp: Preorder printing (print parent first, then left & right)."); - System.out.println( - "\ti: Inorder printing (print left first, then parent & right)."); - System.out.println( - "\to: Postorder printing (print left first, then right & parent)."); - break; - - case 'p': - method = TreeLinearizationMethod.PREORDER; - break; - - case 'i': - method = TreeLinearizationMethod.INORDER; - break; - - case 'o': - method = TreeLinearizationMethod.POSTORDER; - break; - - default: - System.out.println("ERROR: Unknown command."); + case 'm': + System.out.println("Possible tree printing methods: "); + System.out.println("\tp: Preorder printing (print parent first, then left & right)."); + System.out.println("\ti: Inorder printing (print left first, then parent & right)."); + System.out.println("\to: Postorder printing (print left first, then right & parent)."); + break; + case 'p': + method = TreeLinearizationMethod.PREORDER; + break; + case 'i': + method = TreeLinearizationMethod.INORDER; + break; + case 'o': + method = TreeLinearizationMethod.POSTORDER; + break; + default: + System.out.println("ERROR: Unknown command."); } if (method != null) { @@ -60,8 +50,7 @@ public class BinarySearchTest { return; } - System.out.print( - "What order would you like the tree to be printed in (m for options): "); + System.out.print("What order would you like the tree to be printed in (m for options): "); } } @@ -78,71 +67,58 @@ public class BinarySearchTest { char command = ' '; - BinarySearchTree tree = new BinarySearchTree<>( - (o1, o2) -> o1 - o2); + BinarySearchTree tree = new BinarySearchTree<>((o1, o2) -> o1 - o2); while (command != 'e') { System.out.print("Enter a command (m for help): "); command = input.nextLine().charAt(0); switch (command) { - case 'm': - System.out.println("Valid commands: "); - System.out.println("\tm: Display this help message."); - System.out.println("\te: Exit this program."); - System.out.println( - "\ta: Add a node to the binary tree."); - System.out.println("\td: Display the binary tree."); - System.out.println( - "\tr: Remove a node from the binary tree."); - System.out.println( - "\tf: Check if a given node is in the binary tree."); - System.out.println( - "\tt: Trim all deleted nodes from the tree."); - System.out.println( - "\tb: Balance the tree (also trims dead nodes)"); - break; - - case 'a': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = input.nextLine().charAt(0); - - tree.addNode(command); - break; - - case 'r': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = input.nextLine().charAt(0); - - tree.deleteNode(command); - break; - - case 'd': - display(tree, input); - break; - - case 'f': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = input.nextLine().charAt(0); - - System.out.println("Node " + command + " was " - + (tree.isInTree(command) ? "" : "not ") - + "found"); - break; - - case 't': - tree.trim(); - break; - - case 'b': - tree.balance(); - break; - - default: - System.out.println("ERROR: Unrecognized command."); + case 'm': + System.out.println("Valid commands: "); + System.out.println("\tm: Display this help message."); + System.out.println("\te: Exit this program."); + System.out.println("\ta: Add a node to the binary tree."); + System.out.println("\td: Display the binary tree."); + System.out.println("\tr: Remove a node from the binary tree."); + System.out.println("\tf: Check if a given node is in the binary tree."); + System.out.println("\tt: Trim all deleted nodes from the tree."); + System.out.println("\tb: Balance the tree (also trims dead nodes)"); + break; + case 'a': + System.out.print("Enter the letter to add to the binary tree: "); + command = input.nextLine().charAt(0); + + tree.addNode(command); + break; + case 'r': + System.out.print("Enter the letter to add to the binary tree: "); + command = input.nextLine().charAt(0); + + tree.deleteNode(command); + break; + case 'd': + display(tree, input); + break; + case 'f': + System.out.print("Enter the letter to add to the binary tree: "); + command = input.nextLine().charAt(0); + + boolean inTree = tree.isInTree(command); + if(inTree) { + System.out.printf("Node %s was found\n", command); + } else { + System.out.printf("Node %s was not found\n", command); + } + break; + case 't': + tree.trim(); + break; + case 'b': + tree.balance(); + break; + default: + System.out.println("ERROR: Unrecognized command."); } } diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java index 47bbaf8..1317db7 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java @@ -26,13 +26,11 @@ public class ShuntTest { String line = inputSource.nextLine(); ShuntingYard yard = new ShuntingYard<>(true); - - IList shuntedTokens = yard - .postfix(new FunctionalStringTokenizer(line) - .toList((strang) -> strang), (strang) -> strang); + IList preTokens = new FunctionalStringTokenizer(line).toList(strang -> strang); + IList shuntedTokens = yard.postfix(preTokens, strang -> strang); System.out.println(shuntedTokens.toString()); inputSource.close(); } -} \ No newline at end of file +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java index fbc3638..6559a1e 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java @@ -26,8 +26,7 @@ import bjc.utils.parserutils.TreeConstructor; * */ public class TreeConstructTest { - private static final class OperatorPicker - implements Predicate { + private static final class OperatorPicker implements Predicate { @Override public boolean test(String token) { if (StringUtils.containsOnly(token, "\\[")) { @@ -37,13 +36,13 @@ public class TreeConstructTest { } switch (token) { - case "+": - case "-": - case "*": - case "/": - return true; - default: - return false; + case "+": + case "-": + case "*": + case "/": + return true; + default: + return false; } } } @@ -61,8 +60,7 @@ public class TreeConstructTest { System.out.print("Enter a expression to parse: "); String line = inputSource.nextLine(); - IList tokens = new FunctionalStringTokenizer(line) - .toList(); + IList tokens = new FunctionalStringTokenizer(line).toList(); ShuntingYard yard = new ShuntingYard<>(true); @@ -75,23 +73,18 @@ public class TreeConstructTest { ops.add(new Pair<>(":=", ":=")); ops.add(new Pair<>("=>", "=>")); - IList semiExpandedTokens = ListUtils.splitTokens(tokens, - ops); + IList semiExpandedTokens = ListUtils.splitTokens(tokens, ops); ops = new LinkedList<>(); - ops.add(new Pair<>("(", "\\(")); ops.add(new Pair<>(")", "\\)")); ops.add(new Pair<>("[", "\\[")); ops.add(new Pair<>("]", "\\]")); - IList fullyExpandedTokens = ListUtils - .deAffixTokens(semiExpandedTokens, ops); - + IList fullyExpandedTokens = ListUtils.deAffixTokens(semiExpandedTokens, ops); fullyExpandedTokens.removeIf((strang) -> strang.equals("")); - IList shuntedTokens = yard.postfix(fullyExpandedTokens, - (token) -> token); + IList shuntedTokens = yard.postfix(fullyExpandedTokens, (token) -> token); System.out.println("Shunted: " + shuntedTokens.toString()); @@ -105,8 +98,8 @@ public class TreeConstructTest { return false; }; - IMap>, - ITree>> operators = new FunctionalMap<>(); + IMap>, ITree>> operators = + new FunctionalMap<>(); operators.put("[", (queuedTrees) -> { return null; @@ -132,12 +125,12 @@ public class TreeConstructTest { return arrayTree; }); - ITree constructedTree = TreeConstructor.constructTree( - shuntedTokens, new OperatorPicker(), specialPicker, - operators::get); + ITree constructedTree = + TreeConstructor.constructTree(shuntedTokens, + new OperatorPicker(), specialPicker, operators::get); System.out.println("AST: " + constructedTree.toString()); inputSource.close(); } -} \ No newline at end of file +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java index 521d8e3..fc1d6c2 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java @@ -15,8 +15,10 @@ public class DiabloItemGen { private static void addCase(String ruleName, int probability, String ruleParts) { - rules.addCase(ruleName, probability, FunctionalStringTokenizer - .fromString(ruleParts).toList(s -> s)); + IList parts = FunctionalStringTokenizer.fromString(ruleParts) + .toList(strang -> strang); + + rules.addCase(ruleName, probability, parts); } private static void addInfixRules() { @@ -36,7 +38,7 @@ public class DiabloItemGen { addCase(rn, 10, ""); addCase(rn, 20, " "); addCase(rn, 30, " "); - addCase(rn, 40, " "); + addCase(rn, 40, " "); addCase(rn, 50, " "); addCase(rn, 60, " "); } @@ -86,8 +88,7 @@ public class DiabloItemGen { IList ls = rules.generateListValues("", " "); StringBuilder sb = new StringBuilder(); - - ls.forEach(sp -> sb.append(sp)); + ls.forEach(sb::append); System.out.println(sb.toString().replaceAll("\\s+", " ")); } diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java index f9adb72..c8a7871 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java @@ -18,8 +18,10 @@ public class RandomStringExamples { IList> cses = new FunctionalList<>(); for (String strang : cases) { - cses.add(FunctionalStringTokenizer.fromString(strang) - .toList(s -> s)); + IList lst = FunctionalStringTokenizer.fromString(strang) + .toList(s -> s) + + cses.add(lst); } rg.makeRule(rule, cses); @@ -34,33 +36,52 @@ public class RandomStringExamples { public static void main(String[] args) { rg = new RandomGrammar<>(); - addRule("", " ", + addRule("", + " ", " thinks that I am ", - "I ", "You think that I am "); - - addRule("", "dancing", "eating", "sleeping"); - - addRule("", "", "life", "my computer", + "I ", + "You think that I am "); + + addRule("", + "dancing", + "eating", + "sleeping"); + + addRule("", + "", + "life", + "my computer", "my friends"); - addRule("", "hate", "am jealous of", "love"); + addRule("", + "hate", + "am jealous of", + "love"); - addRule("", "hates", "loves"); + addRule("", + "hates", + "loves"); - addRule("", "my sister", "my father", "my girlfriend", + addRule("", + "my sister", + "my father", + "my girlfriend", "the man next door"); - addRule("", "creative", "intelligent"); + addRule("", + "creative", + "intelligent"); - addRule("", "", " with ", + addRule("", + "", + " with ", ""); for (int i = 0; i < 10; i++) { IList ls = rg.generateListValues("", " "); StringBuilder sb = new StringBuilder(); - - ls.forEach(sp -> sb.append(sp)); + ls.forEach(sb::append); System.out.println(sb.toString().replaceAll("\\s+", " ")); } -- cgit v1.2.3