new semester, new repo structure
This commit is contained in:
Binary file not shown.
14
Semester 2/Assignments/lab-2D-arrays_CalebFontenot/pom.xml
Normal file
14
Semester 2/Assignments/lab-2D-arrays_CalebFontenot/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.calebfontenot</groupId>
|
||||
<artifactId>lab-2D-arrays_CalebFontenot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<exec.mainClass>com.calebfontenot.lab.d.arrays_calebfontenot.Lab2DArrays_CalebFontenot</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
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 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) {
|
||||
if (ar1[i] > ar1[j]) {
|
||||
String temp = ar1[i];
|
||||
ar1[i] = ar1[j];
|
||||
ar1[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user