1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
GenaCL600 [577]
3 years ago
13

Write a class called DisArray with methods to convert a 1-dimensional array to a 2-dimensional array. The methods' name should b

e convert2D. You should create methods to convert int[] and String[], that can be tested against the following class. Your convert2D methods should choose the closest possible square-ish size for the 2D array. For example, if your input array is [10], its 2D conversion should be [3][4] or [4][3] -- you decide if you want to favor rows over columns. Your method should place the elements of the one-dimensional array into the two-dimensional array in row-major order, and fill the remaining elements with 0 (for integer arrays) or null (for String arrays). The process of filling unused elements with 0 or null is called padding. If the input array's length is a perfect square, e.g., [16], then your output should a square array, i.e., [4][4]. For any other size, your objective is to minimize the number of padded elements. For example, if your input is [10] you should opt for a [3][4] array instead of a [4][4]. The former will have only 2 padded elements; the latter 6.
Computers and Technology
1 answer:
meriva3 years ago
8 0
<h2>Answer:</h2>

=======================================================

//Class header definition

public class DisArray {

   //First method with int array as parameter

   public static void convert2D(int[] oneD) {

       //1. First calculate the number of columns

       //a. get the length of the one dimensional array

       int arraylength = oneD.length;

       //b. find the square root of the length and typecast it into a float

       float squareroot = (float) Math.sqrt(arraylength);

       //c. round off the result and save in a variable called row

       int row = Math.round(squareroot);

       //2. Secondly, calculate the number of columns

       //a. if the square of the number of rows is greater than or equal to the

       //length of  the one dimensional array,

       //then to minimize padding, the number of

       //columns  is the same as the number of rows.

       //b. otherwise, the number of columns in one more than the

       // number of rows

       int col = ((row * row) >= arraylength) ? row : row + 1;

       //3. Create a 2D int array with the number of rows and cols

       int [ ][ ] twoD = new int [row][col];

       //4. Place the elements in the one dimensional array into

       //the two dimensional array.

       //a. First create a variable counter to control the cycle through the one

       // dimensional array.

       int counter = 0;

       //b. Create two for loops to loop through the rows and columns of the

       // two  dimensional array.

       for (int i = 0; i < row; i++) {

           for (int j = 0; j < col; j++) {

               //if counter is less then the length of the one dimensional array,

               //then  copy the element at that position into the two dimensional

               //array.  And also increment counter by one.

               if (counter < oneD.length) {

                   twoD[i][j] = oneD[counter];

                   counter++;

              }

              //Otherwise, just pad the array with zeros

               else {

                   twoD[i][j] = 0;

               }

           }

       }

       //You might want to create another pair of loop to print the elements

       //in the  populated two dimensional array as follows

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

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

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

           }

           System.out.println("");

       }

   }     //End of first method

   //Second method with String array as parameter

   public static void convert2D(String[] oneD) {

       //1. First calculate the number of columns

       //a. get the length of the one dimensional array

       int arraylength = oneD.length;

       //b. find the square root of the length and typecast it into a float

       float squareroot = (float) Math.sqrt(arraylength);

       //c. round off the result and save in a variable called row

       int row = Math.round(squareroot);

       //2. Secondly, calculate the number of columns

       //a. if the square of the number of rows is greater than or equal to the length of  

       //the one dimensional array, then to minimize padding, the number of

       //columns  is the same as the number of rows.

       //b. otherwise, the number of columns in one more than the

       //number of rows.

       int col = (row * row >= arraylength) ? row : row + 1;

       //3. Create a 2D String array with the number of rows and cols

       String[][] twoD = new String[row][col];

       //4. Place the elements in the one dimensional array into the two

       // dimensional array.

       //a. First create a variable counter to control the cycle through the one

       // dimensional array.

       int counter = 0;

       //b. Create two for loops to loop through the rows and columns of the

       //two  dimensional array.

       for (int i = 0; i < row; i++) {

           for (int j = 0; j < col; j++) {

               //if counter is less then the length of the one dimensional array,

               //then  copy the element at that position into the two dimensional

               //array.  And also increment counter by one.

               if (counter < oneD.length) {

                   twoD[i][j] = oneD[counter];

                   counter++;

               }

               //Otherwise, just pad the array with null values

               else {

                   twoD[i][j] = null;

               }

           }

       }

       //You might want to create another pair of loop to print the elements  

       //in the populated two dimensional array as follows:

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

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

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

           }

           System.out.println("");

       }

   }    // End of the second method

   //Create the main method

   public static void main(String[] args) {

       //1. Create an arbitrary one dimensional int array

       int[] x = {23, 3, 4, 3, 2, 4, 3, 3, 5, 6, 5, 3, 5, 5, 6, 3};

       //2. Create an arbitrary two dimensional String array

       String[] names = {"abc", "john", "dow", "doe", "xyz"};

       

       //Call the respective methods

       convert2D(x);

       System.out.println("");

       convert2D(names);

   }         // End of the main method

}            // End of class definition

=========================================================

==========================================================

<h2>Sample Output</h2>

23 3 4 3  

2 4 3 3  

5 6 5 3  

5 5 6 3  

abc john dow  

doe xyz null

==========================================================

<h2>Explanation:</h2>

The above code has been written in Java and it contains comments explaining each line of the code. Please go through the comments. The actual executable lines of code are written in bold-face to distinguish them from the comments.

You might be interested in
What type of platform is SAP?
Alex787 [66]
SAP is a data and business processing platform.
7 0
3 years ago
How does beamforming improve network service?
fenix001 [56]

Beamforming can improve network service by using device locations to better target service signals.

This is because, beamforming helps to deliver higher signal quality to the target receiver.

<h3>What is beamforming?</h3>

beamforming can be regarded as application of different radiating elements that is transmitting the same signal.

This signal is usually identical in wavelength and phase, and  by reinforcing the waves in a specific direction the goal can be acheived.

Learn more about beamforming at:

brainly.com/question/12809344

7 0
3 years ago
Look at the following Python code:
aliya0001 [1]

Ump will be assigned to word2

word1[0:3] gets all of the letters from index 0 to index 3 exclusive (that means index 3 is not included)

4 0
3 years ago
It is the way to convey a message,an idea,a picture,or speech that is received and understood clearly and correctly by the perso
Goryan [66]

Answer:

Communication.

Explanation:

Communication can be defined as a process which typically involves the transfer of information from one person (sender) to another (recipient), through the use of semiotics, symbols and signs that are mutually understood by both parties.

In this context, communication is the way to convey a message, an idea, a picture, or speech that is received and understood clearly and correctly by the person for whom it is sent.​

Generally, the linear model of communication comprises of four (4) main components and these are;

1. Sender (S): this is typically the source of information (message) or the originator of a message that is being sent to a receiver. Thus, they are simply the producer of a message.

2. Channel (C): this is the medium used by the sender for the dissemination or transmission of the message to the recipient. For example, telephone, television, radio, newspapers, billboards etc.

3. Message (M): this is the information or data that is being sent to a recipient by a sender. It could be in the form of a video, audio, text message etc.

4. Receiver (R): this is typically the destination of information (message) or the recipient of a message that is being sent from a sender.

8 0
3 years ago
What term is used to identify the visual interface that displays information it receives from the computer?
olga nikolaevna [1]
The answer to this question is A
7 0
4 years ago
Other questions:
  • Match the items.
    15·1 answer
  • Pressing the Backspace key deletes the text to the of the insertion point. The left or the right?
    8·2 answers
  • When citing an Internet source:__________. a. it is ok to omit quotation marks if you change some words in the original sentence
    8·1 answer
  • You are developing a system to process lists of days of the week, represented as strings. Create a function is_weekend that cons
    12·1 answer
  • Can someone help me i need to write something for cyber bullying
    11·2 answers
  • Can someone help me with this pls
    5·2 answers
  • We will create a shopping cart, it is a dictionary mapping items in the car to the amount of that item. For example if there is
    9·1 answer
  • Which Internet connection type tends to use shared bandwidth?<br> Cable<br> Dial-up<br> DSL<br> ISDN
    5·2 answers
  • What is the difference between Remote Assistance and Remote Desktop?
    5·1 answer
  • which classification of money describes money that can be accessed quickly and easily, and includes coins and paper money as wel
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!