From 8a5efae1847c74a87f910168db57f3fe382b26b2 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Wed, 27 Sep 2017 18:53:20 -0400
Subject: [PATCH] Implement time-keeping and comparisons for search functions

Note: Times may be too small to record at small values.
---
 a0/src/com/CS3310/a0/ComputeTime.java |   2 +-
 a0/src/com/CS3310/a0/Controller.java  | 175 +++++++++++++++++++++++---
 2 files changed, 156 insertions(+), 21 deletions(-)

diff --git a/a0/src/com/CS3310/a0/ComputeTime.java b/a0/src/com/CS3310/a0/ComputeTime.java
index 069c7c4..581ef68 100644
--- a/a0/src/com/CS3310/a0/ComputeTime.java
+++ b/a0/src/com/CS3310/a0/ComputeTime.java
@@ -67,7 +67,7 @@ public class ComputeTime {
         hours = minutes / 60;
         minutes = minutes % 60;
 
-        System.out.println("H:M:S:m  " +
+        System.out.println(//"H:M:S:m  " +
                             hours + ":" + minutes + ":" + seconds + ":" + miliseconds);
     }
 
diff --git a/a0/src/com/CS3310/a0/Controller.java b/a0/src/com/CS3310/a0/Controller.java
index d68c598..cbd4d6e 100644
--- a/a0/src/com/CS3310/a0/Controller.java
+++ b/a0/src/com/CS3310/a0/Controller.java
@@ -36,6 +36,10 @@ public class Controller {
     public Controller() {
         boolean runProgram = true;
         String userInputString;
+        System.out.println();
+        System.out.println("Note, all times displayed are in the format of:");
+        System.out.println("   hours:minutes:seconds:milliseconds   ");
+        System.out.println();
 
         while (arrayDim1 < 1 || arrayDim2 < 1) {
             GetArrayDimensions();
@@ -84,15 +88,22 @@ public class Controller {
      * Core functionality of program.
      */
     private void RunCore() {
+        Date startDate;
         userName = GetUserInput("Enter a name to search: ");
 
         CreateArray();
 
-        PrintUnsortedArray();
+        //PrintUnsortedArray();
+        startDate = new Date();
         SearchNameUnsorted(true);
+        GetTimeLapse(startDate, "Unsorted Search Time: ");
+        System.out.println();
 
-        PrintSortedArray();
+        //PrintSortedArray();
+        startDate = new Date();
         SearchNameSorted(true);
+        GetTimeLapse(startDate, "Sorted Search Time: ");
+        System.out.println();
 
         ReplaceName();
     }
@@ -107,9 +118,7 @@ public class Controller {
         int arrayIndex2;
         int randomInt;
         char[] tempChar;
-//        Date startDate = new Date();
-//        Date endDate;
-//        long timeResult;
+        Date startDate = new Date();
 
         unsortedArray = new char[arrayDim1][arrayDim2];
         sortedArray = new char[arrayDim1 * arrayDim2];
@@ -124,13 +133,13 @@ public class Controller {
             }
         }
 
-        SortArray();
+        GetTimeLapse(startDate, "Array Creation Time: ");
 
-//        // Get time lapse of process.
-//        System.out.print("Array Creation Time: ");
-//        endDate = new Date();
-//        timeResult = computeTime.ComputeDateDifference(startDate, endDate);
-//        computeTime.PrintTime(timeResult);
+        // Sort array.
+        startDate = new Date();
+        SortArray();
+        GetTimeLapse(startDate, "Array Sorting Time: ");
+        System.out.println();
     }
 
 
@@ -167,6 +176,10 @@ public class Controller {
         char tempChar;
         unsortedFoundHolder = new char[arrayDim1][arrayDim2];
 
+        if (printAll) {
+            System.out.println("Unsorted (Linear) Search Values: ");
+        }
+
         // Create array to hold found values.
         for (arrayIndex1 = 0; arrayIndex1 < arrayDim1; arrayIndex1++) {
             for (arrayIndex2 = 0; arrayIndex2 < arrayDim2; arrayIndex2++) {
@@ -199,6 +212,10 @@ public class Controller {
         char tempChar;
         sortedFoundHolder = new char[(arrayDim1 * arrayDim2)];
 
+        if (printAll) {
+            System.out.println("Sorted (Binary) Search Values: ");
+        }
+
         // Create array to hold found values.
         for (arrayIndex1 = 0; arrayIndex1 < arrayDim1; arrayIndex1++) {
             for (arrayIndex2 = 0; arrayIndex2 < arrayDim2; arrayIndex2++) {
@@ -372,7 +389,7 @@ public class Controller {
     /**
      * Desired char was found but value is already "used".
      * Check values directly right for "unused" match.
-     * @param searchValue Char to serach for.
+     * @param searchValue Char to search for.
      * @param index Index that was "used".
      * @return Valid match or -1;
      */
@@ -409,6 +426,17 @@ public class Controller {
         int index;
         int replaceInt = 0;
         String userInputString;
+        Date startDate;
+        long elapsedTime;
+        long averageUnsortedTime;
+        long averageSortedTime;
+        long totalUnsortedTime = 0;
+        long totalSortedTime = 0;
+        long shortestUnsortedTime = 0;
+        long longestUnsortedTime = 0;
+        long shortestSortedTime = 0;
+        long longestSortedTime = 0;
+        boolean firstRun = true;
 
         // Loop until user enters a valid integer.
         while (!(validInput)) {
@@ -427,12 +455,73 @@ public class Controller {
             ReplaceNameChar();
             index++;
 
-            // Search with new name value.
-            SearchNameUnsorted(true);
-            SearchNameSorted(true);
+            // Linear Search with new name value.
+            startDate = new Date();
+            SearchNameUnsorted(false);
+            elapsedTime = GetTimeLapse(startDate);
+
+            // Get unsorted time data.
+            totalUnsortedTime += elapsedTime;
+            if (firstRun) {
+                shortestUnsortedTime = elapsedTime;
+                longestUnsortedTime = elapsedTime;
+            } else {
+                if (elapsedTime < shortestUnsortedTime) {
+                    shortestUnsortedTime = elapsedTime;
+                }
+                if (elapsedTime > longestUnsortedTime) {
+                    longestUnsortedTime = elapsedTime;
+                }
+            }
+
+
+            // Binary Search with new name value.
+            startDate = new Date();
+            SearchNameSorted(false);
+            elapsedTime = GetTimeLapse(startDate);
+
+            // Get sorted time data.
+            totalSortedTime += elapsedTime;
+            if (firstRun) {
+                shortestSortedTime = elapsedTime;
+                longestSortedTime = elapsedTime;
+            } else {
+                if (elapsedTime < shortestSortedTime) {
+                    shortestSortedTime = elapsedTime;
+                }
+                if (elapsedTime > longestSortedTime) {
+                    longestSortedTime = elapsedTime;
+                }
+            }
+
+            firstRun = false;
         }
 
-        System.out.println("New Name: " + userName);
+
+        averageUnsortedTime = totalUnsortedTime / replaceInt;
+        averageSortedTime = totalSortedTime / replaceInt;
+        System.out.print("Number of replacements: ");
+        computeTime.PrintTime(replaceInt);
+        System.out.println();
+        System.out.print("Average Unsorted Time:  ");
+        computeTime.PrintTime(averageUnsortedTime);
+        System.out.print("Average Sorted Time:    ");
+        computeTime.PrintTime(averageSortedTime);
+        System.out.println();
+        System.out.print("Total Unsorted Time:    ");
+        computeTime.PrintTime(totalUnsortedTime);
+        System.out.print("Total Sorted Time:      ");
+        computeTime.PrintTime(totalSortedTime);
+        System.out.println();
+        System.out.print("Shortest Unsorted Time: ");
+        computeTime.PrintTime(shortestUnsortedTime);
+        System.out.print("Shortest Sorted Time:   ");
+        computeTime.PrintTime(shortestSortedTime);
+        System.out.print("Longest Unsorted Time:  ");
+        computeTime.PrintTime(longestUnsortedTime);
+        System.out.print("Longest Sorted Time:    ");
+        computeTime.PrintTime(longestSortedTime);
+        System.out.println();
     }
 
 
@@ -441,7 +530,7 @@ public class Controller {
      * Pays attention to which indexes have already been replaced.
      */
     private void ReplaceNameChar() {
-        int randomSpot = 0;
+        int randomSpot;
         int randomInt;
         char[] tempChar;
 
@@ -470,7 +559,13 @@ public class Controller {
      * @return String of user's input.
      */
     private String GetUserInput() {
-        return reader.next().trim().toLowerCase();
+        String tempString;
+
+        System.out.print("Enter input: ");
+        tempString = reader.next().trim().toLowerCase();
+        System.out.println();
+
+        return tempString;
     }
 
 
@@ -480,8 +575,13 @@ public class Controller {
      * @return String of user's input.
      */
     private String GetUserInput(String promptString) {
-        System.out.println(promptString);
-        return reader.next().trim().toLowerCase();
+        String tempString;
+
+        System.out.print(promptString);
+        tempString = reader.next().trim().toLowerCase();
+        System.out.println();
+
+        return tempString;
     }
 
 
@@ -580,6 +680,41 @@ public class Controller {
         }
     }
 
+
+    /**
+     * Prints the difference between provided start time and current time.
+     * @param startDate Start time.
+     * @return Long of elapsed time.
+     */
+    private long GetTimeLapse(Date startDate) {
+        long timeResult;
+
+        // Get time lapse of process.
+        timeResult = computeTime.ComputeDateDifference(startDate, (new Date()));
+        //computeTime.PrintTime(timeResult);
+
+        return timeResult;
+    }
+
+
+    /**
+     * Prints the difference between provided start time and current time.
+     * Also outputs additional information for user.
+     * @param startDate Start time.
+     * @param userDisplayString Additional info to show user.
+     * @return Long of elapsed time.
+     */
+    private long GetTimeLapse(Date startDate, String userDisplayString) {
+        long timeResult;
+
+        // Get time lapse of process.
+        System.out.print(userDisplayString);
+        timeResult = computeTime.ComputeDateDifference(startDate, (new Date()));
+        computeTime.PrintTime(timeResult);
+
+        return timeResult;
+    }
+
     //endregion User Interface Methods
 
     //endregion Methods
-- 
GitLab