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

Preliminaries Create a project submission folder, in the form Lab#FirstNameLastN

ID: 3839954 • Letter: P

Question

Preliminaries

Create a project submission folder, in the form Lab#FirstNameLastName. For example, mine would be Lab8DavidLiu

Create an Eclipse Java Project.

Add a Java class named ArrayOps2D to your project

Add a Java class called Main to your project

Exercises

Part 1) – The ArrayOps2D class – 18 Points

ArrayOps2D holds the methods we will use to interact with 2DArrays. All of the methods will be public static. All methods should be written to use regular 2D arrays and ragged 2D arrays.

getTotal. This method should take a 2D array of doubles as an argument and return the sum of all the values in the 2D array.

getAverage. This method should take a 2D array of doubles as an argument and return the average of all the values. (Hint: you will need the sum and the total count of elements).

getRowTotal. This method should take a 2DArray of doubles and an int as arguments. It should return the sum of the row specified by the int (if the int is 1, then the row at index 1, like arr[1]).

findHighestValue. This method should accept a 2D array of doubles as an argument. It should find the largest value in the 2D array. Rather than returning the value, it should print out where it is located. So if the highest value is 25.5 in row 2 and column 3, it should print:

“The highest value is 25.5, from row 2, column 3”

This means you will need to keep track of the row and column (the indices being used in your double for loop), as well as the highest value.

findHighestValueInRow. This method should accept a 2D array of doubles and an int as arguments. It should find the highest value in the row specified by the int and then return the index where it occurred.

sequentialSearch2D. This method should accept a 2D array of doubles and a double as arguments. It should search the array for the value in the double argument. When it finds the value, it should print:

“Value I found, at x, y!” where I should be the value and x and y the row and column. So if it finds value 5.0 in row 1, column 6, it should print:

“Value 5.0 found, at 1,6!”

Part 2) Main – 2 Points

The second part of the lab is focused on testing the ArrayOps2D class.

Make a 2D array and a 2D ragged array. They can be whatever you wish, but could be something like:

double[][] simple2D = {{3,4,5}, {6,7,8}, {9,9,1}};

double[][] ragged2D = new ragged2D[2][];

ragged2D[0] = {1,2,3,3,3,3};

ragged2D[1] = {3,4,5,8,9};

ragged2D[2] = {8,9,1,};

Call all methods, using both simple2D and ragged2D. For all methods that return something, you should print the result. It is recommended you do something like this for easy reading and testing:

2D Results

---

Print out the results for the 2D array

---

Ragged Results

---

Print out the results for the ragged array

While it is not required, it is recommended that you use a few different array set ups, just for the sake of testing correctness.

Explanation / Answer


import java.io.*;
import java.util.*;
public class ArrayOps2D {
static Scanner sc=new Scanner(System.in);
   public static void main(String[] args) {
      
       // TODO Auto-generated method stub
      
       double[][] simple2D = {{3,4,5}, {6,7,8}, {9,9,1}};
       //double[][] ragged2D = new double[2][];
      
      
   double []   ragged2D1 = {1,2,3,3,3,3};
   double []   ragged2D2 = {3,4,5,8,9};
   double[][]   ragged2D={ragged2D1,ragged2D2};
   double total=getTotal(simple2D);
   System.out.println("total="+total);
  
/*   for(int i=0;i<simple2D.length;i++){
       for(int j=0;j<simple2D[i].length;j++)
           System.out.print(simple2D[i][j]);
       System.out.println();
   }*/
   double average= getAverage(simple2D);
   System.out.println("Average="+average);
   System.out.println("enter row number to get sum:");
   int rownumber=sc.nextInt();
   double rowsum=rowtotal(simple2D,rownumber);
   System.out.println("rowsum="+rowsum);
   findHighestValue(simple2D);
   System.out.println("enter a row number to get highest value:");
   int rowhighest=sc.nextInt();
   findHighestValueinRow(simple2D,rowhighest);
   sequentioasearch(simple2D);
   }
   private static void sequentioasearch(double[][] arr) {
       System.out.println("enter element to search in array:" );
       double ele=sc.nextDouble();
      
       for(int i=0;i<arr.length;i++){
    for(int j=0;j<arr[i].length;j++){
        double getd=arr[i][j];
        if(getd==ele)
            System.out.println("the element is present at row "+i+" and colum "+j);
          
    }
}
      
   }
   private static void findHighestValueinRow(double[][] arr, int rownum) {
       double max=0;
       int col=0;
       int row=0;
       for(int i=rownum;i<=rownum;i++){
           for(int j=0;j<arr[i].length;j++){
               double value=arr[i][j];
               if(value>max){
                   max=value;
                   col=j;
                   row=i;
                  
               }
           }
       }
      
       System.out.println("Highest value is "+max+" at column "+col);
      
   }
   private static void findHighestValue(double[][] arr) {
       double max=0;
       int row=0,col=0;
       for(int i=0;i<arr.length;i++){
           for(int j=0;j<arr[i].length;j++){
               double value=arr[i][j];
               if(arr[i][j]>max){
                   max=arr[i][j];
                   row=i;
                   col=j;
                  
               }
           }
       }
       System.out.println("Highest element is "+max+" found at row="+row+" and colum is="+col);
   }
   private static double rowtotal(double[][] arr, int rownumber) {
       double rowsum=0;
       for(int i=rownumber;i<=rownumber;i++){
           for(int j=0;j<arr[i].length;j++){
               rowsum+=arr[i][j];
           }
       }
      
       return rowsum;
      
   }
   private static double getAverage(double[][] arr) {
       double average=0;
       double total= getTotal(arr);
       average=total/arr.length;
       return average;
      
      
   }
   public static double getTotal(double arr[][]){
       double total=0;
       for(int i=0;i<arr.length;i++){
           for(int j=0;j<arr[i].length;j++){
               total+=arr[i][j];
           }
       }
      
      
       return total;
  
   }
  
}

output:
total=52.0
Average=17.333333333333332
enter row number to get sum:
0
rowsum=12.0
Highest element is 9.0 found at row=2 and colum is=0
enter a row number to get highest value:
1
Highest value is 8.0 at column 2
enter element to search in array:
7
the element is present at row 1 and colum 1

output:
if we give ragged array as input:
total=52.0
Average=22.0
enter row number to get sum:
1
rowsum=29.0
Highest element is 9.0 found at row=1 and colum is=4
enter a row number to get highest value:
1
Highest value is 9.0 at column 4
enter element to search in array:
1
the element is present at row 0 and colum 0