Preliminaries Create a project submission folder, in the form Lab#FirstNameLastN
ID: 3843299 • 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
package pratice;
import java.util.Arrays;
public class ArrayOps2D {
public static double getTotal(double siple2D[][]) {
double total = 0;
for (int i = 0; i < siple2D.length; i++) {
for (int j = 0; j < siple2D[i].length; j++) {
total = total + siple2D[i][j];
}
}
return total;
}
public static double findHighestValue(double simple2D[][]) {
double largeValue = 0;
int row = 0;
int col = 0;
for (int i = 0; i < simple2D.length; i++) {
for (int j = 0; j < simple2D[i].length; j++) {
if (simple2D[i][j] > largeValue) {
largeValue = simple2D[i][j];
row = i;
col = j;
}
}
}
System.out.println("Max value location in a matrix:row:" + row + ",col:" + col);
return largeValue;
}
public static double getAverage(double simple2D[][]) {
double sum = 0;
int noOfElements = 0;
double avg = 0;
for (int i = 0; i < simple2D.length; i++) {
for (int j = 0; j < simple2D[i].length; j++) {
sum += simple2D[i][j];
noOfElements++;
}
}
if (noOfElements != 0) {
avg = sum / noOfElements;
}
return avg;
}
public static double getRowTotal(double simple2D[][], int rowCount) {
double rowTotal = 0;
double[] row = simple2D[rowCount];
for (int i = 0; i < row.length; i++) {
rowTotal += row[i];
}
return rowTotal;
}
public static int findHighestValueInRow(double[][] simple2D, int rowCount) {
double[] row = simple2D[rowCount];
double max = 0;
int index = 0;
for (int i = 0; i < simple2D.length; i++) {
for (int j = 0; j < simple2D[i].length; j++) {
if (simple2D[i][rowCount] > max) {
max = simple2D[i][rowCount];
index = i;
}
}
}
return index;
}
public static void sequentialSearch2D(double simple2D[][], double value) {
for (int x = 0; x < simple2D.length; x++) {
for (int y = 0; y < simple2D[x].length; y++) {
if (simple2D[x][y] == value) {
System.out.println();
System.out.println("Value " + value + " found, at" + x + "," + y + "!");
}
}
}
}
}
//-----------------------------
public class Main{
//Note Matrix index starts from 0 0
public static void main(String[] args) {
double[][] simple2D = { { 10, 2, 3 }, { 11, 1, 0 }, { 1, 1, 9 } };
double[][] ragged2D = new double[2][];
ragged2D[0][0]=1.0;
ragged2D[0][0]=1.0;
ragged2D[0][0]=1.0;
System.out.println("Sum of all elements:" + getTotal(simple2D));
System.out.println("Average elements:" + getAverage(simple2D));
System.out.println("get Row Total:" + getRowTotal(simple2D, 0));
System.out.println("Max value in matrix:" + findHighestValue(simple2D));
System.out.println("HighestValueInRowindex:" + findHighestValueInRow(simple2D, 0));
sequentialSearch2D(simple2D, 0);
}
}