Question

In: Computer Science

Write a java method that creates a two dimensional char array after asking the user to...

Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that

int rows=(int)Math.ceil(text.length()/key.length())+1;

int columns= key.length();

The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key.

For example, if the user enter 2 3 1 as key, then the columns of the array should start with the second, then third, and then first. After changing the columns, the method returns a String of all the characters in the 2d array but written row by row.

For example,

W R T

Y H J

using 2 3 1 is transformed into

R T W

H J Y >>>> The string becomes: RTWHJY

Solutions

Expert Solution

package test2;

import java.util.Scanner;

public class test {
   static Scanner in = new Scanner(System.in);
  
   static String shift(String text, String key)
   {
       try{
          
       int row = (int)Math.ceil(text.length()/key.length());
       int column = key.length();
       char Cmatrix[][] = new char[row][column];
       char CmatrixMod[][] = new char[row][column];
       String out = new String();
      
       int k = 0;
      
       for(int i = 0; i < row; i++)
           for(int j = 0; j < column; j++)
               Cmatrix [i][j] = text.charAt(k++);
      
       System.out.print("\nOriginal Matrix\n");
      
       for(int i = 0; i < row; i++){
           for(int j = 0; j < column; j++)
               System.out.print(Cmatrix [i][j]);
           System.out.print("\n");
       }
      
       System.out.print("\nTransformed Matrix\n");
      
       for(int i = 0; i < row; i++)
       {
           for(int j = 0; j < column; j++){
               int columnAt = Integer.parseInt(Character.toString(key.charAt(j))) - 1;
               CmatrixMod[i][j] = Cmatrix[i][columnAt];
               out = out + CmatrixMod[i][j];
               System.out.print(CmatrixMod[i][j]);
           }
           System.out.print("\n");
       }
      
       System.out.print("\n");
      
       return out;
      
       }catch(Exception e){
          
           return "Input Data Error";
      
       }
   }
   public static void main(String[] args) {
      
       String text = new String();
       String key = new String();
      
       System.out.print("Enter Text: ");
       text = in.nextLine();
       System.out.print("\nEnter key: ");
       key = in.nextLine();
      
       System.out.print(shift(text, key));
   }

}

//Alternate

package test2;

import java.util.Scanner;

public class test {
   static Scanner in = new Scanner(System.in);
  
   static String shift(String text, String key)
   {
       try{
          
       int row = (int)Math.ceil(text.length()/key.length());
       int column = key.length();
       char Cmatrix[][] = new char[row][column];
       String out = new String();
      
       int k = 0;
      
       for(int i = 0; i < row; i++)
           for(int j = 0; j < column; j++)
               Cmatrix [i][j] = text.charAt(k++);
      
       for(int i = 0; i < row; i++)
       {
           for(int j = 0; j < column; j++){
               int columnAt = Integer.parseInt(Character.toString(key.charAt(j))) - 1;
               out = out + Cmatrix[i][columnAt];
           }
       }
      
       return out;
      
       }catch(Exception e){
          
           return "Input Data Error";
      
       }
   }
   public static void main(String[] args) {
      
       String text = new String();
       String key = new String();
      
       System.out.print("Enter Text: ");
       text = in.nextLine();
       System.out.print("\nEnter key: ");
       key = in.nextLine();
      
       System.out.print(shift(text, key));
   }

}


Related Solutions

Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA,...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: -getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. -getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. -getRowTotal. This method should accept a two-dimensional array as...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT