I do not quite understand what you are supposed to do.
Below is an implementation of my interpretation, which may be wrong;
but I hope it can help.
The template uses comments starting with //
For clarity all my comments are surrounded by /* ... */
It turns out that my answer is longer than Wyzant limit.
I will try to provide it in several pieces.
class FactorChecker {
// n: number that'd be queried
// div: divisor
// maxFactor: maximum factor of the number (except than itself)
private int n, div;
private int maxFactor;
/* I do not know the original intention for the variable div.
I am going to use div and maxFactor as private variables
always satisfying the invariant
n = div*maxFactor
Those two variables will be changed whenever another such pair
with a larger factor is found.
The variables div and maxFactor can be modified in different
threads and therefore their accesses will have to be protected
in a critical region.
The variable n can be read by multiple threads, but will not be
modified, therefore need not be protected.
*/
public FactorChecker(int n, int div, int maxFactor) {
this.n = n;
this.div = div;
this.maxFactor = maxFactor;
/* In my usage of the variables div and maxFactor
the caller has no business setting them.
I am going to set them so as to satisfy the invariant.
Initially they will be set indicating
n = n*1
This trivial factoring will remain in case n is a prime,
or n < 2
*/
this.div = n;
this.maxFactor = 1;
/* I do not understand why your teacher's initial setting was
div=2.
I do understand why the initial setting must be
maxFactor=1;
setting it to the smallest factor facilitates increases.
*/
}
Part 2:
// Check whether the number is divisible by the multipliers of two (e.g. 2,4,6,...)
/* I do not understand what your teacher means by "multipliers of two".
The example suggests that he means "multiples of two",
i.e., "even numbers".
So I am letting checkFactor2 look for even divisors.
*/
public /* synchronized */ void checkFactor2() {
factor(0); /* Find largest factor with an even divisor */
}
// Check whether the number is divisible by the rest of the integers
/* I am interpretting "rest of the integers" as "odd numbers"
*/
public /* synchronized */ void checkFactorRest() {
factor(1); /* Find largest factor with an odd divisor */
}
Part 3:
/* I do not understand why your teacher declared checkFactor2() and
checkFactorRest() as synchronized.
It makes sense to declared a method as synchronized if it can be
executed in more than one thread.
But that is not the case here.
If you understand why they should be declared synchronized then
please go ahead and add the keywords back.
*/
Sorry, it turns out that Wyzant will let let me put in the whole thing.