Question

The last element in each array in a 2D array is incorrect. It’s your job to...

The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value.

In the first array, the final value should be the length of the first array.

In the second array, the final value should be the sum of the first value, and the second to last value in the array.

In the third array, the final value should be the length of the 2D array.

Create a method called fixArray(int[][] array, int row, int col, int value) that sets the [row][column] to the correct value. Then, call the fixArray method three times - once for each value change that you are supposed to make.

When inputting values to fixArray, you will have to hard code the row value, but the column value and the new value should be set using the array accessor methods. For example, if we wanted to set the value of the first index in the first array to the length of the 2D array, we would write:

fixArray(array, 0, 0, array.length)
public class ArrayPractice
{
public static void main(String[] args)
{
int[][] array = {{5, 4, 2, 1, 0}, {523, 63, 2342, 586, 1, 6534, 0}, {10, 9, 2, 0}};
//Call the fixArray method three times on this array:
  
  
  
print(array);
}
  
//Create a method to add the correct value to the array at the correct col, row
public static void fixArray(int[][] arr, int row, int col, int value)
{
  
}
  
  
  
//Do not make alterations to this method!
public static void print(int[][] array)
{
for(int[] row: array)
{
for(int num: row)
{
System.out.print(num + " ");
}
System.out.println();
}
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note : I have comment out the code , where i thought necessary for you to understand it well.So refer to commented part if any issue in getting code.

**************************************** DRIVER CLASS (ArrayPractice.java - file) *****************************************************

import java.util.Scanner;
public class ArrayPractice {

   public static void main(String[] args) {
      
       int[][] array = {{5, 4, 2, 1, 0}, {523, 63, 2342, 586, 1, 6534, 0}, {10, 9, 2, 0}};
      
      
       // length of first array stored in 2D array will calculated with the help of reference of that
       // first array stored at array[0] (i.e 0th index of array) so it is calculated as
       // (array[0].length)
fixArray(array,0,array[0].length-1,array[0].length);
  
// length of second array stored in 2D array will calculated with the help of reference of that
// second array stored at array[1] (i.e 1th index of array) so it is calculated as
// (array[1].length) and , first element of that second array will be : array[1][0]
// second element last of that second array of 2D array will be : array[1][array[1].length-2]
fixArray(array,1,array[1].length-1,array[1][0]+array[1][array[1].length-2]);
  
//Length of whole 2D array will be no. array's references stored in 2D array
// this will be calculated by ( array.length ) directly
fixArray(array,2,array[2].length-1,array.length);
  
print(array);
      

   }
  
  
   public static void fixArray(int[][] arr, int row, int col, int value)
   {
   arr[row][col]=value;
   }
  
   public static void print(int[][] array)
   {
   for(int[] row: array)
   {
   for(int num: row)
   {
   System.out.print(num + " ");
   }
   System.out.println();
   }
   }

****************************************** CONSOLE (Sample input/output) *********************************************************

5 4 2 1 5
523 63 2342 586 1 6534 7057
10 9 2 3

******************************************************************************************************************************************

I have also attach a pictorial view of example array , so refer to that image , Hope u will be more clear with the 2D array concept.

Thanks.


}

Add a comment
Know the answer?
Add Answer to:
The last element in each array in a 2D array is incorrect. It’s your job to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

  • COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;...

    COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;        int minIntValue = 0;        int maxIntValue = array.length - 1;        // Create bucket array        List<Integer>[] buckets = new List[bucketCount];        // Associate a list with each index in the bucket array           for(int i = 0; i < bucketCount; i++){            buckets[i] = new LinkedList<>();        }        //...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019...

    please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019 1. Write a class, SumOrRows. The main method will do the following methods: a. Call a method, createAndFillArray which: defines a 2 dim array of 3 rows and 4 columns, prompts the user for values and stores the values in the array. b. Calls a method, printArray, which: prints the Array as shown below c. Cails a method, calcSums, which: returns à 1 dimensional...

  • In Java Please Create A Program For The Following; Please Note: This program should be able...

    In Java Please Create A Program For The Following; Please Note: This program should be able accept changes to the values of constants ROWS and COLS when testing the codes. Switch2DRows Given a 2D array with ROWS rows and COLS columns of random numbers 0-9, re-order the rows such that the row with the highest row sum is switched with the first row. You can assume that 2D arrau represents a rectangular matrix (i.e. it is not ragged). Sample run:...

  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

  • Java method that accepts (so you don't need to make an array for it) a 2d...

    Java method that accepts (so you don't need to make an array for it) a 2d array.of unknown row and column length. it functions like minesweeper but a lot smaller scale. basically in this case the 0's in the array indicate nothing an 8's indicate a mine. So squares surrounding the 8 (mine) indicate how many mines are adjacent to it. So if there are two mines adjacent to one squre it will show two.so like this. 0 1 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT