Mason Help
This commit is contained in:
parent
c233baa498
commit
fe3eb30a9c
1
.gitignore
vendored
1
.gitignore
vendored
@ -100,3 +100,4 @@
|
||||
/Semester 2/Assignments/MP1_CalebFontenot/target/
|
||||
/Semester 2/Assignments/MP2_CalebFontenot/target/
|
||||
/Semester 2/Assignments/lab2_CalebFontenot/target/
|
||||
/ASDV-Help/Mason/target/
|
||||
|
14
ASDV-Help/Mason/pom.xml
Normal file
14
ASDV-Help/Mason/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>Mason</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.mason.Mason</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
211
ASDV-Help/Mason/src/main/java/com/calebfontenot/mason/Mason.java
Normal file
211
ASDV-Help/Mason/src/main/java/com/calebfontenot/mason/Mason.java
Normal file
@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.calebfontenot.mason;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Mason {
|
||||
|
||||
public static void sortNames(char[][] names)
|
||||
{
|
||||
|
||||
for (int i = 0; i < names.length - 1; ++i)
|
||||
{
|
||||
|
||||
for (int j = i + 1; j < names.length; ++j)
|
||||
{
|
||||
//>compare character by character names[i] to names[j]
|
||||
for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
|
||||
//index2 to traverse names[j]
|
||||
index1 < names[i].length
|
||||
&& index2 < names[j].length;
|
||||
++index1, ++index2)
|
||||
{
|
||||
|
||||
//>>SWAP name[i] with names[j]
|
||||
//if names[i] > names[j] OR the name at i has more chars after the tie
|
||||
// ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
|
||||
if (names[i][index1] > names[j][index2]
|
||||
|| (index2 == names[j].length - 1 && index1 < names[i].length - 1))
|
||||
{
|
||||
//>> copy to tempI the names[i]
|
||||
char[] temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
break;///you must break , no need to continue comparing characters at index1, index2
|
||||
}
|
||||
//>>if the characters are equal, continue
|
||||
if (names[i][index1] == names[j][index2])
|
||||
{
|
||||
continue;//do nothing, but continue
|
||||
} else //name at i is smaller than name
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void sortNames(char[][][] tables)
|
||||
{
|
||||
for (int numTable = 0; numTable < tables.length; ++numTable)
|
||||
{
|
||||
for (int i = 0; i < tables[numTable].length - 1; ++i)
|
||||
{
|
||||
|
||||
for (int j = i + 1; j < tables[numTable].length; ++j)
|
||||
{
|
||||
//>compare character by character names[i] to names[j]
|
||||
for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
|
||||
//index2 to traverse names[j]
|
||||
index1 < tables[i][i].length
|
||||
&& index2 < tables[j][i].length;
|
||||
++index1, ++index2)
|
||||
{
|
||||
|
||||
//>>SWAP name[i] with names[j]
|
||||
//if names[i] > names[j] OR the name at i has more chars after the tie
|
||||
// ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
|
||||
if (tables[numTable][i][index1] > tables[numTable][j][index2]
|
||||
|| (index2 == tables[numTable][j].length - 1 && index1 < tables[numTable][i].length - 1))
|
||||
{
|
||||
//>> copy to tempI the names[i]
|
||||
char[][] temp = tables[i];
|
||||
tables[i] = tables[j];
|
||||
tables[j] = temp;
|
||||
break;///you must break , no need to continue comparing characters at index1, index2
|
||||
}
|
||||
//>>if the characters are equal, continue
|
||||
if (tables[i][index1] == tables[j][index2])
|
||||
{
|
||||
continue;//do nothing, but continue
|
||||
} else //name at i is smaller than name
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints row by row
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void printRowMajorOrder(char[][] ar)
|
||||
{
|
||||
for (int i = 0; i < ar.length; ++i)
|
||||
{
|
||||
for (int j = 0; j < ar[i].length; ++j)
|
||||
{
|
||||
System.out.print(ar[i][j]);
|
||||
}
|
||||
System.out.print(" ");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void printRowMajorOrder(char[][][] tables)
|
||||
{
|
||||
|
||||
for (int numTable = 0; numTable < tables.length; ++numTable)
|
||||
{
|
||||
for (int i = 0; i < tables[numTable].length; ++i)
|
||||
{
|
||||
for (int j = 0; j < tables[numTable][i].length; ++j)
|
||||
{
|
||||
System.out.print(tables[numTable][i][j]);
|
||||
|
||||
}
|
||||
System.out.print(" ");
|
||||
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
char[][] names =
|
||||
{
|
||||
{
|
||||
'j', 'o', 'h', 'n'
|
||||
},
|
||||
{
|
||||
'a', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n', 'i'
|
||||
},
|
||||
{
|
||||
'a', 'a', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'b'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'c'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b'
|
||||
}
|
||||
};
|
||||
|
||||
char[][][] tables =
|
||||
{
|
||||
{
|
||||
{
|
||||
'j', 'o', 'h', 'n'
|
||||
},
|
||||
{
|
||||
'a', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n', 'i'
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
'a', 'a', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'b'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'c'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b'
|
||||
}
|
||||
}
|
||||
};
|
||||
// System.out.println("ORIGINAL LIST");
|
||||
// printRowMajorOrder(names);
|
||||
//
|
||||
// System.out.println("\n\nSORTED LIST");
|
||||
// sortNames(names);
|
||||
// printRowMajorOrder(names);
|
||||
|
||||
System.out.println("ORIGINAL LIST");
|
||||
printRowMajorOrder(tables);
|
||||
System.out.println("\n\nSORTED LIST");
|
||||
sortNames(tables);
|
||||
printRowMajorOrder(tables);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.mason;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Mason_2 {
|
||||
|
||||
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("");
|
||||
}
|
||||
}
|
||||
|
||||
public static void sortNames(char[][][] names) {
|
||||
for (int numTable = 0; numTable < names.length; ++numTable) {
|
||||
for (int i = 0; i < names[numTable].length - 1; ++i) {
|
||||
|
||||
for (int j = i + 1; j < names[numTable].length; ++j) {
|
||||
//>compare character by character names[i] to names[j]
|
||||
for (int index1 = 0, index2 = 0;//index1 to traverse names[i]
|
||||
//index2 to traverse names[j]
|
||||
index1 < names[numTable][i].length
|
||||
&& index2 < names[numTable][j].length;
|
||||
++index1, ++index2) {
|
||||
|
||||
//>>SWAP name[i] with names[j]
|
||||
//if names[i] > names[j] OR the name at i has more chars after the tie
|
||||
// ( i.e. if "bbbc" at names[i] and "bbb" at names[j] at j then we swap them )
|
||||
if (names[numTable][i][index1] > names[numTable][j][index2]
|
||||
|| (index2 == names[numTable][j].length - 1 && index1 < names[numTable][i].length - 1)) {
|
||||
//>> copy to tempI the names[i]
|
||||
char[] temp = names[numTable][i];
|
||||
names[numTable][i] = names[numTable][j];
|
||||
names[numTable][j] = temp;
|
||||
break;///you must break , no need to continue comparing characters at index1, index2
|
||||
}
|
||||
//>>if the characters are equal, continue
|
||||
if (names[numTable][i][index1] == names[numTable][j][index2]) {
|
||||
continue;//do nothing, but continue
|
||||
} else //name at i is smaller than name
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints row by row
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void printRowMajorOrder(char[][][] array) {
|
||||
for (char[][] i : array) {
|
||||
System.out.println("---------------------------------");
|
||||
printRowMajorOrder(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static void printRowMajorOrder(char[][] ar) {
|
||||
for (int i = 0; i < ar.length; ++i) {
|
||||
for (int j = 0; j < ar[i].length; ++j) {
|
||||
System.out.print(ar[i][j]);
|
||||
}
|
||||
System.out.print(" ");
|
||||
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
char[][] names
|
||||
= {
|
||||
{'j', 'o', 'h', 'n'}, {'a', 'n'}, {'b', 'y', 'r', 'o', 'n'},
|
||||
{'b', 'y', 'r', 'o', 'n', 'i'}, {'a', 'a', 'o', 'n'},
|
||||
{'b', 'b', 'b', 'b'}, {'b', 'b', 'b', 'c'}, {'b', 'b', 'b'}
|
||||
};
|
||||
char[][][] tables
|
||||
= {
|
||||
{
|
||||
{
|
||||
'j', 'o', 'h', 'n'
|
||||
},
|
||||
{
|
||||
'a', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'y', 'r', 'o', 'n', 'i'
|
||||
}
|
||||
},
|
||||
{
|
||||
{
|
||||
'a', 'a', 'o', 'n'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'b'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b', 'c'
|
||||
},
|
||||
{
|
||||
'b', 'b', 'b'
|
||||
}
|
||||
}
|
||||
};
|
||||
System.out.println("ORIGINAL LIST");
|
||||
printRowMajorOrder(tables);
|
||||
|
||||
System.out.println("\n\nSORTED LIST");
|
||||
sortNames(tables);
|
||||
printRowMajorOrder(tables);
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
,caleb,caleb-ryzen-archlinux,14.01.2023 14:36,file:///home/caleb/.config/libreoffice/4;
|
@ -0,0 +1,179 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Lab3DArraysSort_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}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.ST2 {color: #9876aa}
|
||||
.ST5 {color: #8a653b}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST1 {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/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/Lab3DArraysSort_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"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.mp1_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> Lab3DArraysSort_CalebFontenot {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST1">maxNumberOfColumnsInJagged2dArray</span>(<span class="literal">char</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 < ar.<span class="ST2">length</span>; ++row) {
|
||||
<span class="literal">if</span> (ar[row].<span class="ST2">length</span> > maxNumberOfColumns) {
|
||||
maxNumberOfColumns = ar[row].<span class="ST2">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="ST1">printColumnMajorOrder</span>(<span class="literal">char</span>[][] 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 < max; ++column) {
|
||||
<span class="literal">for</span> (row = <span class="number">0</span>; row < ar.<span class="ST2">length</span>; ++row) {
|
||||
<span class="literal">if</span> (column < ar[row].<span class="ST2">length</span>) {
|
||||
System.<span class="ST4">out</span>.print(ar[row][column] + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Prints</span> <span class="comment">row</span> <span class="comment">by</span> <span class="comment">row</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST5">ar</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printRowMajorOrder</span>(<span class="literal">char</span>[][][] array) {
|
||||
<span class="literal">for</span> (<span class="literal">char</span>[][] i: array) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">---------------------------------</span><span class="string">"</span>);
|
||||
<span class="ST3">printRowMajorOrder</span>(i);
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printRowMajorOrder</span>(<span class="literal">char</span>[][] ar)<span class="comment">//normal</span>
|
||||
{
|
||||
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < ar.<span class="ST2">length</span>; ++x) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> y = <span class="number">0</span>; y < ar[x].<span class="ST2">length</span>; ++y) {
|
||||
System.<span class="ST4">out</span>.print(ar[x][y] + <span class="string">""</span>);
|
||||
}
|
||||
System.<span class="ST4">out</span>.println();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">returnRowMajorOrder</span>(<span class="literal">char</span>[] ar)<span class="comment">//normal</span>
|
||||
{
|
||||
String returnString = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < ar.<span class="ST2">length</span>; ++x) {
|
||||
returnString += ar[x];
|
||||
}
|
||||
<span class="literal">return</span> returnString;
|
||||
}
|
||||
|
||||
<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"> * </span><span class="comment">@param</span> <span class="ST5">names</span> <span class="comment">the</span> <span class="comment">names</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">sorted</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">sortNames</span>(<span class="literal">char</span>[][][] names) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> array = <span class="number">0</span>; array < names.<span class="ST2">length</span>; ++array) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">times looped: </span><span class="string">"</span> + array);
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < names[array].<span class="ST2">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 < names[array].<span class="ST2">length</span>; ++j) {
|
||||
<span class="literal">char</span> compChar1 = names[array][i][<span class="number">0</span>], compChar2 = names[array][j][<span class="number">0</span>];
|
||||
<span class="comment">// Reoder entire row</span>
|
||||
<span class="literal">for</span> (<span class="literal">int</span> rowIterate = <span class="number">1</span>; rowIterate < <span class="ST3">maxNumberOfColumnsInJagged2dArray</span>(names[array]); ++rowIterate) {
|
||||
<span class="literal">if</span> (Character.<span class="ST3">toLowerCase</span>(compChar1) == Character.<span class="ST3">toLowerCase</span>(compChar2)) {
|
||||
<span class="literal">try</span> {
|
||||
compChar1 = names[array][i][rowIterate];
|
||||
compChar2 = names[array][j][rowIterate];
|
||||
} <span class="literal">catch</span> (Exception ex) {
|
||||
<span class="comment">// If it's failed, the index has gone out of range.</span>
|
||||
<span class="comment">// Check the length of the arrays and swap the larger one with the smaller one.</span>
|
||||
<span class="literal">if</span> (names[array][i].<span class="ST2">length</span> > names[array][j].<span class="ST2">length</span>) {
|
||||
System.<span class="ST4">out</span>.println(names[array][i].<span class="ST2">length</span> + <span class="string">"</span> <span class="string">"</span> + names[array][j].<span class="ST2">length</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Swapping </span><span class="string">"</span> + <span class="ST3">returnRowMajorOrder</span>(names[array][i]) + <span class="string">"</span><span class="string"> with </span><span class="string">"</span> + <span class="ST3">returnRowMajorOrder</span>(names[array][j]));
|
||||
<span class="literal">char</span>[] temp = names[array][i];
|
||||
names[array][i] = names[array][j];
|
||||
names[array][j] = temp;
|
||||
}
|
||||
<span class="literal">break</span>;
|
||||
}
|
||||
|
||||
}
|
||||
<span class="literal">if</span> (Character.<span class="ST3">toLowerCase</span>(compChar1) > Character.<span class="ST3">toLowerCase</span>(compChar2)) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Swapping </span><span class="string">"</span> + <span class="ST3">returnRowMajorOrder</span>(names[array][i]) + <span class="string">"</span><span class="string"> with </span><span class="string">"</span> + <span class="ST3">returnRowMajorOrder</span>(names[array][j]));
|
||||
<span class="literal">char</span>[] temp = names[array][i];
|
||||
names[array][i] = names[array][j];
|
||||
names[array][j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
||||
<span class="comment">/*</span>
|
||||
<span class="comment">char[][] names = {</span>
|
||||
<span class="comment"> {'j', 'o', 'h', 'n'},</span>
|
||||
<span class="comment"> {'a', 'n'},</span>
|
||||
<span class="comment"> {'b', 'y', 'r', 'o', 'n'},};</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="comment">///*</span>
|
||||
<span class="literal">char</span>[][][] names = {
|
||||
{
|
||||
{<span class="string">'</span><span class="string">j</span><span class="string">'</span>, <span class="string">'</span><span class="string">o</span><span class="string">'</span>, <span class="string">'</span><span class="string">h</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">y</span><span class="string">'</span>, <span class="string">'</span><span class="string">r</span><span class="string">'</span>, <span class="string">'</span><span class="string">o</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">y</span><span class="string">'</span>, <span class="string">'</span><span class="string">r</span><span class="string">'</span>, <span class="string">'</span><span class="string">o</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>, <span class="string">'</span><span class="string">i</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">o</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">c</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>}
|
||||
},
|
||||
{
|
||||
{<span class="string">'</span><span class="string">L</span><span class="string">'</span>, <span class="string">'</span><span class="string">i</span><span class="string">'</span>, <span class="string">'</span><span class="string">s</span><span class="string">'</span>, <span class="string">'</span><span class="string">s</span><span class="string">'</span>, <span class="string">'</span><span class="string">e</span><span class="string">'</span>, <span class="string">'</span><span class="string">t</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">E</span><span class="string">'</span>, <span class="string">'</span><span class="string">t</span><span class="string">'</span>, <span class="string">'</span><span class="string">h</span><span class="string">'</span>, <span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">C</span><span class="string">'</span>, <span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">l</span><span class="string">'</span>, <span class="string">'</span><span class="string">e</span><span class="string">'</span>, <span class="string">'</span><span class="string">b</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">L</span><span class="string">'</span>, <span class="string">'</span><span class="string">u</span><span class="string">'</span>, <span class="string">'</span><span class="string">l</span><span class="string">'</span>, <span class="string">'</span><span class="string">y</span><span class="string">'</span>},
|
||||
{<span class="string">'</span><span class="string">C</span><span class="string">'</span>, <span class="string">'</span><span class="string">h</span><span class="string">'</span>, <span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">n</span><span class="string">'</span>, <span class="string">'</span><span class="string">c</span><span class="string">'</span>, <span class="string">'</span><span class="string">e</span><span class="string">'</span>}
|
||||
}
|
||||
};
|
||||
<span class="comment">//*/</span>
|
||||
<span class="comment">//printColumnMajorOrder(names);</span>
|
||||
<span class="ST3">printRowMajorOrder</span>(names);
|
||||
System.<span class="ST4">out</span>.println();
|
||||
<span class="ST3">sortNames</span>(names);
|
||||
System.<span class="ST4">out</span>.println();
|
||||
<span class="ST3">printRowMajorOrder</span>(names);
|
||||
<span class="comment">//printColumnMajorOrder(names);</span>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
296
Semester 2/Assignments/MP1_CalebFontenot/MP1_CalebFontenot.html
Normal file
296
Semester 2/Assignments/MP1_CalebFontenot/MP1_CalebFontenot.html
Normal file
@ -0,0 +1,296 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>MP1_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}
|
||||
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.ST3 {color: #9876aa}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST2 {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/MP1_CalebFontenot/src/main/java/com/calebfontenot/mp1_calebfontenot/MP1_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"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.mp1_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Scanner;
|
||||
|
||||
<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> MP1_CalebFontenot {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span>[][] <span class="ST1">inputArray</span>() {
|
||||
<span class="literal">int</span> m, n, i, j;
|
||||
<span class="comment">// Create scanner</span>
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter the number of rows: </span><span class="string">"</span>);
|
||||
<span class="comment">//taking row as input </span>
|
||||
m = input.nextInt();
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter the number of columns: </span><span class="string">"</span>);
|
||||
<span class="comment">//taking column as input </span>
|
||||
n = input.nextInt();
|
||||
<span class="comment">// Declaring the two-dimensional matrix </span>
|
||||
<span class="literal">int</span> returnArray[][] = <span class="literal">new</span> <span class="literal">int</span>[m][n];
|
||||
<span class="comment">// Read the matrix values </span>
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Enter the elements of the array: </span><span class="string">"</span>);
|
||||
<span class="comment">//loop for row </span>
|
||||
<span class="literal">for</span> (i = <span class="number">0</span>; i < m; i++) <span class="comment">//inner for loop for column </span>
|
||||
{
|
||||
<span class="literal">for</span> (j = <span class="number">0</span>; j < n; j++) {
|
||||
returnArray[i][j] = input.nextInt();
|
||||
}
|
||||
}
|
||||
<span class="comment">//accessing array elements </span>
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Elements of the array are: </span><span class="string">"</span>);
|
||||
<span class="literal">for</span> (i = <span class="number">0</span>; i < m; i++) {
|
||||
<span class="literal">for</span> (j = <span class="number">0</span>; j < n; j++) {
|
||||
<span class="literal">return</span> returnArray;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">null</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">isConsecutiveFour</span>(<span class="literal">int</span>[] values) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < values.<span class="ST3">length</span> - <span class="number">4</span>; ++i) {
|
||||
<span class="literal">if</span> (values[i] == values[i + <span class="number">1</span>]
|
||||
&& values[i + <span class="number">1</span>] == values[i + <span class="number">2</span>]
|
||||
&& values[i + <span class="number">2</span>] == values[i + <span class="number">3</span>]) {
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) { <span class="comment">//2965</span>
|
||||
<span class="literal">int</span>[][] horizontalTestArray = { <span class="comment">// Horizontal</span>
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">1</span>},
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">0</span>, <span class="number">1</span>},
|
||||
{<span class="number">5</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">9</span>},
|
||||
{<span class="number">6</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">1</span>},
|
||||
{<span class="number">1</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>},
|
||||
{<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>}
|
||||
};
|
||||
|
||||
<span class="literal">int</span>[][] verticalTestArray = { <span class="comment">// Vertical</span>
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">1</span>},
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">0</span>, <span class="number">1</span>},
|
||||
{<span class="number">5</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">9</span>},
|
||||
{<span class="number">6</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">1</span>},
|
||||
{<span class="number">1</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>},
|
||||
{<span class="number">3</span>, <span class="number">5</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>}
|
||||
};
|
||||
<span class="literal">int</span>[][] diagonalTestArray1 = { <span class="comment">// Diagonal 1</span>
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">1</span>},
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">0</span>, <span class="number">1</span>},
|
||||
{<span class="number">9</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">9</span>},
|
||||
{<span class="number">6</span>, <span class="number">9</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">1</span>},
|
||||
{<span class="number">1</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>},
|
||||
{<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>}
|
||||
};
|
||||
<span class="literal">int</span>[][] diagonalTestArray2 = { <span class="comment">// Diagonal 2</span>
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">1</span>},
|
||||
{<span class="number">0</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">0</span>, <span class="number">1</span>},
|
||||
{<span class="number">9</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">9</span>},
|
||||
{<span class="number">6</span>, <span class="number">9</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">1</span>},
|
||||
{<span class="number">1</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>},
|
||||
{<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">7</span>}
|
||||
};
|
||||
|
||||
<span class="comment">// Create scanner</span>
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
||||
<span class="literal">int</span> userInput;
|
||||
<span class="literal">do</span> {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Do you want to enter an array, or use a predefined one?</span><span class="string">"</span>);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">1. Use predefined array.</span><span class="string">"</span>);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">2. Enter a new array.</span><span class="string">"</span>);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">-1. quit and exit program</span><span class="string">"</span>);
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Input: </span><span class="string">"</span>);
|
||||
userInput = input.nextInt();
|
||||
<span class="literal">if</span> (userInput == <span class="number">1</span>) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Selected </span><span class="literal">\"</span><span class="string">Use predefined array</span><span class="literal">\"</span><span class="string">"</span>);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Printing arrays...</span><span class="string">"</span>);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">1. horizontalTestArray</span><span class="string">"</span>);
|
||||
<span class="ST4">printArray</span>(horizontalTestArray);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">2. verticalTestArray</span><span class="string">"</span>);
|
||||
<span class="ST4">printArray</span>(verticalTestArray);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">3. diagonalTestArray1</span><span class="string">"</span>);
|
||||
<span class="ST4">printArray</span>(diagonalTestArray1);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">4. diagonalTestArray2</span><span class="string">"</span>);
|
||||
<span class="ST4">printArray</span>(diagonalTestArray2);
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Input: </span><span class="string">"</span>);
|
||||
userInput = input.nextInt();
|
||||
<span class="literal">if</span> (userInput == <span class="number">1</span>) {
|
||||
<span class="literal">if</span> (<span class="ST4">isConsecutiveFour</span>(horizontalTestArray)) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns true</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns false</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">if</span> (userInput == <span class="number">2</span>) {
|
||||
<span class="literal">if</span> (<span class="ST4">isConsecutiveFour</span>(verticalTestArray)) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns true</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns false</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">if</span> (userInput == <span class="number">3</span>) {
|
||||
<span class="literal">if</span> (<span class="ST4">isConsecutiveFour</span>(diagonalTestArray1)) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns true</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns false</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">if</span> (userInput == <span class="number">4</span>) {
|
||||
<span class="literal">if</span> (<span class="ST4">isConsecutiveFour</span>(diagonalTestArray2)) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns true</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns false</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
userInput = <span class="number">0</span>;
|
||||
}
|
||||
<span class="literal">if</span> (userInput == <span class="number">2</span>) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Selected </span><span class="literal">\"</span><span class="string">Enter new array</span><span class="literal">\"</span><span class="string">"</span>);
|
||||
<span class="literal">int</span>[][] newArray = <span class="ST4">inputArray</span>();
|
||||
<span class="literal">if</span> (<span class="ST4">isConsecutiveFour</span>(newArray)) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns true</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Testing method returns false</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
} <span class="literal">while</span> (userInput != -<span class="number">1</span>);
|
||||
System.<span class="ST4">exit</span>(<span class="number">0</span>);
|
||||
<span class="comment">//printArray(intArray1);</span>
|
||||
<span class="comment">//System.out.println(isConsecutiveFour(intArray1));</span>
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printArray</span>(<span class="literal">int</span>[][] array) {
|
||||
<span class="literal">int</span> rowCounter = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < array.<span class="ST3">length</span>; x++) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> y = <span class="number">0</span>; y < array[x].<span class="ST3">length</span>; y++) {
|
||||
System.<span class="ST2">out</span>.print(array[x][y] + <span class="string">"</span> <span class="string">"</span>);
|
||||
rowCounter++;
|
||||
<span class="literal">if</span> (rowCounter % (array.<span class="ST3">length</span> + <span class="number">1</span>) == <span class="number">0</span>) {
|
||||
System.<span class="ST2">out</span>.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printArray</span>(<span class="literal">int</span>[] array) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i : array) {
|
||||
System.<span class="ST2">out</span>.print(i + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">isConsecutiveFour</span>(<span class="literal">int</span>[][] values) {
|
||||
<span class="comment">// Horizontal Checking</span>
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Checking for Horizontal matches...</span><span class="string">"</span>);
|
||||
<span class="literal">boolean</span> horizontalTest = <span class="ST4">horizontalMatch</span>(values);
|
||||
<span class="comment">// Vertical Checking</span>
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Checking for Vertical matches...</span><span class="string">"</span>);
|
||||
<span class="literal">boolean</span> verticalTest = <span class="ST4">verticalMatch</span>(values);
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Checking for Diagonal matches...</span><span class="string">"</span>);
|
||||
<span class="literal">boolean</span> diagonalTest = <span class="ST4">diagonalMatch</span>(values);
|
||||
<span class="literal">if</span> (horizontalTest || verticalTest || diagonalTest) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Match found!</span><span class="string">"</span>);
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">No match found.</span><span class="string">"</span>);
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">diagonalMatch</span>(<span class="literal">int</span>[][] array) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> row = <span class="number">0</span>; row < array.<span class="ST3">length</span> - <span class="number">3</span>; row++) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> col = <span class="number">0</span>; col < array[row].<span class="ST3">length</span> - <span class="number">3</span>; col++) {
|
||||
<span class="comment">// Check for four consecutive numbers diagonally from top-left to bottom-right</span>
|
||||
<span class="literal">if</span> (array[row][col] == array[row + <span class="number">1</span>][col + <span class="number">1</span>] && array[row + <span class="number">1</span>][col + <span class="number">1</span>] == array[row + <span class="number">2</span>][col + <span class="number">2</span>]
|
||||
&& array[row + <span class="number">2</span>][col + <span class="number">2</span>] == array[row + <span class="number">3</span>][col + <span class="number">3</span>]) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Found four consecutive numbers diagonally at: </span><span class="string">"</span> + row + <span class="string">"</span><span class="string">,</span><span class="string">"</span> + col);
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
<span class="comment">// Check for four consecutive numbers diagonally from top-right to bottom-left</span>
|
||||
<span class="literal">if</span> (array[row][col + <span class="number">3</span>] == array[row + <span class="number">1</span>][col + <span class="number">2</span>] && array[row + <span class="number">1</span>][col + <span class="number">2</span>] == array[row + <span class="number">2</span>][col + <span class="number">1</span>]
|
||||
&& array[row + <span class="number">2</span>][col + <span class="number">1</span>] == array[row + <span class="number">3</span>][col]) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Found four consecutive numbers diagonally at: </span><span class="string">"</span> + row + <span class="string">"</span><span class="string">,</span><span class="string">"</span> + (col + <span class="number">3</span>));
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">verticalMatch</span>(<span class="literal">int</span>[][] values) {
|
||||
<span class="literal">int</span> intCounter = <span class="number">0</span>, y = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> rowIterate = <span class="number">0</span>; rowIterate < values.<span class="ST3">length</span>; ++rowIterate) {
|
||||
y = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < values.<span class="ST3">length</span> - <span class="number">1</span>; ++x) {
|
||||
++y;
|
||||
<span class="literal">if</span> (values[x][rowIterate] == values[y][rowIterate]) {
|
||||
intCounter++;
|
||||
System.<span class="ST2">out</span>.println(values[x][rowIterate] + <span class="string">"</span> <span class="string">"</span> + values[y][rowIterate] + <span class="string">"</span><span class="string">, intCounter: </span><span class="string">"</span> + intCounter);
|
||||
} <span class="literal">else</span> {
|
||||
intCounter = <span class="number">0</span>;
|
||||
System.<span class="ST2">out</span>.println(values[x][rowIterate] + <span class="string">"</span> <span class="string">"</span> + values[y][rowIterate] + <span class="string">"</span><span class="string">, intCounter: </span><span class="string">"</span> + intCounter);
|
||||
}
|
||||
<span class="literal">if</span> (intCounter == <span class="number">3</span>) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Vertical match!</span><span class="string">"</span>);
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
}
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Checking next line...</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">horizontalMatch</span>(<span class="literal">int</span>[][] values) {
|
||||
<span class="literal">int</span> intCounter = <span class="number">0</span>, y = <span class="number">0</span>;
|
||||
<span class="comment">// Horizontal checking</span>
|
||||
<span class="comment">// If the same value has been observed 4 times, return true</span>
|
||||
<span class="comment">//System.out.println("values[0].length: " + values[0].length);</span>
|
||||
<span class="comment">//System.out.println("values.length: " + values.length);</span>
|
||||
|
||||
<span class="literal">for</span> (<span class="literal">int</span> rowIterate = <span class="number">0</span>; rowIterate < values.<span class="ST3">length</span>; ++rowIterate) {
|
||||
y = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < values[<span class="number">0</span>].<span class="ST3">length</span> - <span class="number">1</span>; ++x) {
|
||||
++y;
|
||||
<span class="literal">if</span> (values[rowIterate][x] == values[rowIterate][y]) {
|
||||
intCounter++;
|
||||
System.<span class="ST2">out</span>.println(values[rowIterate][x] + <span class="string">"</span> <span class="string">"</span> + values[rowIterate][y] + <span class="string">"</span><span class="string">, intCounter: </span><span class="string">"</span> + intCounter);
|
||||
} <span class="literal">else</span> {
|
||||
intCounter = <span class="number">0</span>;
|
||||
System.<span class="ST2">out</span>.println(values[rowIterate][x] + <span class="string">"</span> <span class="string">"</span> + values[rowIterate][y] + <span class="string">"</span><span class="string">, intCounter: </span><span class="string">"</span> + intCounter);
|
||||
}
|
||||
<span class="literal">if</span> (intCounter == <span class="number">3</span>) {
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Horizontal match!</span><span class="string">"</span>);
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
}
|
||||
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Checking next line...</span><span class="string">"</span>);
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -73,7 +73,7 @@ public class MP1_CalebFontenot {
|
||||
{1, 5, 6, 1, 4, 0, 7},
|
||||
{3, 5, 3, 3, 4, 0, 7}
|
||||
};
|
||||
int[][] diagonalTestArray = { // Diagonal 1
|
||||
int[][] diagonalTestArray1 = { // Diagonal 1
|
||||
{0, 1, 0, 3, 1, 6, 1},
|
||||
{0, 1, 6, 8, 6, 0, 1},
|
||||
{9, 6, 2, 1, 8, 2, 9},
|
||||
@ -81,6 +81,15 @@ public class MP1_CalebFontenot {
|
||||
{1, 3, 9, 1, 4, 0, 7},
|
||||
{3, 3, 3, 9, 4, 0, 7}
|
||||
};
|
||||
int[][] diagonalTestArray2 = { // Diagonal 2
|
||||
{0, 1, 0, 3, 1, 6, 1},
|
||||
{0, 1, 6, 8, 6, 0, 1},
|
||||
{9, 6, 2, 1, 8, 2, 9},
|
||||
{6, 9, 6, 1, 1, 9, 1},
|
||||
{1, 3, 9, 1, 4, 0, 7},
|
||||
{3, 3, 3, 9, 4, 0, 7}
|
||||
};
|
||||
|
||||
// Create scanner
|
||||
Scanner input = new Scanner(System.in);
|
||||
int userInput;
|
||||
@ -98,8 +107,10 @@ public class MP1_CalebFontenot {
|
||||
printArray(horizontalTestArray);
|
||||
System.out.println("2. verticalTestArray");
|
||||
printArray(verticalTestArray);
|
||||
System.out.println("3. diagonalTestArray");
|
||||
printArray(diagonalTestArray);
|
||||
System.out.println("3. diagonalTestArray1");
|
||||
printArray(diagonalTestArray1);
|
||||
System.out.println("4. diagonalTestArray2");
|
||||
printArray(diagonalTestArray2);
|
||||
System.out.print("Input: ");
|
||||
userInput = input.nextInt();
|
||||
if (userInput == 1) {
|
||||
@ -117,7 +128,14 @@ public class MP1_CalebFontenot {
|
||||
}
|
||||
}
|
||||
if (userInput == 3) {
|
||||
if (isConsecutiveFour(diagonalTestArray)) {
|
||||
if (isConsecutiveFour(diagonalTestArray1)) {
|
||||
System.out.println("Testing method returns true");
|
||||
} else {
|
||||
System.out.println("Testing method returns false");
|
||||
}
|
||||
}
|
||||
if (userInput == 4) {
|
||||
if (isConsecutiveFour(diagonalTestArray2)) {
|
||||
System.out.println("Testing method returns true");
|
||||
} else {
|
||||
System.out.println("Testing method returns false");
|
||||
@ -167,10 +185,8 @@ public class MP1_CalebFontenot {
|
||||
System.out.println("Checking for Vertical matches...");
|
||||
boolean verticalTest = verticalMatch(values);
|
||||
System.out.println("Checking for Diagonal matches...");
|
||||
boolean diagonalNorthwest = MarkouCode.topLeftTriangleDiagonalNothwest(values);
|
||||
boolean diagonalMiddle = MarkouCode.bottomRightTriangleDiagonalNothwest(values);
|
||||
System.out.println(diagonalNorthwest);
|
||||
if (horizontalTest || verticalTest || diagonalNorthwest) {
|
||||
boolean diagonalTest = diagonalMatch(values);
|
||||
if (horizontalTest || verticalTest || diagonalTest) {
|
||||
System.out.println("Match found!");
|
||||
return true;
|
||||
}
|
||||
@ -178,6 +194,26 @@ public class MP1_CalebFontenot {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean diagonalMatch(int[][] array) {
|
||||
for (int row = 0; row < array.length - 3; row++) {
|
||||
for (int col = 0; col < array[row].length - 3; col++) {
|
||||
// Check for four consecutive numbers diagonally from top-left to bottom-right
|
||||
if (array[row][col] == array[row + 1][col + 1] && array[row + 1][col + 1] == array[row + 2][col + 2]
|
||||
&& array[row + 2][col + 2] == array[row + 3][col + 3]) {
|
||||
System.out.println("Found four consecutive numbers diagonally at: " + row + "," + col);
|
||||
return true;
|
||||
}
|
||||
// Check for four consecutive numbers diagonally from top-right to bottom-left
|
||||
if (array[row][col + 3] == array[row + 1][col + 2] && array[row + 1][col + 2] == array[row + 2][col + 1]
|
||||
&& array[row + 2][col + 1] == array[row + 3][col]) {
|
||||
System.out.println("Found four consecutive numbers diagonally at: " + row + "," + (col + 3));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean verticalMatch(int[][] values) {
|
||||
int intCounter = 0, y = 0;
|
||||
for (int rowIterate = 0; rowIterate < values.length; ++rowIterate) {
|
||||
@ -200,8 +236,9 @@ public class MP1_CalebFontenot {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean horizontalMatch(int[][] values) {
|
||||
int intCounter = 0, y = 0;
|
||||
int intCounter = 0, y = 0;
|
||||
// Horizontal checking
|
||||
// If the same value has been observed 4 times, return true
|
||||
//System.out.println("values[0].length: " + values[0].length);
|
||||
|
BIN
Semester 2/ZIPs/MP1_CalebFontenot.zip
Normal file
BIN
Semester 2/ZIPs/MP1_CalebFontenot.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user