Question

In: Computer Science

Write a method that will accept a 2D character array. The 2D array represents a grid...

Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to bottom). You should NOT consider diagonals. If a set is found, stop searching and return the character in the set, if no set is found throw the SetNotFoundException and provide an appropriate message.

You may either write your answer in the space provided or upload a PDF containing your solution.

Examples:
For the following array a search will result in a triple being found and returning the character C:

For the following array a search will result in a triple being found and returning the character B:

For the following array a search will result in a triple NOT being found and throwing the SetNotFoundException:

/**
Searches the character grid for the appearance of a set of 3 identical characters, horizontally or vertically.
@param searchGrid The 2D character array in which the search will occur.
@return The character that was in the set.
@throws SetNotFoundException if the searchGrid does not contain a set of exactly 3 matching characters.
*/
public static char setSearchChar(char[][] searchGrid) throws SetNotFoundException {

The language is Java

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;


class setNotFoundException extends Exception{
setNotFoundException(String s){
super(s);
}
}

public class Solution
{
static int MAX = 26;
public static char setSearchChar(char[][] searchGrid) throws setNotFoundException
{
int x_len = searchGrid[0].length;
int y_len = searchGrid.length;
  
for(int i = 0;i<x_len;i++){
for(int j = 0;j<y_len-2;j++){
if(searchGrid[i][j] == searchGrid[i][j+1] && searchGrid[i][j+1] == searchGrid[i][j+2])
return searchGrid[i][j];
}
}
  
  
for(int i = 0;i<y_len;i++)
{
for(int j =0;j<x_len-2;j++){
if(searchGrid[j][i] == searchGrid[j+1][i] && searchGrid[j+1][i] == searchGrid[j+2][i])
return searchGrid[j][i];
}
}
  
throw new setNotFoundException("For the following array a search will result in a triple NOT being found and throwing the SetNotFoundException");
  
}
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
      
       Scanner sc = new Scanner(System.in);
      
       char []alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };
      
       char[][] searchGrid = new char[10][10];
//        for(int i = 0;i<10;i++){
//        for(int j = 0;j<10;j++){
//        char ch = sc.next().charAt(0);
//        searchGrid[i][j] = ch;
//        }
//        }

// The following is a code to generate random characters to be place inside searchGrid if manual
// entry is required then commnt this code and uncomment above code.

for(int i = 0;i<10;i++){
for(int j = 0;j<10;j++){
char ch = alphabet[(int) (Math.random() * 10 % MAX)];
searchGrid[i][j] = ch;
}
}
      
       char c= setSearchChar(searchGrid);
       System.out.println("For the following array a search will result in a triple being found and returning the character " + c);
   }
}

/* The code has been compiled in the code chef compiler. Tested for about 20 random results seems workable please let me know if some error persists */


Related Solutions

IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
Please write a Java method contains that checks whether the second given character array is contained...
Please write a Java method contains that checks whether the second given character array is contained in the first given character array. We require that both of the arrays are partially filled.
write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with...
write a java programming using 2D arrey. The Lo Shu Magic Square is a grid with 3 rows and 3 columns. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 (each number only once) The sum of each row, each column and each diagonal are the same In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional array as an argument, and...
write a function that accept an array and its size than return an array call modeAry...
write a function that accept an array and its size than return an array call modeAry that store the following information:    modeAry[0] = Number of modes; modeAry[1] = Frequency of the modes ; modeAry[>=2] = All the modes you found   ;        EXP:if the array input is [1,1,2,2,4,3,3], the function modAry should be [3,2,1,2,3]. In c++
Write a code to initialize a 2D array with random integers (not has to be different)....
Write a code to initialize a 2D array with random integers (not has to be different). Write a function to count the number of cells that contain an even number within the 2D array. C++
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT