Question
--Create a Java application called FindElement . This application will need a number of parameters: Scanner class variable, called keyboard, for inputting information Integer variables,
--Create a Java application called FindElement . This application will need a number of parameters:
Scanner class variable, called keyboard, for inputting information
Integer variables, called numberOfRows and numberOfColumns
, for storing the dimensions of the array
Two dimensional array of double values, called arrayElements, which is the two dimensional array
This is a reference to a two dimensional array, which will not be created at this time. There reference will be null, since it is not pointing to anywhere.
One dimensional array of two integer values. This array will contain the row and column of the location in the array that has the largest value.
--The user will determine the size of the 2-dimensional array that they want to create.
Prompt the user to enter the dimensions of the array, read the dimensions from the keyboard and store them into the variables numberOfRows and numberOfColumns.
--You will need to allocate memory for the two-dimensional double array using the new command. This will create memory and store the reference value into the variable arrayElements.
When you create an array, you need to allocate memory for it by the following command.
arrayElements= new double[numberOfRows][numberOfColumns];
By using new, the computer will allocates space for the array and store the memory location of the array into the memory reference, which is stored in the arrayElements variable.
For Example: if the user responds with an array of 3 rows and 4 columns, it will correspond to the following:
Column
arrayElements [ 0 ] [ 1 ] [ 2 ] [ 3 ]
row [0] [ a ] [ b ] [ c ] [ d ]
row [1] [ e ] [ f ] [ g ] [ h ]
row [2] [ I ] [ j ] [ k ] [ l ]
When we want to reference the location of an array, we use arrayElements, along with the row and column for the specified location.
arrayElements [0][0] references location a.
arrayElements [0][1] references location b.
arrayElements [0][2] references location c.
arrayElements [0][3] references location d.
arrayElements [1][0] references location e.
arrayElements [1][1] references location f.
arrayElements [1][2] references location g.
arrayElements [1][3] references location h.
arrayElements [2][0] references location i.
arrayElements [2][1] references location j.
arrayElements [2][2] references location k.
arrayElements [2][3] references location l.
The application will now prompt the user to enter the values of each element of the array. Since you dont know the size of the array until the user enters the desired dimensions, you must use multiple loops to properly read the value for each.
Enter the number of rows of the desired array : 2
Enter the number of columns of the desired array: 4
Enter the Value for Row [0], Column [0]: 12
Enter the Value for Row [0], Column [1]: 34.1
Enter the Value for Row [0], Column [2]: 9.4
Enter the Value for Row [0], Column [3]: 14.3
Enter the Value for Row [1], Column [0]: 32.8
Enter the Value for Row [1], Column [1]: 1.7
Enter the Value for Row [1], Column [2]: 17.6
Enter the Value for Row [1], Column [3]: 14.9
To prompt the user for each entry, you can prompt the user to enter data for each specific location or you can build a double for loop to cycle through each row and each column. To build double for loop: for ( int row=0; row { for ( int column=0; column { // Prompt for Row [row], Column [column] // Read from keyboard and store into arrayElements[row][column] } } An alternative for the loop, you can use the .length field embedded in the array to go through each element // This first for loop will traverse each row in the array for ( int row=0; row { // This second for loop will traverse each column of each row for ( int column=0; column { // Prompt for Row [row], Column [column] // Read from keyboard and store into arrayElements[row][column] } } --To verify that the values have been input and allocated corrected, write a double loop to display the contents of the two-dimensional array. The double loop you used for reading the values of the array should be usable for displaying the array. If done properly for the 3 rows and 4 columns specified above, you output should look something like: Two-Dimensional Array: 3 rows X 4 columns Row 0: 12.0 34.1 9.4 14.3 Row 1: 32.8 1.7 17.6 14.9 Row 2: 2.3 87.1 67.6 8.0 --The last part of the application will be calling a method (which you will write), called locateLargest, which will search each element of the array and find the index location of the largest number in the two-dimensional array. The definition of this method is defined below: public static int[] locateLargest( double[][] arrayx2 ) Create the method locateLargest, which will contain the above signature. This method should also contain: Single dimensional array, called index, to store the index of the arraywhich has the largest value This method will accept the two-dimensional array, arrayElements, as a parameter. It will return a reference to a single dimensional array, which contains the index of the location that has the largest value. To call this method, the following may be used: index = locateLargest( arrayElements ); When the method locateLargest() is called, the reference of the array arrayElements is passed as an argument. When you referencing the two-dimensional array is referenced in the method locateLargest, you are using the parameter name arrayx2 instead of the original arrayElements variable name. When passing in the reference to the array as an argument, everything that makes up the two-dimensional array goes along with it. Please note that the single dimensional array is not created anywhere in the main method, it must be created in the locateLargest method. The memory reference is returned to the index variable, which can then be used by the main method to display the location of the array which contains the largest value. When you pass an array to a method as a reference, the details of that array are passed with it. arrayx2.length will contain the number of rows of the array argument arrayx2[0].length will contain the number of columns for the first row of the array argument arrayx2[1].length will contain the number of columns for the second row of the array argument System.out.printf( "The largest number is located at { %d, %d } ", index[0], index[1] ); --Create the locateLargest method to scan each row and column of the two-dimensional array to find the index which contains the largest value. This method must create an integer array which contains two elements, element [0] will contain the row which contains the largest value and element [1] will contain the column which contains the largest value
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started