In: Computer Science
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
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));
}
}