Question

In: Computer Science

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

Solutions

Expert Solution

Here in the program didnt mentioned about no of rows and columns so I took it as 5.

TwoDimensionalDemo.java

package org.students;

import java.util.Random;

public class TwoDimensionalDemo {
  
//Declaring the constants
public static final int ROW=5;
public static final int COL=5;
   public static void main(String[] args) {
       //Calling the methods
       int[][] array=genRandom();
       printArray(array);
   int max=largest(array);
   int min=smallest(array);
   int sum=sumOfElements(array);
   //Displaying the results
   System.out.println("The Largest Element in the Array is :"+max);
   System.out.println("The Smallest Element in the Array is :"+min);
   System.out.println("The Sum of Elements in the Array is :"+sum);
     

   }
   //Method which Calculate the sum of elements in the array.
   private static int sumOfElements(int[][] array) {
       int sum=0;
       for(int i=0;i<array.length;i++)
       {
           for(int j=0;j<array[i].length;j++)
           {
               sum+=array[i][j];
           }
          
       }
       return sum;
   }
   //Method which find the smallest element in the array
   private static int smallest(int[][] array) {
       int min=0;
       min=array[0][0];
       for(int i=0;i<array.length;i++)
       {
           for(int j=0;j<array[i].length;j++)
           {
               if(array[i][j]<min)
               {
                   min=array[i][j];
               }
           }
          
       }
       return min;
   }
      
   //Method which find the largest Element in the array
   private static int largest(int[][] array) {
       int max=0;
       max=array[0][0];
       for(int i=0;i<array.length;i++)
       {
           for(int j=0;j<array[i].length;j++)
           {
               if(array[i][j]>max)
               {
                   max=array[i][j];
               }
           }
          
       }
       return max;
   }
   //Which which displays the elements in the array.
   private static void printArray(int[][] array) {
       System.out.println("The Elements In the Array are ::");
       for(int i=0;i<array.length;i++)
       {
           for(int j=0;j<array[i].length;j++)
           {
               System.out.print(array[i][j]+" ");
           }
           System.out.println("\n");
       }
      
   }
   //Method which generated the random numbers and stored in an array.
   private static int [][] genRandom() {
       int min=0;
       int max=500;
       Random r = 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]=r.nextInt((max - min) + 1) + min;  
           }
              
       }
       return array;
      
   }

}

_______________________________________________________________________________________

output:

The Elements In the Array are ::
79 462 276 178 81

412 291 304 363 80

193 141 52 101 404

459 417 21 74 383

157 213 331 8 460

The Largest Element in the Array is :462
The Smallest Element in the Array is :8
The Sum of Elements in the Array is :5940

________________________________________________________________________________________


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 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...
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 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...
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT