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

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...
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 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...
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept 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 values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT