Java Project: Concepts tested by this program: Variables Input/output Loops Cond
ID: 3720951 • Letter: J
Question
Java Project:
Concepts tested by this program:
Variables
Input/output
Loops
Conditional Statements
Methods
Arrays
Multi-Dimensional Arrays
Program 2: (Pattern Recognition)Consecutive four equal numbers
Name your program PatterRecognition.java
Problem Description:
Write the following function that tests whether a two-dimensional list has four consecutive numbers of the same value, either horizontally, vertically, or diagonally.
public static boolean isConsecutiveFour(int[][] values)
Write a test program that prompts the user to enter the number of rows and columns of a two-dimensional list and then the values in the list and displays True if the list contains four consecutive numbers with the same value. Otherwise, display False. Here are some examples of the true cases:
Doc1.jpeg Compatibility Mode Saved to this PC Sign in -0 Insert Design Layout References Mailings Review View Help Tell me what you want to do Share File Home Cut ?Copy Replace Paste ?????,it?. Nor al TNo Spac, tending 1 Heading 2 Title Subtitle Subtle Em, Emphasis; Select Fonmat Painter Clipboard Font Paragraph Styles G Editing 0 10 3 1 6 1 0 1 6 8 6 0 1 5 6 21829 5 521829 5 6 21629 9 6 2 18 29 6 5 611 91 65611 91 6 5 661 91 6 9 61191 13614 07 1 561407 1 361407 1 391407 3 3 3 3 4073 5 3 3 407 3 6 3 3 4073 3 3 9 4 0 7 0 1 0 3 1 6 1 0 1 6 86 0 1 0 1 0 3 1 6 1 0 168 6 0 1 0 1 0 3 1 6 1 0 168 6 0 1 Page 1 otl 0 words ??? +100%Explanation / Answer
I have completed the required method for you. The method contains four nested for loop blocks to check for the consecutive occurrence of a value either vertically, horizontally, diagonally from right to left, or diagonally from left to right. The method is working perfectly as intended and explained by comments. Thanks
// PatternRecognition.java
import java.util.Scanner;
public class PatternRecognition {
/**
* method to check for the 4 consecutive occurrences of a value in a 2d array
* @param values - a 2d array
* @return - true if found,else false
*/
public static boolean isConsecutiveFour(int[][] values) {
int rows = values.length;
int cols = values[0].length;
int value = 0;// current value which is being checked
int consecutiveCount;// a counter to keep track of current value count
/**
* checking horizontally
*/
for (int i = 0; i < rows; i++) {
consecutiveCount = 0;// resetting the counter
for (int j = 0; j < cols; j++) {
if (j == 0) {
// first value in the row, making it as current value,
// and incrementing the count
value = values[i][j];
consecutiveCount++;
} else if (values[i][j] == value) {
// same as the previous value, incrementing the count
consecutiveCount++;
if (consecutiveCount == 4) {
// consecutive count reached 4, returning true
return true;
}
} else {
// different value, making it as the current value and
// setting count to 1
value = values[i][j];
consecutiveCount = 1;
}
}
}
/**
* checking vertically
*/
for (int i = 0; i < cols; i++) {
consecutiveCount = 0;// resetting the counter
for (int j = 0; j < rows; j++) {
if (j == 0) {
// first value in the row, making it as current value,
// and incrementing the count
value = values[j][i];
consecutiveCount++;
} else if (values[j][i] == value) {
// same as the previous value, incrementing the count
consecutiveCount++;
if (consecutiveCount == 4) {
// consecutive count reached 4, returning true
return true;
}
} else {
// different value, making it as the current value and
// setting count to 1
value = values[j][i];
consecutiveCount = 1;
}
}
}
/**
* checking diagonally, left to right
*/
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// getting current value
value = values[i][j];
// resetting count
consecutiveCount = 0;
/**
* continuously searching diagonally to the right until 4
* consecutive values are found or bounds are met
*/
int tmpRow = i, tmpCol = j;
while (tmpRow < rows && tmpCol < cols) {
if (value == values[tmpRow][tmpCol]) {
consecutiveCount++;
if (consecutiveCount == 4) {
return true;
}
} else {
break;
}
tmpRow++;//next row
tmpCol++;//next column
}
}
}
/**
* checking diagonally, right to left
*/
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
value = values[i][j];
consecutiveCount = 0;
/**
* continuously searching diagonally to the left until 4
* consecutive values are found or bounds are met
*/
int tmpRow = i, tmpCol = j;
while (tmpRow < rows && tmpCol >= 0) {
if (value == values[tmpRow][tmpCol]) {
consecutiveCount++;
if (consecutiveCount == 4) {
return true;
}
} else {
break;
}
tmpRow++;//next row
tmpCol--;//previous column
}
}
}
//not found
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int array[][];
/**
* getting inputs
*/
System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int cols = scanner.nextInt();
array = new int[rows][cols];
/**
* creating and initializing the array
*/
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print("Enter value for row-" + i + ", col-" + j
+ ": ");
array[i][j] = scanner.nextInt();
}
}
/**
* printing the array in proper order
*/
System.out.println("Input array:");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
/**
* testing the method
*/
System.out.println("has four values occuring consecutively? "
+ isConsecutiveFour(array));
}
}
/*OUTPUT*/
Enter number of rows: 5
Enter number of columns: 4
Enter value for row-0, col-0: 1
Enter value for row-0, col-1: 2
Enter value for row-0, col-2: 3
Enter value for row-0, col-3: 2
Enter value for row-1, col-0: 4
Enter value for row-1, col-1: 5
Enter value for row-1, col-2: 6
Enter value for row-1, col-3: 1
Enter value for row-2, col-0: 1
Enter value for row-2, col-1: 2
Enter value for row-2, col-2: 1
Enter value for row-2, col-3: 2
Enter value for row-3, col-0: 2
Enter value for row-3, col-1: 1
Enter value for row-3, col-2: 4
Enter value for row-3, col-3: 1
Enter value for row-4, col-0: 1
Enter value for row-4, col-1: 2
Enter value for row-4, col-2: 4
Enter value for row-4, col-3: 1
Input array:
1 2 3 2
4 5 6 1
1 2 1 2
2 1 4 1
1 2 4 1
has four values occuring consecutively? true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.