Progress on diagonal searching
parent
7fd9b8a489
commit
44950337e6
@ -0,0 +1,234 @@
|
||||
/*
|
||||
* 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.mp1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class MarkouCode {
|
||||
|
||||
/**
|
||||
* Traverses the parm array diagonally from top left towards the top and
|
||||
* prints the (i,j) indexes of the traversal.
|
||||
*
|
||||
* @param ar 2D array
|
||||
*/
|
||||
public static boolean topLeftTriangleDiagonalNothwest(int[][] ar)
|
||||
{
|
||||
System.out.println("diagonal indexes top-triangle of array ");
|
||||
int rowCount = 0;
|
||||
for (int columnCounter = 0; columnCounter < ar[0].length; ++columnCounter)
|
||||
{
|
||||
int i = 0;
|
||||
int j = columnCounter;
|
||||
int[] diagonalArray = new int[rowCount + 1];
|
||||
for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter)
|
||||
{
|
||||
diagonalArray[diagonalCounter] = ar[i][j];
|
||||
if (isConsecutiveFour(diagonalArray)) {
|
||||
return true;
|
||||
}
|
||||
System.out.print(i + "," + j + " ");
|
||||
++i;
|
||||
j--;
|
||||
if (i == ar.length | j == ar[0].length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
rowCount++;
|
||||
|
||||
System.out.println("");
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*Traverses the parm array diagonally from bottom right towrads the to
|
||||
//and prints the (i,j) indexes of the traversal.
|
||||
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void bottomRightTriangleDiagonalNothwest(int[][] ar)
|
||||
{
|
||||
System.out.println("diagonal indexes bottom-triangle of array ");
|
||||
|
||||
int rowCount = 0;
|
||||
int numColumns = 0;
|
||||
if (ar[0].length == ar.length)//square table
|
||||
{
|
||||
numColumns = ar[0].length - 1;
|
||||
}
|
||||
else if (ar[0].length > ar.length)//wide table
|
||||
{
|
||||
numColumns = ar.length - 1;
|
||||
}
|
||||
|
||||
else //narrow-width rectangle array
|
||||
{
|
||||
numColumns = ar[0].length;
|
||||
}
|
||||
|
||||
for (int columnCounter = 0; columnCounter < numColumns; ++columnCounter)
|
||||
{
|
||||
int i = ar.length - 1;
|
||||
int j = ar[0].length - 1 - columnCounter;
|
||||
for (int diagonalCounter = 0; diagonalCounter <= rowCount; ++diagonalCounter)
|
||||
{
|
||||
|
||||
System.out.print(i + "," + j + " ");
|
||||
--i;
|
||||
j++;
|
||||
}
|
||||
rowCount++;
|
||||
System.out.println("");
|
||||
|
||||
}
|
||||
//middle chunk of narrow array
|
||||
|
||||
System.out.println("-----------------------");
|
||||
if (ar.length > ar[0].length)
|
||||
{
|
||||
System.out.println("diagonal indexes middle part of array when the array "
|
||||
+ "is narrow ");
|
||||
|
||||
for (int i = 1; i < ar.length - ar[0].length; ++i)
|
||||
{
|
||||
int rowIndex = i;
|
||||
int columnIndex = ar[0].length - 1;
|
||||
for (int j = 0; j < ar[0].length; ++j)
|
||||
{
|
||||
|
||||
System.out.print(rowIndex + "," + columnIndex + " ");
|
||||
rowIndex++;
|
||||
columnIndex--;
|
||||
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean isConsecutiveFour(int[][] values)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
public static boolean isConsecutiveFour(int[] values)
|
||||
{
|
||||
for (int i = 0; i < values.length - 4; ++i) {
|
||||
if ( values[i] == values[i+1] &&
|
||||
values[i+1] == values[i+2] &&
|
||||
values[i+2] == values[i+3]
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void printIndexesDiagonally(int ar[][])
|
||||
{
|
||||
topLeftTriangleDiagonalNothwest(ar);
|
||||
System.out.println("------------------------");
|
||||
bottomRightTriangleDiagonalNothwest(ar);
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int[][] ar1 =
|
||||
{
|
||||
{
|
||||
1, 1, 1, 1, 8
|
||||
},
|
||||
{
|
||||
1, 1, 1, 8, 9
|
||||
},
|
||||
{
|
||||
1, 1, 8, 1, 9
|
||||
},
|
||||
{
|
||||
1, 8, 1, 3, 2
|
||||
},
|
||||
};
|
||||
|
||||
int[][] ar2 =
|
||||
{
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1, 1, 1, 1, 1, 1
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
int[][] ar3 =
|
||||
{
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
{
|
||||
1, 1, 1
|
||||
},
|
||||
|
||||
};
|
||||
System.out.println(topLeftTriangleDiagonalNothwest(ar1));
|
||||
/*
|
||||
System.out.println("SQUARE array of size 4x4");
|
||||
printIndexesDiagonally(ar1);
|
||||
|
||||
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
|
||||
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
|
||||
System.out.println("\nWIDE array of size 8x5");
|
||||
printIndexesDiagonally(ar2);
|
||||
|
||||
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
|
||||
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
|
||||
System.out.println("\nNARROW array of size 10x3");
|
||||
printIndexesDiagonally(ar3);
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>MyInteger.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}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.ST2 {color: #9876aa}
|
||||
.ST3 {color: #ffc66d}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST4 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST6 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST5 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab3_CalebFontenot/src/main/java/com/calebfontenot/lab3_calebfontenot/MyInteger.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.lab3_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> MyInteger {
|
||||
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">value</span>;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Get</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">value</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">the</span> <span class="comment">value</span> <span class="comment">of</span> <span class="comment">value</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">getValue</span>()
|
||||
{
|
||||
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST2">value</span>;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">hashCode</span>()
|
||||
{
|
||||
<span class="literal">int</span> hash = <span class="number">7</span>;
|
||||
<span class="literal">return</span> hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">equals</span>(Object obj)
|
||||
{
|
||||
<span class="literal">if</span> (<span class="literal">this</span> == obj) {
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
<span class="literal">if</span> (obj == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
<span class="literal">if</span> (getClass() != obj.getClass()) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
<span class="literal">final</span> MyInteger other = (MyInteger) obj;
|
||||
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST2">value</span> == other.<span class="ST2">value</span>;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> String <span class="ST3">toString</span>()
|
||||
{
|
||||
<span class="literal">return</span> <span class="string">"</span><span class="string">MyInteger{</span><span class="string">"</span> + <span class="string">"</span><span class="string">value=</span><span class="string">"</span> + <span class="ST2">value</span> + <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyInteger(<span class="literal">int</span> value)
|
||||
{
|
||||
<span class="literal">this</span>.<span class="ST2">value</span> = value;
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST4">isEven</span>(MyInteger other) {
|
||||
<span class="literal">return</span> other.getValue() % <span class="number">2</span> == <span class="number">0</span>;
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST4">isPrime</span>(MyInteger other) {
|
||||
<span class="literal">int</span> numToTest = other.getValue();
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">2</span>; i <= numToTest / <span class="number">2</span>; ++i) {
|
||||
<span class="literal">if</span> (numToTest % i == <span class="number">0</span>) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST4">parseInteger</span>(<span class="literal">char</span>[] array) {
|
||||
String s = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">char</span> i: array) {
|
||||
s += i;
|
||||
}
|
||||
<span class="literal">return</span> Integer.<span class="ST5">parseInt</span>(String.<span class="ST5">valueOf</span>(s));
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args)
|
||||
{
|
||||
|
||||
System.<span class="ST6">out</span>.println(<span class="ST5">p</span><span class="ST5">arseInteger</span>(<span class="literal">new</span> <span class="literal">char</span>[] {<span class="string">'</span><span class="string">7</span><span class="string">'</span>, <span class="string">'</span><span class="string">6</span><span class="string">'</span>}));
|
||||
System.<span class="ST6">out</span>.println(MyInteger.<span class="ST5">isPrime</span>(<span class="literal">new</span> MyInteger(<span class="number">3</span>)));
|
||||
System.<span class="ST6">out</span>.println(MyInteger.<span class="ST5">isEven</span>(<span class="literal">new</span> MyInteger(<span class="number">4</span>)));
|
||||
System.<span class="ST6">out</span>.println(MyInteger.<span class="ST5">isEven</span>(<span class="literal">new</span> MyInteger(<span class="number">3</span>)));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.lab3_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class MyInteger {
|
||||
|
||||
private int value;
|
||||
|
||||
/**
|
||||
* Get the value of value
|
||||
*
|
||||
* @return the value of value
|
||||
*/
|
||||
public int getValue()
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int hash = 7;
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final MyInteger other = (MyInteger) obj;
|
||||
return this.value == other.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "MyInteger{" + "value=" + value + '}';
|
||||
}
|
||||
|
||||
public MyInteger(int value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
public static boolean isEven(MyInteger other) {
|
||||
return other.getValue() % 2 == 0;
|
||||
}
|
||||
public static boolean isPrime(MyInteger other) {
|
||||
int numToTest = other.getValue();
|
||||
for (int i = 2; i <= numToTest / 2; ++i) {
|
||||
if (numToTest % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static int parseInteger(char[] array) {
|
||||
String s = "";
|
||||
for (char i: array) {
|
||||
s += i;
|
||||
}
|
||||
return Integer.parseInt(String.valueOf(s));
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
|
||||
System.out.println(parseInteger(new char[] {'7', '6'}));
|
||||
System.out.println(MyInteger.isPrime(new MyInteger(3)));
|
||||
System.out.println(MyInteger.isEven(new MyInteger(4)));
|
||||
System.out.println(MyInteger.isEven(new MyInteger(3)));
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue