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 and String key consisting of integers only, such that

int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ?

int columns= key.length();

int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder

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

After changing the columns, the method returns a String of all the characters in the 2d array but written column by column.

text = Sara;

key= 312

For example, the initial 2d array is

S A R
A

transformed into

R S A
A

The final retuned string is RSAA

Solutions

Expert Solution

Code with comments

import java.util.Scanner;

class Temp {

    static void print(char[][] arr) { // print char array as grid

        for (int i = 0; i < arr.length; i++) {

            for (int j = 0; j < arr[0].length; j++) {

                System.out.print(arr[i][j] + " ");

            }

            System.out.println();

        }

    }

    static void fun() {

        Scanner sc = new Scanner(System.in);

        String text = sc.nextLine();// input text

        String key = sc.nextLine();// input key

        sc.close();

        int cols = key.length();

        int rows = text.length() / cols;

        int rem = text.length() % cols;

        char[][] arr = new char[rows + 1][cols];// extra row for remainder

        for (int i = 0; i < rows; i++) {// fill row by row

            for (int j = 0; j < cols; j++) {// fill col by col

                arr[i][j] = text.charAt(cols * i + j);

            }

        }

        int j = 0;

        for (; j < rem; j++)// fill remainder

            arr[rows][j] = text.charAt(cols * rows + j);

        for (; j < cols; j++)// fikl zeros

            arr[rows][j] = 0;

        print(arr);

        String res = "";

        for (int i = 0; i < key.length(); i++) {

            int c = key.charAt(i) - '1'; // get column

            for (j = 0; j <= rows; j++) { // all rows of that column

                res += (arr[j][c] != 0 ? arr[j][c] : "");

            }

        }

        System.out.println(res);

    }

    public static void main(String[] args) {

        fun();

    }

}


Related Solutions

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...
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.
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
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 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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT