JAVA JAVA JAVA Please include comment lines 8.19 (Pattern recognition: four cons
ID: 3847719 • Letter: J
Question
JAVA JAVA JAVA Please include comment lines
8.19 (Pattern recognition: four consecutive equal numbers) Write the following method that tests whether a two-dimensional array has four consecutive numbers of the same value, either horizontally, vertically, or diagonally.
public static boolean isConsecutiveFour(int[][] values)
FIGURE
Write a test program that prompts the user to enter the number of rows and columns of a two-dimensional array and then the values in the array and displays true if the array contains four consecutive numbers with the same value. Otherwise, display false. Here are some examples of the true cases:
| | | | 5621829 5621629 9621829 1361407 1361407 1391407 3333407 3633407 3339407 119177 16814 38111 9 062603 116933 009613 119177 602900 166 381613 005613 119177 602900 168144 062663 119177 602900 16814 38111 062663 11653 005613Explanation / Answer
import java.util.Scanner;
public class pattern{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = input.nextInt();
System.out.print("Enter the number of columns: ");
int col = input.nextInt();
int[][] arr = new int[rows][col];
System.out.println("Enter the array values: ");
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = input.nextInt();
}
System.out.println(isConsecutiveFour(arr));
}
public static boolean isConsecutiveFour(int[][] values){
int numberOfRows = values.length;
int numberOfColumns = values[0].length;
for (int i = 0; i < numberOfRows; i++){
if (isConsecutiveFour(values[i])){
return true;
}
}
for (int j = 0; j < numberOfColumns; j++){
int[] column = new int[numberOfRows];
for (int i = 0; i < numberOfRows; i++){
column[i] = values[i][j];
}
if (isConsecutiveFour(column)){
return true;
}
}
for (int i = 0; i < numberOfRows - 3; i++){
int numberOfElementsInDiagonal = Math.min(numberOfRows - i, numberOfColumns);
int[] diagonal = new int[numberOfElementsInDiagonal];
for (int k = 0; k < numberOfElementsInDiagonal; k++){
diagonal[k] = values[(k + i)][k];
}
if (isConsecutiveFour(diagonal)){
return true;
}
}
for (int j = 1; j < numberOfColumns - 3; j++){
int numberOfElementsInDiagonal = Math.min(numberOfColumns - j, numberOfRows);
int[] diagonal = new int[numberOfElementsInDiagonal];
for (int k = 0; k < numberOfElementsInDiagonal; k++){
diagonal[k] = values[k][(k + j)];
}
if (isConsecutiveFour(diagonal)) {
return true;
}
}
for (int j = 3; j < numberOfColumns; j++) {
int numberOfElementsInDiagonal = Math.min(j + 1, numberOfRows);
int[] diagonal = new int[numberOfElementsInDiagonal];
for (int k = 0; k < numberOfElementsInDiagonal; k++) {
diagonal[k] = values[k][(j - k)];
}
if (isConsecutiveFour(diagonal)) {
return true;
}
}
for (int i = 1; i < numberOfRows - 3; i++) {
int numberOfElementsInDiagonal = Math.min(numberOfRows - i, numberOfColumns);
int[] diagonal = new int[numberOfElementsInDiagonal];
for (int k = 0; k < numberOfElementsInDiagonal; k++) {
diagonal[k] = values[(k + i)][(numberOfColumns - k - 1)];
}
if (isConsecutiveFour(diagonal)) {
return true;
}
}
return false;
}
public static boolean isConsecutiveFour(int[] values) {
for (int i = 0; i < values.length - 3; i++) {
boolean isEqual = true;
for (int j = i; j < i + 3; j++) {
if (values[j] != values[(j + 1)]) {
isEqual = false;
break;
}
}
if (isEqual) return true;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.