From 8a8b457c98e207d809a7616e73eb59bfe197a7a5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 31 Mar 2016 11:43:21 -0400 Subject: More code maintenance --- .../java/bjc/utils/examples/BinarySearcher.java | 73 +++++++++++++--------- .../bjc/utils/examples/parsing/ShuntTester.java | 16 ++--- .../utils/examples/parsing/TreeConstructTest.java | 28 ++++----- .../bjc/utils/examples/rangen/DiabloItemGen.java | 20 +++--- .../examples/rangen/RandomStringExamples.java | 2 +- 5 files changed, 77 insertions(+), 62 deletions(-) (limited to 'BJC-Utils2/src/examples/java') diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java index 6492ade..c3b494c 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearcher.java @@ -19,20 +19,20 @@ public class BinarySearcher { * Unused CLI args */ public static void main(String[] args) { - Scanner s = new Scanner(System.in); + Scanner inputSource = new Scanner(System.in); System.out.println("Binary Tree Constructor/Searcher"); - char c = 'n'; + char command = ' '; - BinarySearchTree bst = + BinarySearchTree searchTree = new BinarySearchTree<>((o1, o2) -> o1 - o2); - while (c != 'e') { + while (command != 'e') { System.out.print("Enter a command (m for help): "); - c = s.nextLine().charAt(0); + command = inputSource.nextLine().charAt(0); - switch (c) { + switch (command) { case 'm': System.out.println("Valid commands: "); System.out.println("\tm: Display this help message."); @@ -53,55 +53,62 @@ public class BinarySearcher { case 'a': System.out.print( "Enter the letter to add to the binary tree: "); - c = s.nextLine().charAt(0); + command = inputSource.nextLine().charAt(0); - bst.addNode(c); + searchTree.addNode(command); break; + case 'r': System.out.print( "Enter the letter to add to the binary tree: "); - c = s.nextLine().charAt(0); + command = inputSource.nextLine().charAt(0); - bst.deleteNode(c); + searchTree.deleteNode(command); break; + case 'd': - displayTree(bst, s); + displayTree(searchTree, inputSource); break; + case 'f': System.out.print( "Enter the letter to add to the binary tree: "); - c = s.nextLine().charAt(0); + command = inputSource.nextLine().charAt(0); - System.out.println("Node " + c + " was " - + (bst.isInTree(c) ? "" : "not ") + "found"); + System.out.println("Node " + command + " was " + + (searchTree.isInTree(command) ? "" : "not ") + + "found"); break; + case 't': - bst.trim(); + searchTree.trim(); break; + case 'b': - bst.balance(); + searchTree.balance(); break; + default: System.out.println("ERROR: Unrecognized command."); } - } - s.close(); + + inputSource.close(); } - private static void displayTree(BinarySearchTree bst, - Scanner s) { + private static void displayTree(BinarySearchTree tree, + Scanner inputSource) { System.out.print( "What order would you like the tree to be printed in (m for options): "); - char c; + char command; while (true) { - c = s.nextLine().charAt(0); + command = inputSource.nextLine().charAt(0); - TreeLinearizationMethod tlm = null; + TreeLinearizationMethod linearizationMethod = null; - switch (c) { + switch (command) { case 'm': System.out.println("Possible tree printing methods: "); System.out.println( @@ -111,24 +118,30 @@ public class BinarySearcher { System.out.println( "\to: Postorder printing (print left first, then right & parent)."); break; + case 'p': - tlm = TreeLinearizationMethod.PREORDER; + linearizationMethod = TreeLinearizationMethod.PREORDER; break; + case 'i': - tlm = TreeLinearizationMethod.INORDER; + linearizationMethod = TreeLinearizationMethod.INORDER; break; + case 'o': - tlm = TreeLinearizationMethod.POSTORDER; + linearizationMethod = + TreeLinearizationMethod.POSTORDER; break; + default: System.out.println("ERROR: Unknown command."); } - if (tlm != null) { - bst.traverse(tlm, ch -> { - System.out.println("Node: " + ch); + if (linearizationMethod != null) { + tree.traverse(linearizationMethod, (element) -> { + System.out.println("Node: " + element); return true; }); + return; } diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java index 7b69c95..5016834 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTester.java @@ -20,19 +20,19 @@ public class ShuntTester { * Unused CLI args */ public static void main(String[] args) { - Scanner scn = new Scanner(System.in); + Scanner inputSource = new Scanner(System.in); System.out.print("Enter a expression to shunt: "); - String ln = scn.nextLine(); + String line = inputSource.nextLine(); ShuntingYard yard = new ShuntingYard<>(); - FunctionalList ls = yard.postfix( - new FunctionalStringTokenizer(ln).toList((s) -> s), - (s) -> s); + FunctionalList shuntedTokens = + yard.postfix(new FunctionalStringTokenizer(line) + .toList((strang) -> strang), (strang) -> strang); - System.out.println(ls.toString()); + System.out.println(shuntedTokens.toString()); - scn.close(); + 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 fe9b5e6..509c407 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 @@ -23,24 +23,24 @@ public class TreeConstructTest { * Unused CLI args */ public static void main(String[] args) { - Scanner scn = new Scanner(System.in); + Scanner inputSource = new Scanner(System.in); System.out.print("Enter a expression to parse: "); - String ln = scn.nextLine(); + String line = inputSource.nextLine(); ShuntingYard yard = new ShuntingYard<>(); - FunctionalList ls = yard.postfix( - new FunctionalStringTokenizer(ln).toList((s) -> s), - (s) -> s); + FunctionalList shuntedTokens = + yard.postfix(new FunctionalStringTokenizer(line) + .toList((strang) -> strang), (s) -> s); - System.out.println("Shunted: " + ls.toString()); + System.out.println("Shunted: " + shuntedTokens.toString()); - AST ast = - TreeConstructor.constructTree(ls, new Predicate() { + AST constructedTree = TreeConstructor + .constructTree(shuntedTokens, new Predicate() { @Override - public boolean test(String tok) { - switch (tok) { + public boolean test(String token) { + switch (token) { case "+": case "-": case "*": @@ -50,10 +50,10 @@ public class TreeConstructTest { return false; } } - }, (op) -> false, null); + }, (operator) -> false, null); - System.out.println("AST: " + ast.toString()); + System.out.println("AST: " + constructedTree.toString()); - scn.close(); + 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 1b5b37c..10bf131 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 @@ -11,11 +11,12 @@ import bjc.utils.gen.WeightedGrammar; * */ public class DiabloItemGen { - private static WeightedGrammar parts = new WeightedGrammar<>(); + private static WeightedGrammar rules = new WeightedGrammar<>(); - private static void addCase(String rn, int prob, String prts) { - parts.addCase(rn, prob, - FunctionalStringTokenizer.fromString(prts).toList(s -> s)); + private static void addCase(String ruleName, int probability, + String ruleParts) { + rules.addCase(ruleName, probability, FunctionalStringTokenizer + .fromString(ruleParts).toList(s -> s)); } private static void addInfixRules() { @@ -69,20 +70,21 @@ public class DiabloItemGen { * Unused CLI args */ public static void main(String[] args) { - parts.addRule(""); + rules.addRule(""); addItemRules(); - parts.addRule(""); + rules.addRule(""); addSuffixRules(); - parts.addRule(""); + rules.addRule(""); addPrefixRules(); - parts.addRule(""); + rules.addRule(""); addInfixRules(); for (int i = 0; i < 100; i++) { - FunctionalList ls = parts.genList("", " "); + FunctionalList ls = + rules.generateListValues("", " "); StringBuilder sb = new StringBuilder(); 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 95ff5ba..bb1f8c6 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 @@ -44,7 +44,7 @@ public class RandomStringExamples { ""); for (int i = 0; i < 10; i++) { - FunctionalList ls = rg.genList("", " "); + FunctionalList ls = rg.generateListValues("", " "); StringBuilder sb = new StringBuilder(); -- cgit v1.2.3