Question

In: Computer Science

Write in main • setArray() – This method has two integer parameters, one for the rows...

Write in main

• setArray() – This method has two integer parameters, one for the rows of a two-dimensional array and the other for the columns of a two-dimensional array. The method will return a two-dimensional array of integers with rows and columns given by the parameters. In addition, the method will use the Random class, along with the nextInt() method, to give each element of the two-dimensional array a value between 1 and 20.

• display() – This is a void method that has three parameters: a twodimensional array of integers, the number of rows, and the number of columns. The method will display all the elements of the array, under the following conditions: o Each row will be displayed on a separate line. o Each value in the array should be displayed using System.out.printf with a field width of 3 characters. o There should be at least one space between each element, so the rows line up neatly.

• rowAvg() – This is a void method that has three parameters: a twodimensional array of integers, the number of rows, and the number of columns. The method will calculate and display the averages of the elements of each row in the two-dimensional array. The averages should be displayed using System.out.printf so they can be rounded to two decimal places.

• colAvg() – This is a void method that has three parameters: a twodimensional array of integers, the number of rows, and the number of columns. The method will calculate and display the averages of the elements of each column in the two-dimensional array. The averages should be displayed using System.out.printf so they can be rounded to two decimal places.

Solutions

Expert Solution

import java.util.*;

class Array2D
{
public static void main (String[] args)
{
  Scanner input = new Scanner(System.in);
  System.out.println("Enter the number of rows : ");
  int rows = input.nextInt();
  System.out.println("Enter the number of columns : ");
  int cols = input.nextInt();
  int[][] array = new int[rows][cols];
  array = setArray(rows,cols);
  
  System.out.println("Random array : ");
  display(array,rows,cols);
  
  System.out.println("Average of rows in the array : ");
  rowAvg(array,rows,cols);
  
  System.out.println("\nAverage of columns in the array : ");
  colAvg(array,rows,cols);
}
public static int[][] setArray(int rows,int cols)
{
  int[][] array = new int[rows][cols];
  Random rand = new Random();
  for(int i = 0;i<rows;i++)
  {
   for(int j = 0;j<cols;j++)
   {
    array[i][j] = rand.nextInt(19) + 1;
   }
  }
  return array;
  

}
    public static void display(int[][] array,int rows,int cols)
    {
    for(int i = 0;i<rows;i++)
  {
   for(int j = 0;j<cols;j++)
   {
    System.out.printf("%3d ",array[i][j]);
   }
   System.out.println();
  }
   
    }
    public static void rowAvg(int[][] array,int rows,int cols)
{
  double[] avgRow = new double[rows];
  for(int i = 0;i<rows;i++)
  {
   avgRow[i] = 0;
   for(int j = 0;j<cols;j++)
   {
    avgRow[i] = avgRow[i] + array[i][j];
   }
   System.out.printf("%.2f\t",avgRow[i]/cols);
  }
  
}
public static void colAvg(int[][] array,int rows,int cols)
{
double[] avgCol = new double[cols];
  for(int i = 0;i<cols;i++)
  {
   avgCol[i] = 0;
   for(int j = 0;j<rows;j++)
   {
    avgCol[i] = avgCol[i] + array[j][i];
   }
   System.out.printf("%.2f\t",avgCol[i]/rows);
  }
}
}

Output:

Enter the number of rows :3
Enter the number of columns :4
Random array :
7 10   6 14
4 12 17   7
7   9   4 19
Average of rows in the array :
9.25 10.00 9.75
Average of columns in the array :
6.00 10.33 9.00 13.33

Do ask if any doubt. Please upvote.


Related Solutions

IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to the 1st parameter the concatenation of both parameters. Perform the concatenation using the + operator. We are assuming reference parameter work that way (they don't).    Version #2 - fine. return the value of the 1st param after concatenating to it the second parameter then.    Version #3 - Let's do it the proper way now, I say. Create a temporary String reference variable...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT