Skip to content

Commit 7e0c019

Browse files
committed
2D Arraylab
0 parents  commit 7e0c019

4 files changed

Lines changed: 209 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Project exclude paths
2+
/target/

pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>ArraysLab</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.junit.jupiter</groupId>
13+
<artifactId>junit-jupiter-api</artifactId>
14+
<version>5.8.1</version>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
19+
<properties>
20+
<maven.compiler.source>8</maven.compiler.source>
21+
<maven.compiler.target>8</maven.compiler.target>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
</properties>
24+
25+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package edu.sdccd.cisc191;
2+
3+
/**
4+
* CISC191 2.3 Multidimensional Arrays - Junior
5+
* @author Ahmed Abdullahi
6+
*/
7+
8+
9+
public class ArrayLab {
10+
/**
11+
* @param args command line parameters
12+
*/
13+
public static void main(String[] args) {
14+
int[][] spreadsheet = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
15+
System.out.println("Max: " + max(spreadsheet));
16+
System.out.println("Sum of row 0: " + rowSum(spreadsheet, 0));
17+
System.out.println("Sum of col 0: " + columnSum(spreadsheet, 0));
18+
int[] rowSums = allRowSums(spreadsheet);
19+
for (int row = 0; row < rowSums.length; row++) {
20+
System.out.println("Sum of row " + row + ": " + rowSums[row]);
21+
}
22+
23+
}
24+
25+
/**
26+
* @param a 2D array
27+
* @return the maximum value in the 2d parameter array a
28+
*/
29+
public static int max(int[][] a) {
30+
31+
// Setting the max to row 0 column 0
32+
int max = a[0][0];
33+
34+
//if there exists a number greater than the current max a[0][0]
35+
// then set it as the new max
36+
for (int i = 0; i < a.length; i++) {
37+
for (int j = 0; j < a[i].length; j++) {
38+
if (a[i][j] > max) max = a[i][j];
39+
}
40+
}
41+
42+
return max;
43+
}
44+
45+
/**
46+
* @param a 2D array
47+
* @param x row index
48+
* @return the sum of the elements in Row x of a
49+
*/
50+
public static int rowSum(int[][] a, int x) {
51+
52+
// totalSum will hold the total sum for row x of a
53+
int totalSum = 0;
54+
55+
//loop that prints out the row sum
56+
//a[x].length reads length of the row and the loop reiterates until it reaches the length
57+
for (int i = 0; i < a[x].length; i++) {
58+
totalSum += a[x][i];
59+
}
60+
return totalSum;
61+
}
62+
63+
/**
64+
* @param a 2D array
65+
* @param x column index
66+
* @return the sum of the elements in Column x of a (careful with rows of different lengths!)
67+
*/
68+
public static int columnSum(int[][] a, int x) {
69+
70+
//totalSum will hold the total sum for the column x of a
71+
int totalSum = 0;
72+
73+
//loop that will get the total sum for each column x of a
74+
for (int i = 0; i < a.length; i++) {
75+
if(a[i].length > x )
76+
totalSum += a[i][x];
77+
78+
}
79+
80+
return totalSum;
81+
}
82+
83+
/**
84+
* @param a 2D array
85+
* @return calculates the row sum for every row and returns each of the values in an array. Index i of the return array contains the sum of elements in row i.
86+
*/
87+
public static int[] allRowSums(int[][] a) {
88+
89+
//array rowSums made to have same length as a
90+
int[] rowSums = new int[a.length];
91+
92+
//for loops to compute the sum of each row
93+
for(int i = 0; i < a.length; i++){
94+
for(int j = 0; j< a[i].length; j++){
95+
rowSums[i] += a[i][j];
96+
}
97+
}
98+
return rowSums;
99+
100+
}
101+
}
102+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package edu.sdccd.cisc191;
2+
3+
import org.junit.jupiter.api.BeforeAll;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.TestInstance;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9+
10+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
11+
public class ArrayLabTest {
12+
13+
// example arrays for testing
14+
private int[][] basic, allneg, nonsquare;
15+
16+
/**
17+
* Sets up the test fixture with some arrays to test.
18+
* This method is called before every test case method.
19+
*/
20+
@BeforeAll
21+
public void setUp() {
22+
basic = new int[][] { {1,2,3}, {4,5,6}, {7,8,9} };
23+
allneg = new int[][] { {-10,-12,-3}, {-4,-5,-6,-8}, {-7,-8} }; //all neg and ragged
24+
nonsquare = new int[][] { {1,2,3}, {4,5}, {6,7,8,9} };
25+
}
26+
27+
/**
28+
* Test max is found correctly (last element in the search)
29+
*/
30+
@Test
31+
public void testMaxNormal() {
32+
assertEquals(9,ArrayLab.max(basic));
33+
}
34+
35+
/**
36+
* Test max correct when all vals are negative
37+
*/
38+
@Test public void testMaxAllNeg() {
39+
assertEquals(-3,ArrayLab.max(allneg));
40+
}
41+
42+
/**
43+
* Test row sum calculated correctly including for nonsquare arrays
44+
*/
45+
@Test public void testRowSum() {
46+
assertEquals(6, ArrayLab.rowSum(basic, 0));
47+
assertEquals(15, ArrayLab.rowSum(basic, 1));
48+
assertEquals(24, ArrayLab.rowSum(basic, 2));
49+
assertEquals(30, ArrayLab.rowSum(nonsquare, 2));
50+
}
51+
52+
/**
53+
* Test column sum calculated correctly for standard cases
54+
*/
55+
@Test public void testColumnSum() {
56+
assertEquals(12, ArrayLab.columnSum(basic, 0));
57+
assertEquals(15, ArrayLab.columnSum(basic, 1));
58+
assertEquals(18, ArrayLab.columnSum(basic, 2));
59+
}
60+
61+
62+
/**
63+
* Test column sum calculated correctly for nonsquare arrays
64+
* This checks for sum of incomplete columns (from ragged arrays)
65+
*/
66+
@Test public void testColumnSumRagged() {
67+
assertEquals(11, ArrayLab.columnSum(nonsquare, 2));
68+
assertEquals(9, ArrayLab.columnSum(nonsquare, 3));
69+
}
70+
71+
/**
72+
* Checks array of row sums correctly calculated
73+
*/
74+
@Test public void testAllRowSums() {
75+
int[] expected = new int[] {6,15,24};
76+
int[] actual = ArrayLab.allRowSums(basic);
77+
assertArrayEquals(expected, actual);
78+
}
79+
80+
}

0 commit comments

Comments
 (0)