diff --git a/a1/src/edu/wmich/cs3310/a1/ComputeTime.java b/a1/src/edu/wmich/cs3310/a1/ComputeTime.java index c9b921f210395434a63909031eea13038d63b111..6ab5b43538ff340ebaec418b57d7030d49d5b9be 100644 --- a/a1/src/edu/wmich/cs3310/a1/ComputeTime.java +++ b/a1/src/edu/wmich/cs3310/a1/ComputeTime.java @@ -34,6 +34,8 @@ public class ComputeTime { //region Methods + //region Private Methods + /** * Determines the difference in milliseconds between two times. * @param time1 Start time. @@ -47,10 +49,46 @@ public class ComputeTime { startMilliseconds = time1.getTime(); endMilliseconds = time2.getTime(); differenceMilliseconds = endMilliseconds - startMilliseconds; - //PrintTime(differenceMilliseconds); return differenceMilliseconds; } + //endregion Private Methods + + + + //region Public Methods + + /** + * Calculates the difference between provided start time and current time. + * @param startDate Start time. + * @return Long of elapsed time. + */ + public long GetTimeLapse(Date startDate) { + long timeResult; + + // Get time lapse of process. + timeResult = ComputeDateDifference(startDate, (new Date())); + return timeResult; + } + + + /** + * Calculates the difference between provided start time and current time. + * Also outputs time and additional information to user. + * @param startDate Start time. + * @param userDisplayString Additional info to show user. + * @return Long of elapsed time. + */ + public long GetTimeLapse(Date startDate, String userDisplayString) { + long timeResult; + System.out.print(userDisplayString); + + // Get time lapse of process. + timeResult = ComputeDateDifference(startDate, (new Date())); + PrintTime(timeResult); + return timeResult; + } + /** * Calculates hours, minutes, seconds, and milliseconds based upon total milliseconds. @@ -70,9 +108,12 @@ public class ComputeTime { hours = minutes / 60; minutes = minutes % 60; - System.out.println(//"H:M:S:m " + - hours + ":" + minutes + ":" + seconds + ":" + miliseconds); + // Prints in H:M:S:m format. + System.out.println( + hours + ":" + minutes + ":" + seconds + ":" + miliseconds); } + //endregion Public Methods + //endregion Methods } diff --git a/a1/src/edu/wmich/cs3310/a1/QueueCheckBalancedParentheses.java b/a1/src/edu/wmich/cs3310/a1/QueueCheckBalancedParentheses.java index 594d8365c4447c60ed6f19f2656f9170680abd00..eaa8380552738deddc27bd7faf4845d9caa3d5c7 100644 --- a/a1/src/edu/wmich/cs3310/a1/QueueCheckBalancedParentheses.java +++ b/a1/src/edu/wmich/cs3310/a1/QueueCheckBalancedParentheses.java @@ -83,6 +83,46 @@ public class QueueCheckBalancedParentheses extends CharQueue{ return (rightCounter + leftCounter); } + + /** + * Checks for parenthesis balance, via simulating a singly-linked linked list. + * Returns int based on number of unpaired parens. + * @param value String to parse for parens. + * @return Number of unpaired parens. + */ + public int CheckBalancedParenthesisSingly(String value) { + setText(value); + int stringLength = text.length(); + int index = 0; + int leftCounter = 0; + int rightCounter = 0; + + while (index < stringLength) { + this.Enqueue_Singly(text.charAt(index)); + index++; + } + + while ((tempNode = this.Dequeue()) != null) { + + // Check for left paren. + if (tempNode.myData() == '(') { + leftCounter++; + } + + // Check for right paren. + if (tempNode.myData() == ')') { + if (leftCounter < 1) { + // No match. Increasing counter. + rightCounter++; + } else { + // Paren has match. Decreasing counter. + leftCounter--; + } + } + } + return (rightCounter + leftCounter); + } + //endregion Methods } diff --git a/a1/src/edu/wmich/cs3310/a1/StackQueueDemo.java b/a1/src/edu/wmich/cs3310/a1/StackQueueDemo.java index baf0d36a64878d2325eade59e84aa167cae217a7..5364459e7c5ebf13e0c89433370730abcf82722d 100644 --- a/a1/src/edu/wmich/cs3310/a1/StackQueueDemo.java +++ b/a1/src/edu/wmich/cs3310/a1/StackQueueDemo.java @@ -3,6 +3,7 @@ package edu.wmich.cs3310.a1; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Date; import java.util.Scanner; @@ -10,14 +11,13 @@ public class StackQueueDemo { //region Variables - private int stackBalance; - private int queueBalance; private boolean runBool; private String userInputString; - private StackCheckBalancedParentheses stackCheck; - private QueueCheckBalancedParentheses queueCheck; private Scanner reader = new Scanner(System.in); // For user input. private Scanner scanner; // For file reading. + private StackCheckBalancedParentheses stackCheck; + private QueueCheckBalancedParentheses queueCheck; + private ComputeTime computeTime; //endregion Variables @@ -31,6 +31,7 @@ public class StackQueueDemo { */ public StackQueueDemo() throws IOException{ runBool = true; + computeTime = new ComputeTime(); stackCheck = new StackCheckBalancedParentheses(); queueCheck = new QueueCheckBalancedParentheses(); @@ -111,6 +112,8 @@ public class StackQueueDemo { */ private void ReadFile(String fileString) { String tempString; + Date startDate = new Date(); + try { Path filePath = Paths.get(fileString); @@ -124,6 +127,10 @@ public class StackQueueDemo { } catch (Exception e) { System.out.println("No such path exists."); } + + computeTime.GetTimeLapse(startDate, "Total File Processing Time: "); + System.out.println(); + System.out.println(); } @@ -133,13 +140,49 @@ public class StackQueueDemo { * @param printBool Inicates if string should be printed for user display. */ private void ProcessString(String lineString, boolean printBool) { + int stackBalance; + int queueBalance; + long stackTime; + long queueSinglyTime; + long queueDoublyTime; + long totalTime; + Date stackStartDate; + Date queueSinglyStartDate; + Date queueDoublyStartDate; + Date totalStartDate = new Date(); + if (printBool) { System.out.println(lineString); } + + // Run string through stack methods. + stackStartDate = new Date(); stackBalance = stackCheck.CheckBalancedParenthesis(lineString); - queueBalance = queueCheck.CheckBalancedParenthesis(lineString); + + stackTime = computeTime.GetTimeLapse(stackStartDate); + + // Run string through singly-linked linked list methods. + queueSinglyStartDate = new Date(); + queueBalance = queueCheck.CheckBalancedParenthesisSingly(lineString); + queueSinglyTime = computeTime.GetTimeLapse(queueSinglyStartDate); + + // Run string through doubly-linked linked list methods. + queueDoublyStartDate = new Date(); + queueCheck.CheckBalancedParenthesis(lineString); + queueDoublyTime = computeTime.GetTimeLapse(queueDoublyStartDate); + totalTime = computeTime.GetTimeLapse(totalStartDate); + + // Print output for user. System.out.println("Stack balance: " + stackBalance); System.out.println("Queue balance: " + queueBalance); + System.out.print("Stack Processing Time: "); + computeTime.PrintTime(stackTime); + System.out.print("Doubly-Linked Queue Processing Time: "); + computeTime.PrintTime(queueDoublyTime); + System.out.print("Singly-Linked Queue Processing Time: "); + computeTime.PrintTime(queueSinglyTime); + System.out.print("Total String Processing Time: "); + computeTime.PrintTime(totalTime); System.out.println(); } diff --git a/a1/tests/ComputeTimeTests.java b/a1/tests/ComputeTimeTests.java index fc21ebe4010b5014480632a1d85fcdb6437ad1ce..cec8a7614ca9a1d894b75116c2c7ea0d6adb8cc9 100644 --- a/a1/tests/ComputeTimeTests.java +++ b/a1/tests/ComputeTimeTests.java @@ -42,6 +42,7 @@ public class ComputeTimeTests { Assert.assertEquals(0, result); } + @Test public void TestMillisecondDifference() { // Date1 earlier than date2. @@ -59,6 +60,7 @@ public class ComputeTimeTests { Assert.assertEquals(-1, result); } + @Test public void TestSecondDifference() { // Date1 earlier than date2. @@ -76,6 +78,7 @@ public class ComputeTimeTests { Assert.assertEquals(-1000, result); } + @Test public void TestMinuteDifference() { // Date1 earlier than date2. @@ -93,6 +96,7 @@ public class ComputeTimeTests { Assert.assertEquals(-(60 * 1000), result); } + @Test public void TestHourDifference() { // Date1 earlier than date2. @@ -110,6 +114,7 @@ public class ComputeTimeTests { Assert.assertEquals(-(60 * 60 * 1000), result); } + @Test public void TestDayDifference() { // Date1 earlier than date2.