Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  CrossWord.Java
Language: JAVA
Code:
public class CrossWord {

     static char[][] arr;
     
     static class Cross                  // Inner class
     {
          int row;
          int column;
     
          Cross(int j, int i)            // Constructor
          {
               row = j;
               column = i;
          }
     }
     static Cross findIntersection(String s1, String s2)  // To find two string intersection.
     {
          for(int i = 0; i < s1.length(); i++)
          {
               for(int j = 0; j < s2.length(); j++)
               {
                    if(s1.charAt(i) == s2.charAt(j))
                         return new Cross(j, i);
               }
          }
                    return null;
     }
     
     static void placeHorizontal(String s1, int row, int col) // place string in Horizontal. 
     {
          for(int i = 0; i < s1.length(); i++)
          {
               arr[row][col + i] = s1.charAt(i);
          }
     }
     
     static void placeVertical(String s2, int row, int col)    // place string in vertical.
     {
          for(int i = 0; i < s2.length(); i++)
               arr[row + i][col] = s2.charAt(i);
     }
     
     static void print(char[][] arr1)                    // print the string.
     {
          for(int i = 0; i < arr1.length; i++)
          {
               for(int j = 0; j < arr1[i].length; j++)
                    System.out.print(arr[i][j]+ " ");
          System.out.println();
          }
     }
     
     static void initialize(char[][] arr1)         // initialize the method.
     {
          for(int i = 0; i < arr1.length; i++)
          {
               for(int j = 0 ; j < arr1[i].length; j++)
                    arr1[i][j] = '-';
          }
     }
     
     static void createCross(String s1, String s2)   // call all method here.
     {
          arr = new char[s2.length()][s1.length()];
          initialize(arr);
          Cross in = findIntersection(s1, s2);
          placeHorizontal(s1, in.row, 0);
          placeVertical(s2, 0, in.column);
          print(arr);
     }
     public static void main(String[] args) {
          
          createCross("MATCHES", "CHEESECAKE");
          createCross("PICNIC","EXCUSES");
     }

}
     
Comments: