aboutsummaryrefslogtreecommitdiff
path: root/challenge-145/mohammad-anwar/java/theweeklychallenge/DotProduct.java
blob: 3bd12a93b9224a752978302ac24cf2d583fd509c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package theweeklychallenge;

/*

Week 145:

    https://theweeklychallenge.org/blog/perl-weekly-challenge-145

Task #1: Dot Product

    You are given 2 arrays of same size, @a and @b.

    Write a script to implement Dot Product.

*/

import junit.framework.TestCase;
import static junit.framework.Assert.*;

public class DotProduct extends TestCase {

    public static void main(String[] args) {
        junit.textui.TestRunner.run(theweeklychallenge.DotProduct.class);
    }

    public void testDotProduct() {
        assertEquals(32, dotProduct(new int[] {1,2,3}, new int[] {4,5,6}));
    }

    public static int dotProduct(int[] a, int[] b) {

        int dp = 0;
        if (a.length == b.length) {
            for(int i = 0; i < a.length; i++) {
                dp += a[i] * b[i];
            }
        }

        return dp;
    }
}