diff --git a/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/lab-2D-arrays-sort_CalebFontenot.html b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/lab-2D-arrays-sort_CalebFontenot.html new file mode 100644 index 0000000..455b44d --- /dev/null +++ b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/lab-2D-arrays-sort_CalebFontenot.html @@ -0,0 +1,117 @@ + + + +Lab2DArraysSort_CalebFontenot.java + + + + +
/home/caleb/ASDV-Java/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java
+
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ */
+package com.calebfontenot.lab.d.arrays.sort_calebfontenot;
+
+/**
+ *
+ * @author caleb
+ */
+public class Lab2DArraysSort_CalebFontenot {
+
+    public static int maxNumberOfColumnsInJagged2dArray(char[][] ar) {
+        int maxNumberOfColumns = 0;
+        for (int row = 0; row < ar.length; ++row) {
+            if (ar[row].length > maxNumberOfColumns) {
+                maxNumberOfColumns = ar[row].length;
+            }
+        }
+
+        return maxNumberOfColumns;
+    }
+
+    public static void printColumnMajorOrder(char[][] ar) {
+        int row = 0;
+        int column = 0;
+        int max = maxNumberOfColumnsInJagged2dArray(ar);
+        for (column = 0; column < max; ++column) {
+            for (row = 0; row < ar.length; ++row) {
+                if (column < ar[row].length) {
+                    System.out.print(ar[row][column] + " ");
+                }
+            }
+            System.out.println("");
+        }
+    }
+
+    /**
+     * Prints row by row
+     *
+     * @param ar
+     */
+    public static void printRowMajorOrder(char[][] ar)//normal
+    {
+        for (int x = 0; x < ar.length; ++x) {
+            for (int y = 0; y < ar[x].length; ++y) {
+                System.out.print(ar[x][y] + "");
+            }
+            System.out.println();
+        }
+
+    }
+
+    /**
+     * Sorts the methods in ascending order using Selection Sort
+     *
+     * @param names the names to be sorted
+     */
+    public static void sortNames(char[][] names) {
+        for (int i = 0; i < names.length - 1; ++i) {
+            for (int j = i + 1; j < names.length; ++j) {
+                char compChar1 = names[i][0], compChar2 = names[j][0];
+                // Reoder entire row
+                for (int rowIterate = 0; rowIterate < maxNumberOfColumnsInJagged2dArray(names); ++rowIterate) {
+                    if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
+                        char[] temp = names[i];
+                        names[i] = names[j];
+                        names[j] = temp;
+                    }
+                }
+            }
+        }
+    }
+    public static void main(String[] args) {
+        char[][] names = {
+            {'j', 'o', 'h', 'n'},
+            {'a', 'n'},
+            {'b', 'y', 'r', 'o', 'n'},};
+        //printColumnMajorOrder(names);
+        printRowMajorOrder(names);
+        System.out.println();
+        sortNames(names);
+        printRowMajorOrder(names);
+        //printColumnMajorOrder(names);
+
+    }
+
+}
+
+
+ diff --git a/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/lab-2D-arrays_CalebFontenot.html b/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/lab-2D-arrays_CalebFontenot.html index deef3ea..73ba1e7 100644 --- a/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/lab-2D-arrays_CalebFontenot.html +++ b/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/lab-2D-arrays_CalebFontenot.html @@ -9,16 +9,10 @@ body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-we pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold} table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold} .literal {color: #cc7832} -.ST3 {font-family: monospace; font-weight: bold; font-style: italic} -.ST0 {color: #287bde} -.string {color: #6a8759} .number {color: #6897bb} -.ST5 {color: #9876aa} -.ST1 {color: #8a653b} +.string {color: #6a8759} .comment {color: #808080} .whitespace {color: #505050} -.ST2 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic} -.ST4 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic} --> @@ -26,8 +20,8 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
/home/caleb/ASDV-Java/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java
 /*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
  */
 package com.calebfontenot.lab.d.arrays_calebfontenot;
 
@@ -36,22 +30,9 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
  * @author caleb
  */
 public class Lab2DArrays_CalebFontenot {
-    
-    /**
-     * Sorts the methods in ascending order using Selection Sort
-     * @param names 
-     */
-    public static void sortNames(char[][] names) {
-        
-    }
 
-    public static void main(String[] args)
+    public static void main(String[] args)
     {
-        char[][] names = {
-        {'j', 'o', 'h', 'n'},
-        {'a', 'n'},
-        {'b', 'y', 'r', 'o', 'n'}
-    };
         int[][] a1 = {
             {1, 2, 3, 4},
             {5, 6, 7, 8},
@@ -62,157 +43,157 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
             {"mary", "laura", "margaret"}
         };
 
-        print(a1);
-        System.out.println();
-        print(a2);
+        print(a1);
+        System.out.println();
+        print(a2);
 
         int a3[][] = new int[5][5];
-        initializeArray(a3);
-        System.out.println();
-        print(a3);
+        initializeArray(a3);
+        System.out.println();
+        print(a3);
 
-        System.out.println();
-        int[][] dupDup = dup(a1);
-        print(dupDup);
+        System.out.println();
+        int[][] dupDup = dup(a1);
+        print(dupDup);
 
-        System.out.println();
+        System.out.println();
         String[][] a4 = {
             {"ASDV", "MATH", "ENGL"},
             {"BIOL", "CHEM"},
             {"PHYS"}
         };
-        print(a4);
+        print(a4);
 
-        System.out.println();
-        printColumnMajorOrder(a4);
+        System.out.println();
+        printColumnMajorOrder(a4);
         
-        System.out.println();
+        System.out.println();
         int[][] a5 = {
             {1},
             {1, 2},   
             {1, 2, 3}
         };
-        System.out.println("Max number of columns in ar5 is " + maxNumberOfColumnsInJagged2DArray(a5));
+        System.out.println("Max number of columns in ar5 is " + maxNumberOfColumnsInJagged2DArray(a5));
 
-        System.out.println();
+        System.out.println();
         int[][][] a6 = {
             { {1, 2}, {3, 4} }, // table 1 NOT jagged 2x2
             {{5, 6}, {-1} }, // table 2 jagged
             {{7, 8 ,9}, {10, 11}, {12, 13, 14, 15}} // table 3 jagged
         };
-        print(a6);
+        print(a6);
 
         String[] a7 = {"john", "Mary", "Paul", "nick", "Peter", "anna"};
         
-        System.out.println();
-        selectionSort(a7);
-        print(a7);
+        System.out.println();
+        selectionSort(a7);
+        print(a7);
     }
 
-    public static void print(int[][] a1)
+    public static void print(int[][] a1)
     {
-        for (int i = 0; i < a1.length; ++i) {
-            for (int j = 0; j < a1[i].length; ++j) {
-                System.out.print(a1[i][j] + " ");
+        for (int i = 0; i < a1.length; ++i) {
+            for (int j = 0; j < a1[i].length; ++j) {
+                System.out.print(a1[i][j] + " ");
             }
-            System.out.println();
+            System.out.println();
         }
     }
-     public static void print(int[][][] a1)
+     public static void print(int[][][] a1)
     {
-        for (int i = 0; i < a1.length; ++i) {
-            System.out.println("\n Table: " + (i+1));
-            for (int j = 0; j < a1[i].length; ++j) {
-                for (int k = 0; k < a1[i][j].length; ++k) {
-                    System.out.print(a1[i][j][k] + " ");
+        for (int i = 0; i < a1.length; ++i) {
+            System.out.println("\n Table: " + (i+1));
+            for (int j = 0; j < a1[i].length; ++j) {
+                for (int k = 0; k < a1[i][j].length; ++k) {
+                    System.out.print(a1[i][j][k] + " ");
                 }
-                System.out.println();
+                System.out.println();
             }
             
         }
     }
 
-    public static void print(String[][] a1)
+    public static void print(String[][] a1)
     {
-        for (int i = 0; i < a1.length; ++i) {
-            for (int j = 0; j < a1[i].length; ++j) {
-                System.out.print(a1[i][j] + " ");
+        for (int i = 0; i < a1.length; ++i) {
+            for (int j = 0; j < a1[i].length; ++j) {
+                System.out.print(a1[i][j] + " ");
             }
-            System.out.println();
+            System.out.println();
         }
     }
     
-    public static void print(String[] a1)
+    public static void print(String[] a1)
     {
-        for (int i = 0; i < a1.length; ++i) {
-                System.out.print(a1[i] + " ");
+        for (int i = 0; i < a1.length; ++i) {
+                System.out.print(a1[i] + " ");
         }
-        System.out.println();
+        System.out.println();
     }
 
-    public static void initializeArray(int[][] a1)
+    public static void initializeArray(int[][] a1)
     {
-        for (int i = 0; i < a1.length; ++i) {
-            for (int j = 0; j < a1[i].length; ++j) {
-                a1[i][j] = (int) (Math.random() * 11);
+        for (int i = 0; i < a1.length; ++i) {
+            for (int j = 0; j < a1[i].length; ++j) {
+                a1[i][j] = (int) (Math.random() * 11);
             }
         }
     }
 
-    public static int[][] dup(int[][] a1)
+    public static int[][] dup(int[][] a1)
     {
-        int[][] dupArray = new int[a1.length][a1[0].length];
+        int[][] dupArray = new int[a1.length][a1[0].length];
 
-        for (int i = 0; i < a1.length; ++i) {
-            for (int j = 0; j < a1[i].length; ++j) {
+        for (int i = 0; i < a1.length; ++i) {
+            for (int j = 0; j < a1[i].length; ++j) {
                 dupArray[i][j] = a1[i][j];
             }
         }
         return dupArray;
     }
 
-    public static void printColumnMajorOrder(String[][] ar)
+    public static void printColumnMajorOrder(String[][] ar)
     {
        int row = 0;
        int column = 0;
-       int max = maxNumberOfColumnsInJagged2DArray(ar);
+       int max = maxNumberOfColumnsInJagged2DArray(ar);
        for (column = 0; column < max; ++column) {
-           for (row = 0; row < ar.length; ++row) {
-               if (column < ar[row].length) {
-                   System.out.print(ar[row][column] + " ");
+           for (row = 0; row < ar.length; ++row) {
+               if (column < ar[row].length) {
+                   System.out.print(ar[row][column] + " ");
                }
            }
-           System.out.println();
+           System.out.println();
        }
     }
     
-    public static int maxNumberOfColumnsInJagged2DArray(int[][] ar) {
+    public static int maxNumberOfColumnsInJagged2DArray(int[][] ar) {
         int maxNumberOfColumns = 0;
-        for (int row = 0; row < ar.length; ++row) {
-            if (ar[row].length > maxNumberOfColumns) {
-                maxNumberOfColumns = ar[row].length;
+        for (int row = 0; row < ar.length; ++row) {
+            if (ar[row].length > maxNumberOfColumns) {
+                maxNumberOfColumns = ar[row].length;
             }
     }
         return maxNumberOfColumns;
     }
-    public static int maxNumberOfColumnsInJagged2DArray(String[][] ar) {
+    public static int maxNumberOfColumnsInJagged2DArray(String[][] ar) {
         int maxNumberOfColumns = 0;
-        for (int row = 0; row < ar.length; ++row) {
-            if (ar[row].length > maxNumberOfColumns) {
-                maxNumberOfColumns = ar[row].length;
+        for (int row = 0; row < ar.length; ++row) {
+            if (ar[row].length > maxNumberOfColumns) {
+                maxNumberOfColumns = ar[row].length;
             }
     }
         return maxNumberOfColumns;
     }
-    public static void printARow(int[] rowOf2D) {
-        for (int i = 0; i < rowOf2D.length; ++i) {
-            System.out.print(rowOf2D[i] + " ");
+    public static void printARow(int[] rowOf2D) {
+        for (int i = 0; i < rowOf2D.length; ++i) {
+            System.out.print(rowOf2D[i] + " ");
         }
-        System.out.println();
+        System.out.println();
     }
-    public static void selectionSort(int[] ar1) {
-        for (int i = 0; i < ar1.length - 1; ++i) {
-            for (int j = i + 1; j < ar1.length; ++j) {
+    public static void selectionSort(int[] ar1) {
+        for (int i = 0; i < ar1.length - 1; ++i) {
+            for (int j = i + 1; j < ar1.length; ++j) {
                 if (ar1[i] > ar1[j]) {
                     int temp = ar1[i];
                     ar1[i] = ar1[j];
@@ -221,18 +202,23 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
             }
         }
     }
-    public static void selectionSort(String[] ar1) {
-        for (int i = 0; i < ar1.length - 1; ++i) {
-            for (int j = i + 1; j < ar1.length; ++j) {
+    public static void selectionSort(String[] ar1) {
+        for (int i = 0; i < ar1.length - 1; ++i) {
+            for (int j = i + 1; j < ar1.length; ++j) {
                 // Compare all chars in string if first chars match
                 char compChar1 = ar1[i].charAt(0), compChar2 = ar1[j].charAt(0);
-                if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
-                    compChar1 = ar1[i].charAt(1);
-                    compChar2 = ar1[j].charAt(1);
-                    
-                    // Note: I'm not happy with this implementation, but it works as long as you're only having to compare 1 other character.
+                for (int index = 1; index < ar1[i].length(); ++index) {
+                if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
+                    try {
+                    compChar1 = ar1[i].charAt(index);
+                    compChar2 = ar1[j].charAt(index);
+                    } catch (Exception ex) {
+                        // If it's failed, the index has gone out of range.
+                        break;
+                    }
                 }
-                if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
+                }
+                if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
                     String temp = ar1[i];
                     ar1[i] = ar1[j];
                     ar1[j] = temp;
diff --git a/Semester 2/ZIPs/lab-2D-arrays-sort_CalebFontenot.zip b/Semester 2/ZIPs/lab-2D-arrays-sort_CalebFontenot.zip
new file mode 100644
index 0000000..e083d61
Binary files /dev/null and b/Semester 2/ZIPs/lab-2D-arrays-sort_CalebFontenot.zip differ
diff --git a/Semester 2/ZIPs/lab-2D-arrays_CalebFontenot.zip b/Semester 2/ZIPs/lab-2D-arrays_CalebFontenot.zip
index 2823dd2..bae61ff 100644
Binary files a/Semester 2/ZIPs/lab-2D-arrays_CalebFontenot.zip and b/Semester 2/ZIPs/lab-2D-arrays_CalebFontenot.zip differ