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

Solutions

Expert Solution

import java.io.*;
import java.util.*;
class TwoDArrayStats
{
public static final int ROW = 10;
public static final int COL = 10;
//Create a method to print the array.
public static void printArray(int[][] A)
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
System.out.printf("%4d ", A[i][j]);
System.out.println();
}
}
  
//Create a method to find the largest element in the array.
public static int findLarge(int[][] A)
{
int large = A[0][0];
for(int i = 0; i < ROW; i++)
for(int j = 0; j < COL; j++)
if(A[i][j] > large)
large = A[i][j];
return large;   
}

//Create a method to find the smallest element in the array.
public static int findSmall(int[][] A)
{
int small = A[0][0];
for(int i = 0; i < ROW; i++)
for(int j = 0; j < COL; j++)
if(A[i][j] < small)
small = A[i][j];
return small;   
}

//Create a method to find the sum of all elements in the array.
public static int summation(int[][] A)
{
int sum = 0;
for(int i = 0; i < ROW; i++)
for(int j = 0; j < COL; j++)
sum += A[i][j];
return sum;
}

public static void main(String[] args)
{
Random ran = new Random();
int[][] Array = new int[ROW][COL];
for(int i = 0; i < ROW; i++)
for(int j = 0; j < COL; j++)
Array[i][j] = ran.nextInt(501);
System.out.println("The random element array is: ");
printArray(Array);
System.out.print("The largest element in the array is: "+findLarge(Array));
System.out.println();
System.out.print("The smallest element in the array is: "+findSmall(Array));
System.out.println();
System.out.println("The sum of the elements in the array is: "+summation(Array));
System.out.println();
}
}


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: 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...
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 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...
In JAVA Use a two-dimensional array to solve the following problem: A company has four salespeople...
In JAVA Use a two-dimensional array to solve the following problem: A company has four salespeople - Sales Person 1, Sales Person 2, Sales Person 3, and Sales Person 4. The company sells 5 products - Product 1, Product 2, Product 3, Product 4, and Product 5. Each day, a sales person hands in a slip with the following information: Sales Person Number (1,2,3, or 4), Product Number (1,2,3,4, or 5), and dollar value of that product sold. This dollar...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
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...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT