Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * @(#)TestArray.java * * * @author * @version 1.00 2016/12/11 */ import java

ID: 3577718 • Letter: #

Question

/**
* @(#)TestArray.java
*
*
* @author
* @version 1.00 2016/12/11
*/
import java.util.*;
class IntArray{
int [][]A;
int rows;
int colms;
int min;
int max;
//***********************************Constructor
public IntArray(int rows, int colms, int min, int max){
}
//***********************************
public void initializeArray(){
Random rn=new Random();
//initializes the array with random numbers in the range min to max.
}
//***********************************
public void printArray(){}

//***********************************
public int getRows(){
//return rows
}
//***********************************
public int getColms(){
//return colms
}
//***********************************
public void findElement(int ele){
//Find the given element in the array.
//if the element exists also print the number of occurrences of the ele.
}
//***********************************
public void findMinElement(){
//Print the minimum value in the array and its index
}
//***********************************
public void findMaxElement(){
//Print the maximum value in the array and its index
}
//***********************************
public void printRowSum(){
//Print the sum of each row
}
//***********************************
public void printColSum(){
//Print the sum of each colms
}
//***********************************
}//end IntArray Class
public class TestArray {
public static void main(String args[]){
Scanner kb=new Scanner(System.in);
int rows, colms, min, max, ele;
System.out.println("Please enter rows and colms of the array");
rows=kb.nextInt();
colms=kb.nextInt();

System.out.println("Please enter minimun and maximum vaue of the elements");
min=kb.nextInt();
max=kb.nextInt();
IntArray A=new IntArray(rows, colms, min, max);
System.out.println("rows "+A.getRows());
System.out.println("colms "+A.getColms());
A.initializeArray();
A.printArray();
A.printRowSum();
A.printColSum();
A.findMinElement();
A.findMaxElement();
System.out.println("Please enter the element to search in the array");
ele=kb.nextInt();
A.findElement(ele);
}//end main
}
a) Given above are the two classes. IntArraya and TestArray. You have to complete the definition of the functions in the IntArray class. Do not change anything in the TestArray.
b) After completeing the IntArray class when you run the complete program following is the sample outout of the program.
//Sample output
Please enter rows and colms of the array
5 5 //(this is the input from user)
Please enter minimun and maximum vaue of the elements
1 9 //(this is the input from the user
rows 5
colms 5
5 7 3 8 2
7 8 1 8 1
1 7 1 3 8
2 4 6 6 6
7 5 4 4 7
row sum
0 25
1   25
2    20

3    24

4   27
col sum
0    22
1    31
2    15
3    29
4    24
Minimum value in the array is 1 index of minimum value is [1][2]
Maximum value in the array is 8 index of maximum value is [0][3]
Please enter the element to search in the array
6
The element 6 occurs 3 times in the array.
Process completed.

Explanation / Answer

IntArray.java

import java.util.*;
class IntArray{
int [][]A;
int rows;
int colms;
int min;
int max;
//***********************************Constructor
public IntArray(int rows, int colms, int min, int max){
   this.rows=rows;
   this.colms=colms;
   this.min=min;
   this.max=max;
}
//***********************************
public void initializeArray(){
   A=new int[rows][colms];
Random rn=new Random();
//initializes the array with random numbers in the range min to max.
for(int i=0;i<rows;i++)
{
   for(int j=0;j<colms;j++)
   {
       A[i][j]=rn.nextInt((max - min) + 1) + min;
   }
}
}
//***********************************
public void printArray()
{
   for(int i=0;i<rows;i++)
   {
       for(int j=0;j<colms;j++)
       {
           System.out.print(A[i][j]+" ");
       }
       System.out.println(" ");
   }
}
//***********************************
public int getRows(){
return rows;
}
//***********************************
public int getColms(){
return colms;
}
//***********************************
public void findElement(int ele){
//Find the given element in the array.
//if the element exists also print the number of occurrences of the ele.
   int count=0;
   for(int i=0;i<rows;i++)
   {
       for(int j=0;j<colms;j++)
       {
           if(ele==A[i][j])
           {
               count++;
           }
       }
   }
   if(count>0)
      
       System.out.println("The element "+ele+" occurs "+count+" times in the array.");
   else
       System.out.println("Element no found in the array");
}
//***********************************
public void findMinElement(){
int min=A[0][0];
for(int i=0;i<rows;i++)
{
   for(int j=0;j<colms;j++)
   {
       if(A[i][j]<min)
       {
       min=A[i][j];
       }
   }
}
System.out.println("The Minimum Element in the array is "+min);
}
//***********************************
public void findMaxElement(){
   int max=A[0][0];
   for(int i=0;i<rows;i++)
   {
       for(int j=0;j<colms;j++)
       {
           if(A[i][j]>max)
           {
           max=A[i][j];
           }
       }
   }
   System.out.println("The Maximum Element in the array is "+max);
}
//***********************************
public void printRowSum(){
int rowSum;
System.out.println("Row Sum");
for(int i=0;i<rows;i++)
{
   rowSum=0;
   for(int j=0;j<colms;j++)
   {
   rowSum+=A[i][j];
   }
   System.out.println(i+" "+rowSum);
}

}
//***********************************
public void printColSum(){
   int colSum;
   System.out.println("Col Sum");
   for(int i=0;i<rows;i++)
   {
       colSum=0;
       for(int j=0;j<colms;j++)
       {
       colSum+=A[j][i];
       }
       System.out.println(i+" "+colSum);
   }
}
//***********************************
}//end IntArray Class

_______________________________

TestArray.java

import java.util.Scanner;

public class TestArray {
   public static void main(String args[]) {
       //Scanner class object is used to read the inputs entered by the user
       Scanner kb = new Scanner(System.in);
      
       //Declaring variables
       int rows, colms, min, max, ele;
      
       //Getting the rows and column values entered by the user
       System.out.println("Please enter rows and colms of the array");
       rows = kb.nextInt();
       colms = kb.nextInt();
      
       //Getting the min and max values entered by the user
       System.out.println("Please enter minimum and maximum vaue of the elements");
       min = kb.nextInt();
       max = kb.nextInt();
      
       //Creating the IntArray class object
       IntArray A = new IntArray(rows, colms, min, max);
      
       //Displaying the rows and colums
       System.out.println("rows " + A.getRows());
       System.out.println("colms " + A.getColms());
      
       //Calling the methods on the IntArray Class object
       A.initializeArray();
       A.printArray();
       A.printRowSum();
       A.printColSum();
       A.findMinElement();
       A.findMaxElement();
      
       //Getting the element value entered by the user
       System.out.println("Please enter the element to search in the array");
       ele = kb.nextInt();
      
       //Calling the method on the IntArray class object
       A.findElement(ele);
   }// end main
}

_________________________

output:

Please enter rows and colms of the array
5 5
Please enter minimum and maximum vaue of the elements
1 9
rows 5
colms 5
9 8 1 3 1
6 8 4 6 7
1 2 8 2 2
5 5 7 7 7
2 2 9 4 3
Row   Sum
0   22
1   31
2   15
3   31
4   20
Col   Sum
0   23
1   25
2   29
3   22
4   20
The Minimum Element in the array is 1
The Maximum Element in the array is 9
Please enter the element to search in the array
6
The element 6 occurs 2 times in the array.

_____________Thank you