diff --git a/.gitignore b/.gitignore index a83cd2d..a29df73 100644 --- a/.gitignore +++ b/.gitignore @@ -95,3 +95,4 @@ /Semester 1/Exams/ProgrammingExam1_CalebFontenot/nbproject/private/ /Semester 1/Test Project/target/ /Semester 2/Assignments/lab-2D-arrays_CalebFontenot/target/ +/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/target/ diff --git a/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/pom.xml b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/pom.xml new file mode 100644 index 0000000..ea3b6a8 --- /dev/null +++ b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + com.calebfontenot + lab-2D-arrays-sort_CalebFontenot + 1.0-SNAPSHOT + jar + + UTF-8 + 17 + 17 + com.calebfontenot.lab.d.arrays.sort_calebfontenot.Lab2DArraysSort_CalebFontenot + + \ No newline at end of file diff --git a/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java new file mode 100644 index 0000000..3536c26 --- /dev/null +++ b/Semester 2/Assignments/lab-2D-arrays-sort_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays/sort_calebfontenot/Lab2DArraysSort_CalebFontenot.java @@ -0,0 +1,96 @@ +/* + * 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 arrayDim = 0; arrayDim < names[0].length; ++arrayDim) { + for (int i = 0; i < names[arrayDim].length - 1; ++i) { + for (int j = i + 1; j < names[arrayDim].length; ++j) { + // Compare all chars in string if first chars match + char compChar1 = names[arrayDim][i].charAt(0), compChar2 = names[arrayDim][j].charAt(0); + if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) { + compChar1 = names[arrayDim][i].charAt(1); + compChar2 = names[arrayDim][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. + } + if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) { + String 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'},}; + System.out.println(maxNumberOfColumnsInJagged2dArray(names)); + //printColumnMajorOrder(names); + printRowMajorOrder(names); + sortNames(names); + System.out.println(); + 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 new file mode 100644 index 0000000..deef3ea --- /dev/null +++ b/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/lab-2D-arrays_CalebFontenot.html @@ -0,0 +1,247 @@ + + + +Lab2DArrays_CalebFontenot.java + + + + +
/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
+ */
+package com.calebfontenot.lab.d.arrays_calebfontenot;
+
+/**
+ *
+ * @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)
+    {
+        char[][] names = {
+        {'j', 'o', 'h', 'n'},
+        {'a', 'n'},
+        {'b', 'y', 'r', 'o', 'n'}
+    };
+        int[][] a1 = {
+            {1, 2, 3, 4},
+            {5, 6, 7, 8},
+            {9, 10, 11, 12}
+        };
+        String[][] a2 = {
+            {"john", "paul", "james"},
+            {"mary", "laura", "margaret"}
+        };
+
+        print(a1);
+        System.out.println();
+        print(a2);
+
+        int a3[][] = new int[5][5];
+        initializeArray(a3);
+        System.out.println();
+        print(a3);
+
+        System.out.println();
+        int[][] dupDup = dup(a1);
+        print(dupDup);
+
+        System.out.println();
+        String[][] a4 = {
+            {"ASDV", "MATH", "ENGL"},
+            {"BIOL", "CHEM"},
+            {"PHYS"}
+        };
+        print(a4);
+
+        System.out.println();
+        printColumnMajorOrder(a4);
+        
+        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();
+        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);
+
+        String[] a7 = {"john", "Mary", "Paul", "nick", "Peter", "anna"};
+        
+        System.out.println();
+        selectionSort(a7);
+        print(a7);
+    }
+
+    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] + " ");
+            }
+            System.out.println();
+        }
+    }
+     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] + " ");
+                }
+                System.out.println();
+            }
+            
+        }
+    }
+
+    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] + " ");
+            }
+            System.out.println();
+        }
+    }
+    
+    public static void print(String[] a1)
+    {
+        for (int i = 0; i < a1.length; ++i) {
+                System.out.print(a1[i] + " ");
+        }
+        System.out.println();
+    }
+
+    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);
+            }
+        }
+    }
+
+    public static int[][] dup(int[][] a1)
+    {
+        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) {
+                dupArray[i][j] = a1[i][j];
+            }
+        }
+        return dupArray;
+    }
+
+    public static void printColumnMajorOrder(String[][] 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();
+       }
+    }
+    
+    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;
+            }
+    }
+        return maxNumberOfColumns;
+    }
+    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;
+            }
+    }
+        return maxNumberOfColumns;
+    }
+    public static void printARow(int[] rowOf2D) {
+        for (int i = 0; i < rowOf2D.length; ++i) {
+            System.out.print(rowOf2D[i] + " ");
+        }
+        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) {
+                if (ar1[i] > ar1[j]) {
+                    int temp = ar1[i];
+                    ar1[i] = ar1[j];
+                    ar1[j] = temp;
+                }
+            }
+        }
+    }
+    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.
+                }
+                if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
+                    String temp = ar1[i];
+                    ar1[i] = ar1[j];
+                    ar1[j] = temp;
+                }
+            }
+        }
+    }
+    
+}
+
+
+ diff --git a/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java b/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java index 8a06101..fdc8790 100644 --- a/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java +++ b/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java @@ -9,22 +9,9 @@ package com.calebfontenot.lab.d.arrays_calebfontenot; * @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) { - char[][] names = { - {'j', 'o', 'h', 'n'}, - {'a', 'n'}, - {'b', 'y', 'r', 'o', 'n'} - }; int[][] a1 = { {1, 2, 3, 4}, {5, 6, 7, 8}, @@ -74,6 +61,12 @@ public class Lab2DArrays_CalebFontenot { {{7, 8 ,9}, {10, 11}, {12, 13, 14, 15}} // table 3 jagged }; print(a6); + + String[] a7 = {"john", "Mary", "Paul", "nick", "Peter", "anna"}; + + System.out.println(); + selectionSort(a7); + print(a7); } public static void print(int[][] a1) @@ -108,6 +101,14 @@ public class Lab2DArrays_CalebFontenot { System.out.println(); } } + + public static void print(String[] a1) + { + for (int i = 0; i < a1.length; ++i) { + System.out.print(a1[i] + " "); + } + System.out.println(); + } public static void initializeArray(int[][] a1) { @@ -183,7 +184,15 @@ public class Lab2DArrays_CalebFontenot { public static void selectionSort(String[] ar1) { for (int i = 0; i < ar1.length - 1; ++i) { for (int j = i + 1; j < ar1.length; ++j) { - if (ar1[i] > ar1[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. + } + 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_CalebFontenot.zip b/Semester 2/ZIPs/lab-2D-arrays_CalebFontenot.zip new file mode 100644 index 0000000..2823dd2 Binary files /dev/null and b/Semester 2/ZIPs/lab-2D-arrays_CalebFontenot.zip differ