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

2D Arrays Lab 1 Basic Operations In Java jGrasp follow the directions carefully

ID: 3790039 • Letter: 2

Question

2D Arrays Lab 1 Basic Operations

In Java jGrasp follow the directions carefully

Write an Array2D class that will have a two-dimensional array of ints of any given size as an instance variable.

Write a constructor that accepts the number of rows and the number of columns in the array. You can assume that the values passed in for rows and cols is greater than zero.

          public Array2D(int rows, int cols)

Write a printArray method that prints out all the elements in the 2D array in matrix form.

public void printArray()

Write an initialize method that will fill the array with random numbers from a given range.

public void initialize(int low, int high)

Write an Array2DTester class.

Create a 3 x 3 integer array and then initialize the array with elements from -9 to 9 (duplicates allowed). Print out the array.

Create a 7 x 7 integer array and then initialize the array with elements from -9 to 9 (duplicates allowed). Print out the array.

Add the following methods to the Array2D class. Test incrementally (i.e. you should test each method after it is written and fix any errors before writing the next method.):

return the maximum element. public int max()

return minimum elements. public int min()

return the element that occurs most frequently. If there is more than one value with the greatest frequency, return the lowest number (mode) public int mode()

return the average of all the elements in the array. public double average()

return the sum of any row of elements. You can assume that the row passed in is valid                   public int sumRow(int row)

return the sum of any column of elements. public int sumCol(int col)

Explanation / Answer

Array2DTester.java


public class Array2DTester {

   public static void main(String[] args) {
       Array2D a = new Array2D(3,3);
       a.initialize(-9,9);
       a.printArray();
       System.out.println();
       System.out.println("Max Value: "+a.max());
       System.out.println("Min Value: "+a.min());
       System.out.println("Average: "+a.average());
       System.out.println("Row Sum: "+a.sumRow(2));
       System.out.println("Col sum: "+a.sumCol(2));
       System.out.println("Mode: "+a.mode());
   }

}

Array2D.java

import java.util.HashMap;
import java.util.Random;


public class Array2D {
   private int array[][];
   public Array2D(int rows, int cols){
       array = new int[rows][cols];
   }
   public void printArray(){
       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();
       }
   }
  
   public void initialize(int low, int high){
       Random r = new Random();
       for(int i=0; i<array.length; i++){
           for(int j=0;j <array[i].length; j++){
               array[i][j] = r.nextInt(high-low)+low;
           }
       }
   }
  
   public int max(){
       int maxValue = array[0][0];
       for(int i=0; i<array.length; i++){
           for(int j=0;j <array[i].length; j++){
               if(maxValue < array[i][j]){
                   maxValue = array[i][j];
               }
           }
       }
       return maxValue;
   }
  
   public int min(){
       int mminValue = array[0][0];
       for(int i=0; i<array.length; i++){
           for(int j=0;j <array[i].length; j++){
               if(mminValue > array[i][j]){
                   mminValue = array[i][j];
               }
           }
       }
       return mminValue;
   }
  
   public double average(){
       int sum = 0;
       for(int i=0; i<array.length; i++){
           for(int j=0;j <array[i].length; j++){
               sum = sum + array[i][j];
           }
       }
       return sum;
   }
   public int sumRow(int row){
       int sum = 0;
       for(int j=0;j <array[row].length; j++){
           sum = sum + array[row][j];
       }  
       return sum;
   }
   public int sumCol(int col){
       int sum = 0;
       for(int j=0;j <array.length; j++){
           sum = sum + array[j][col];
       }  
       return sum;
   }
   public int mode()
{
      
       HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
int max=1,temp = array[0][0];
for(int i=0;i<array.length;i++)
{
   for(int j=0;j<array[i].length;j++){
if(map.get(array[i][j])!=null)
{int count=map.get(array[i][j]);
count=count+1;
map.put(array[i][j],count);
if(count>max)
{max=count;
temp=array[i][j];}
}
else
{map.put(array[i][j],1);}
}
}
return temp;
}

  
}

Output:

5 6 -1
-7 -5 -6
-5 7 4

Max Value: 7
Min Value: -7
Average: -2.0
Row Sum: 6
Col sum: -3
Mode: -5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote