Question

In: Computer Science

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 find the sum of all elements in the array

Generate output similar to the sample output below.

Sample Output:

The matrix is:

   16    8   23   23

    8   22   28    9

   23   19    9   19

   27   16   27   14

    6   11   13   5

The max value for the entire matrix is: 28

Solutions

Expert Solution

package org;

import java.util.Random;

import java.util.Scanner;

public class Matrix {

int ROW;

int COLUMN;

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);

Matrix m=new Matrix();

System.out.println("Enter number of rows: ");

m.ROW=sc.nextInt();

System.out.println("Enter number of columns: ");

m.COLUMN=sc.nextInt();

int[][] a=m.randomArray();

m.printArray(a);

m.largest(a);

m.smallest(a);

m.sum(a);

}

public void largest(int[][] a) {

// TODO Auto-generated method stub

int max=a[0][0];

for(int i=0;i<ROW;i++)

{

for(int j=0;j<COLUMN;j++)

{

if(a[i][j]>max)

{

max=a[i][j];

}

}

}

System.out.println("The largest element in the array is "+max);

}

public void smallest(int[][] a) {

// TODO Auto-generated method stub

int min=a[0][0];

for(int i=0;i<ROW;i++)

{

for(int j=0;j<COLUMN;j++)

{

if(a[i][j]<min)

{

min=a[i][j];

}

}

}

System.out.println("The smallest element in the array is "+min);

}

public void sum(int[][] a) {

// TODO Auto-generated method stub

int sum=0;

for(int i=0;i<ROW;i++)

{

for(int j=0;j<COLUMN;j++)

{

sum=sum+a[i][j];

}

}

System.out.println("The sum of the array is "+sum);

}

public void printArray(int[][] a) {

// TODO Auto-generated method stub

System.out.println("The array is: ");

for(int i=0;i<ROW;i++)

{

for(int j=0;j<COLUMN;j++)

{

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

}

System.out.println();

}

}

public int[][] randomArray() {

// TODO Auto-generated method stub

int a[][]=new int[ROW][COLUMN];

Random r=new Random();

for(int i=0;i<ROW;i++)

{

for(int j=0;j<COLUMN;j++)

{

a[i][j]=r.nextInt(31);

}

}

return a;

}

}

Output:


Related Solutions

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...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array 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. Create a...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept 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 values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following...
problem 1 (Duplicate Elimination) code in JAVA please Use a one-dimensional array to solve the following problem: Write an application that inputs ten numbers, each between 10 and 100, both inclusive. Save each number that was read in an array that was initialized to a value of -1 for all elements. Assume a value of -1 indicates an array element is empty. You are then to process the array, and remove duplicate elements from the array containing the numbers you...
In Java please Your program will sort a two dimensional array (5 * 4) based on...
In Java please Your program will sort a two dimensional array (5 * 4) based on the following: The entire array should be sorted using bubble sort based on the 1st column in ascending order and display the entire array. Reset the array to its original contents. The entire array should again be sorted using selection sort based on the 2nd column in descending order and display the entire array. Reset the array to its original contents. The entire array...
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 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 program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT