ASDV-Java/Semester 2/Assignments/lab-2D-arrays_CalebFontenot/lab-2D-arrays_CalebFontenot.html

248 lines
19 KiB
HTML
Raw Normal View History

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}
.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}
.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}
-->
</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>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$</span><span class="comment">{packagePath}/${mainClassName}.java to edit this template</span>
<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 {
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Sorts</span> <span class="comment">the</span> <span class="comment">methods</span> <span class="comment">in</span> <span class="comment">ascending</span> <span class="comment">order</span> <span class="comment">using</span> <span class="comment">Selection</span> <span class="comment">Sort</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1">names</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">sortNames</span>(<span class="literal">char</span>[][] names) {
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
{
<span class="literal">char</span>[][] <span class="comment">names</span> = {
{<span class="string">&#39;</span><span class="string">j</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">o</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">h</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">n</span><span class="string">&#39;</span>},
{<span class="string">&#39;</span><span class="string">a</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">n</span><span class="string">&#39;</span>},
{<span class="string">&#39;</span><span class="string">b</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">y</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">r</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">o</span><span class="string">&#39;</span>, <span class="string">&#39;</span><span class="string">n</span><span class="string">&#39;</span>}
};
<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">&quot;</span><span class="string">john</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">paul</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">james</span><span class="string">&quot;</span>},
{<span class="string">&quot;</span><span class="string">mary</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">laura</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">margaret</span><span class="string">&quot;</span>}
};
<span class="ST3">print</span>(a1);
System.<span class="ST4">out</span>.println();
<span class="ST3">print</span>(a2);
<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>];
<span class="ST3">initializeArray</span>(a3);
System.<span class="ST4">out</span>.println();
<span class="ST3">print</span>(a3);
System.<span class="ST4">out</span>.println();
<span class="literal">int</span>[][] dupDup = <span class="ST3">dup</span>(a1);
<span class="ST3">print</span>(dupDup);
System.<span class="ST4">out</span>.println();
String[][] a4 = {
{<span class="string">&quot;</span><span class="string">ASDV</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">MATH</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">ENGL</span><span class="string">&quot;</span>},
{<span class="string">&quot;</span><span class="string">BIOL</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">CHEM</span><span class="string">&quot;</span>},
{<span class="string">&quot;</span><span class="string">PHYS</span><span class="string">&quot;</span>}
};
<span class="ST3">print</span>(a4);
System.<span class="ST4">out</span>.println();
<span class="ST3">printColumnMajorOrder</span>(a4);
System.<span class="ST4">out</span>.println();
<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>}
};
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="string">Max number of columns in ar5 is </span><span class="string">&quot;</span> + <span class="ST3">maxNumberOfColumnsInJagged2DArray</span>(a5));
System.<span class="ST4">out</span>.println();
<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>
};
<span class="ST3">print</span>(a6);
String[] a7 = {<span class="string">&quot;</span><span class="string">john</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Mary</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Paul</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">nick</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">Peter</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">anna</span><span class="string">&quot;</span>};
System.<span class="ST4">out</span>.println();
<span class="ST3">selectionSort</span>(a7);
<span class="ST3">print</span>(a7);
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">print</span>(<span class="literal">int</span>[][] a1)
{
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; a1.<span class="ST5">length</span>; ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; a1[i].<span class="ST5">length</span>; ++j) {
System.<span class="ST4">out</span>.print(a1[i][j] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST4">out</span>.println();
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">print</span>(<span class="literal">int</span>[][][] a1)
{
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; a1.<span class="ST5">length</span>; ++i) {
System.<span class="ST4">out</span>.println(<span class="string">&quot;</span><span class="literal">\n</span><span class="string"> Table: </span><span class="string">&quot;</span> + (i+<span class="number">1</span>));
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; a1[i].<span class="ST5">length</span>; ++j) {
<span class="literal">for</span> (<span class="literal">int</span> k = <span class="number">0</span>; k &lt; a1[i][j].<span class="ST5">length</span>; ++k) {
System.<span class="ST4">out</span>.print(a1[i][j][k] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST4">out</span>.println();
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">print</span>(String[][] a1)
{
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; a1.<span class="ST5">length</span>; ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; a1[i].<span class="ST5">length</span>; ++j) {
System.<span class="ST4">out</span>.print(a1[i][j] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST4">out</span>.println();
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">print</span>(String[] a1)
{
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; a1.<span class="ST5">length</span>; ++i) {
System.<span class="ST4">out</span>.print(a1[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST4">out</span>.println();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">initializeArray</span>(<span class="literal">int</span>[][] a1)
{
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; a1.<span class="ST5">length</span>; ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; a1[i].<span class="ST5">length</span>; ++j) {
a1[i][j] = (<span class="literal">int</span>) (Math.<span class="ST3">random</span>() * <span class="number">11</span>);
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span>[][] <span class="ST2">dup</span>(<span class="literal">int</span>[][] a1)
{
<span class="literal">int</span>[][] dupArray = <span class="literal">new</span> <span class="literal">int</span>[a1.<span class="ST5">length</span>][a1[<span class="number">0</span>].<span class="ST5">length</span>];
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; a1.<span class="ST5">length</span>; ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; a1[i].<span class="ST5">length</span>; ++j) {
dupArray[i][j] = a1[i][j];
}
}
<span class="literal">return</span> dupArray;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">printColumnMajorOrder</span>(String[][] ar)
{
<span class="literal">int</span> row = <span class="number">0</span>;
<span class="literal">int</span> column = <span class="number">0</span>;
<span class="literal">int</span> max = <span class="ST3">maxNumberOfColumnsInJagged2DArray</span>(ar);
<span class="literal">for</span> (column = <span class="number">0</span>; column &lt; max; ++column) {
<span class="literal">for</span> (row = <span class="number">0</span>; row &lt; ar.<span class="ST5">length</span>; ++row) {
<span class="literal">if</span> (column &lt; ar[row].<span class="ST5">length</span>) {
System.<span class="ST4">out</span>.print(ar[row][column] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
}
System.<span class="ST4">out</span>.println();
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">maxNumberOfColumnsInJagged2DArray</span>(<span class="literal">int</span>[][] ar) {
<span class="literal">int</span> maxNumberOfColumns = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> row = <span class="number">0</span>; row &lt; ar.<span class="ST5">length</span>; ++row) {
<span class="literal">if</span> (ar[row].<span class="ST5">length</span> &gt; maxNumberOfColumns) {
maxNumberOfColumns = ar[row].<span class="ST5">length</span>;
}
}
<span class="literal">return</span> maxNumberOfColumns;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">maxNumberOfColumnsInJagged2DArray</span>(String[][] ar) {
<span class="literal">int</span> maxNumberOfColumns = <span class="number">0</span>;
<span class="literal">for</span> (<span class="literal">int</span> row = <span class="number">0</span>; row &lt; ar.<span class="ST5">length</span>; ++row) {
<span class="literal">if</span> (ar[row].<span class="ST5">length</span> &gt; maxNumberOfColumns) {
maxNumberOfColumns = ar[row].<span class="ST5">length</span>;
}
}
<span class="literal">return</span> maxNumberOfColumns;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">printARow</span>(<span class="literal">int</span>[] rowOf2D) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; rowOf2D.<span class="ST5">length</span>; ++i) {
System.<span class="ST4">out</span>.print(rowOf2D[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
System.<span class="ST4">out</span>.println();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">selectionSort</span>(<span class="literal">int</span>[] ar1) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; ar1.<span class="ST5">length</span> - <span class="number">1</span>; ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j &lt; ar1.<span class="ST5">length</span>; ++j) {
<span class="literal">if</span> (ar1[i] &gt; ar1[j]) {
<span class="literal">int</span> temp = ar1[i];
ar1[i] = ar1[j];
ar1[j] = temp;
}
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">selectionSort</span>(String[] ar1) {
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; ar1.<span class="ST5">length</span> - <span class="number">1</span>; ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j &lt; ar1.<span class="ST5">length</span>; ++j) {
<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>);
<span class="literal">if</span> (Character.<span class="ST3">toLowerCase</span>(compChar1) == Character.<span class="ST3">toLowerCase</span>(compChar2)) {
compChar1 = ar1[i].charAt(<span class="number">1</span>);
compChar2 = ar1[j].charAt(<span class="number">1</span>);
<span class="comment">// Note: I&#39;m not happy with this implementation, but it works as long as you&#39;re only having to compare 1 other character.</span>
}
<span class="literal">if</span> (Character.<span class="ST3">toLowerCase</span>(compChar1) &gt; Character.<span class="ST3">toLowerCase</span>(compChar2)) {
String temp = ar1[i];
ar1[i] = ar1[j];
ar1[j] = temp;
}
}
}
}
}
</pre></body>
</html>