2023-01-11 06:46:50 -06:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
< html >
< head >
< title > Lab2DArrays_CalebFontenot.java< / title >
< meta http-equiv = "content-type" content = "text/html; charset=UTF-8" >
< style type = "text/css" >
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
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}
.number {color: #6897bb}
2023-01-12 09:09:48 -06:00
.string {color: #6a8759}
2023-01-11 06:46:50 -06:00
.comment {color: #808080}
.whitespace {color: #505050}
-->
< / style >
< / head >
< body >
< table width = "100%" > < tr > < td align = "center" > /home/caleb/ASDV-Java/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/src/main/java/com/calebfontenot/lab/d/arrays_calebfontenot/Lab2DArrays_CalebFontenot.java< / td > < / tr > < / table >
< pre >
< span class = "comment" > /*< / span >
2023-01-12 09:09:48 -06:00
< span class = "comment" > * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license< / span >
< span class = "comment" > * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template< / span >
2023-01-11 06:46:50 -06:00
< span class = "comment" > */< / span >
< span class = "literal" > package< / span > com.calebfontenot.lab.d.arrays_calebfontenot;
< span class = "comment" > /**< / span >
< span class = "comment" > *< / span >
< span class = "comment" > * < / span > < span class = "comment" > @author< / span > < span class = "comment" > caleb< / span >
< span class = "comment" > */< / span >
< span class = "literal" > public< / span > < span class = "literal" > class< / span > Lab2DArrays_CalebFontenot {
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > main(String[] args)
2023-01-11 06:46:50 -06:00
{
< span class = "literal" > int< / span > [][] a1 = {
{< span class = "number" > 1< / span > , < span class = "number" > 2< / span > , < span class = "number" > 3< / span > , < span class = "number" > 4< / span > },
{< span class = "number" > 5< / span > , < span class = "number" > 6< / span > , < span class = "number" > 7< / span > , < span class = "number" > 8< / span > },
{< span class = "number" > 9< / span > , < span class = "number" > 10< / span > , < span class = "number" > 11< / span > , < span class = "number" > 12< / span > }
};
String[][] a2 = {
{< span class = "string" > " < / span > < span class = "string" > john< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > paul< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > james< / span > < span class = "string" > " < / span > },
{< span class = "string" > " < / span > < span class = "string" > mary< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > laura< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > margaret< / span > < span class = "string" > " < / span > }
};
2023-01-12 09:09:48 -06:00
print(a1);
System.out.println();
print(a2);
2023-01-11 06:46:50 -06:00
< span class = "literal" > int< / span > a3[][] = < span class = "literal" > new< / span > < span class = "literal" > int< / span > [< span class = "number" > 5< / span > ][< span class = "number" > 5< / span > ];
2023-01-12 09:09:48 -06:00
initializeArray(a3);
System.out.println();
print(a3);
2023-01-11 06:46:50 -06:00
2023-01-12 09:09:48 -06:00
System.out.println();
< span class = "literal" > int< / span > [][] dupDup = dup(a1);
print(dupDup);
2023-01-11 06:46:50 -06:00
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
String[][] a4 = {
{< span class = "string" > " < / span > < span class = "string" > ASDV< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > MATH< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > ENGL< / span > < span class = "string" > " < / span > },
{< span class = "string" > " < / span > < span class = "string" > BIOL< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > CHEM< / span > < span class = "string" > " < / span > },
{< span class = "string" > " < / span > < span class = "string" > PHYS< / span > < span class = "string" > " < / span > }
};
2023-01-12 09:09:48 -06:00
print(a4);
2023-01-11 06:46:50 -06:00
2023-01-12 09:09:48 -06:00
System.out.println();
printColumnMajorOrder(a4);
2023-01-11 06:46:50 -06:00
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
< span class = "literal" > int< / span > [][] a5 = {
{< span class = "number" > 1< / span > },
{< span class = "number" > 1< / span > , < span class = "number" > 2< / span > },
{< span class = "number" > 1< / span > , < span class = "number" > 2< / span > , < span class = "number" > 3< / span > }
};
2023-01-12 09:09:48 -06:00
System.out.println(< span class = "string" > " < / span > < span class = "string" > Max number of columns in ar5 is < / span > < span class = "string" > " < / span > + maxNumberOfColumnsInJagged2DArray(a5));
2023-01-11 06:46:50 -06:00
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
< span class = "literal" > int< / span > [][][] a6 = {
{ {< span class = "number" > 1< / span > , < span class = "number" > 2< / span > }, {< span class = "number" > 3< / span > , < span class = "number" > 4< / span > } }, < span class = "comment" > // table 1 NOT jagged 2x2< / span >
{{< span class = "number" > 5< / span > , < span class = "number" > 6< / span > }, {-< span class = "number" > 1< / span > } }, < span class = "comment" > // table 2 jagged< / span >
{{< span class = "number" > 7< / span > , < span class = "number" > 8< / span > ,< span class = "number" > 9< / span > }, {< span class = "number" > 10< / span > , < span class = "number" > 11< / span > }, {< span class = "number" > 12< / span > , < span class = "number" > 13< / span > , < span class = "number" > 14< / span > , < span class = "number" > 15< / span > }} < span class = "comment" > // table 3 jagged< / span >
};
2023-01-12 09:09:48 -06:00
print(a6);
2023-01-11 06:46:50 -06:00
String[] a7 = {< span class = "string" > " < / span > < span class = "string" > john< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > Mary< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > Paul< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > nick< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > Peter< / span > < span class = "string" > " < / span > , < span class = "string" > " < / span > < span class = "string" > anna< / span > < span class = "string" > " < / span > };
2023-01-12 09:09:48 -06:00
System.out.println();
selectionSort(a7);
print(a7);
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > print(< span class = "literal" > int< / span > [][] a1)
2023-01-11 06:46:50 -06:00
{
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < a1.length; ++i) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = < span class = "number" > 0< / span > ; j < a1[i].length; ++j) {
System.out.print(a1[i][j] + < span class = "string" > " < / span > < span class = "string" > " < / span > );
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
}
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > print(< span class = "literal" > int< / span > [][][] a1)
2023-01-11 06:46:50 -06:00
{
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < a1.length; ++i) {
System.out.println(< span class = "string" > " < / span > < span class = "literal" > \n< / span > < span class = "string" > Table: < / span > < span class = "string" > " < / span > + (i+< span class = "number" > 1< / span > ));
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = < span class = "number" > 0< / span > ; j < a1[i].length; ++j) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > k = < span class = "number" > 0< / span > ; k < a1[i][j].length; ++k) {
System.out.print(a1[i][j][k] + < span class = "string" > " < / span > < span class = "string" > " < / span > );
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
}
}
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > print(String[][] a1)
2023-01-11 06:46:50 -06:00
{
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < a1.length; ++i) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = < span class = "number" > 0< / span > ; j < a1[i].length; ++j) {
System.out.print(a1[i][j] + < span class = "string" > " < / span > < span class = "string" > " < / span > );
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
}
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > print(String[] a1)
2023-01-11 06:46:50 -06:00
{
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < a1.length; ++i) {
System.out.print(a1[i] + < span class = "string" > " < / span > < span class = "string" > " < / span > );
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > initializeArray(< span class = "literal" > int< / span > [][] a1)
2023-01-11 06:46:50 -06:00
{
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < a1.length; ++i) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = < span class = "number" > 0< / span > ; j < a1[i].length; ++j) {
a1[i][j] = (< span class = "literal" > int< / span > ) (Math.random() * < span class = "number" > 11< / span > );
2023-01-11 06:46:50 -06:00
}
}
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > int< / span > [][] dup(< span class = "literal" > int< / span > [][] a1)
2023-01-11 06:46:50 -06:00
{
2023-01-12 09:09:48 -06:00
< span class = "literal" > int< / span > [][] dupArray = < span class = "literal" > new< / span > < span class = "literal" > int< / span > [a1.length][a1[< span class = "number" > 0< / span > ].length];
2023-01-11 06:46:50 -06:00
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < a1.length; ++i) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = < span class = "number" > 0< / span > ; j < a1[i].length; ++j) {
2023-01-11 06:46:50 -06:00
dupArray[i][j] = a1[i][j];
}
}
< span class = "literal" > return< / span > dupArray;
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > printColumnMajorOrder(String[][] ar)
2023-01-11 06:46:50 -06:00
{
< span class = "literal" > int< / span > row = < span class = "number" > 0< / span > ;
< span class = "literal" > int< / span > column = < span class = "number" > 0< / span > ;
2023-01-12 09:09:48 -06:00
< span class = "literal" > int< / span > max = maxNumberOfColumnsInJagged2DArray(ar);
2023-01-11 06:46:50 -06:00
< span class = "literal" > for< / span > (column = < span class = "number" > 0< / span > ; column < max; ++column) {
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (row = < span class = "number" > 0< / span > ; row < ar.length; ++row) {
< span class = "literal" > if< / span > (column < ar[row].length) {
System.out.print(ar[row][column] + < span class = "string" > " < / span > < span class = "string" > " < / span > );
2023-01-11 06:46:50 -06:00
}
}
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
}
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > int< / span > maxNumberOfColumnsInJagged2DArray(< span class = "literal" > int< / span > [][] ar) {
2023-01-11 06:46:50 -06:00
< span class = "literal" > int< / span > maxNumberOfColumns = < span class = "number" > 0< / span > ;
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > row = < span class = "number" > 0< / span > ; row < ar.length; ++row) {
< span class = "literal" > if< / span > (ar[row].length > maxNumberOfColumns) {
maxNumberOfColumns = ar[row].length;
2023-01-11 06:46:50 -06:00
}
}
< span class = "literal" > return< / span > maxNumberOfColumns;
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > int< / span > maxNumberOfColumnsInJagged2DArray(String[][] ar) {
2023-01-11 06:46:50 -06:00
< span class = "literal" > int< / span > maxNumberOfColumns = < span class = "number" > 0< / span > ;
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > row = < span class = "number" > 0< / span > ; row < ar.length; ++row) {
< span class = "literal" > if< / span > (ar[row].length > maxNumberOfColumns) {
maxNumberOfColumns = ar[row].length;
2023-01-11 06:46:50 -06:00
}
}
< span class = "literal" > return< / span > maxNumberOfColumns;
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > printARow(< span class = "literal" > int< / span > [] rowOf2D) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < rowOf2D.length; ++i) {
System.out.print(rowOf2D[i] + < span class = "string" > " < / span > < span class = "string" > " < / span > );
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
System.out.println();
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > selectionSort(< span class = "literal" > int< / span > [] ar1) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < ar1.length - < span class = "number" > 1< / span > ; ++i) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = i + < span class = "number" > 1< / span > ; j < ar1.length; ++j) {
2023-01-11 06:46:50 -06:00
< span class = "literal" > if< / span > (ar1[i] > ar1[j]) {
< span class = "literal" > int< / span > temp = ar1[i];
ar1[i] = ar1[j];
ar1[j] = temp;
}
}
}
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > public< / span > < span class = "literal" > static< / span > < span class = "literal" > void< / span > selectionSort(String[] ar1) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > i = < span class = "number" > 0< / span > ; i < ar1.length - < span class = "number" > 1< / span > ; ++i) {
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > j = i + < span class = "number" > 1< / span > ; j < ar1.length; ++j) {
2023-01-11 06:46:50 -06:00
< span class = "comment" > // Compare all chars in string if first chars match< / span >
< span class = "literal" > char< / span > compChar1 = ar1[i].charAt(< span class = "number" > 0< / span > ), compChar2 = ar1[j].charAt(< span class = "number" > 0< / span > );
2023-01-12 09:09:48 -06:00
< span class = "literal" > for< / span > (< span class = "literal" > int< / span > index = < span class = "number" > 1< / span > ; index < ar1[i].length(); ++index) {
< span class = "literal" > if< / span > (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
< span class = "literal" > try< / span > {
compChar1 = ar1[i].charAt(index);
compChar2 = ar1[j].charAt(index);
} < span class = "literal" > catch< / span > (Exception ex) {
< span class = "comment" > // If it' s failed, the index has gone out of range.< / span >
< span class = "literal" > break< / span > ;
}
}
2023-01-11 06:46:50 -06:00
}
2023-01-12 09:09:48 -06:00
< span class = "literal" > if< / span > (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
2023-01-11 06:46:50 -06:00
String temp = ar1[i];
ar1[i] = ar1[j];
ar1[j] = temp;
}
}
}
}
}
< / pre > < / body >
< / html >