CPEN 221 / Fall 2017
- Can you solve the provided question in one hour?
- Skeleton code can be obtained by cloning this repository. Add JUnit to your build path in Eclipse.
The skeleton source code for this question is in the package
additionClosure
. You have to implement the methods isClosed.
You are given a List
of Integer
s, listInt
, and another integer n
. We will define closure under addition for listInt
to mean that for two distinct values of i
and j
, (listInt[i]+listInt[j]) % n
is in listInt
or k * n + (listInt[i]+listInt[j]) % n
(for some int k
) is in listInt
.
Your goal is to determine if a given List
of Integer
s is closed under addition for some modulus n
. You will implement a method, isClosed, that will return true
if a List
is closed under addition for a given n
and false
otherwise.
-
input list: [-1, 0, 1];
n
= 2- Returns:
true
(-1 + 0) % 2 = -1; (-1 + 1) % 2 = 0, (0 + 1) % 2 = 1
.
- Returns:
-
input list: [-2, 3];
n
= 4- Returns:
false
- Returns:
-
input list: [1, 1, 0];
n
= 2- Returns:
true
- Returns:
-
input list: [1, 1];
n
= 3- Returns:
false
- Returns:
-
input list: [1, 1, 2];
n
= 2- Returns:
true
- Returns:
-
input list: [0, 1, 1, 2];
n
= 2- Returns:
true
- Returns:
-
input list: [0, 1, 100, 50, 50],
n
= 100- Return:
false
- Return:
-
input list: [0, 1, 100, 50, 50],
n
= 50- Return:
true
- Return: