I have two Java class files: primegen and primecheck sitting in the same directory. primegen calls a public static function from primecheck. primecheck compiles fine.
However, I receive the following compilation error in primegen:
primegen.java:31: cannot find symbol
symbol : variable primecheck
location: class primegen
} while (!primecheck.prime(primeCandidate));
^
Shouldn't Java be checking other (compiled) classes within the same directory? Does Java have a problem with primecheck being in lower-case letters (e.g. Is it treating primecheck as a variable instead of a class?)?
Update with Complete Code
Code for primegen:
import java.math.BigInteger;
import java.util.Random;
public class primegen
{
public static void main(String args[])
{
try
{
int numBits = Integer.parseInt(args[0].trim());
System.out.println(generatePrime(numBits));
}
catch (Exception e)
{
System.out.println("You must enter a positive integer number of bits.");
}
}
private static BigInteger generatePrime(int numBits) throws Exception
{
if (numBits < 1)
throw new Exception("You must enter a positive integer number of bits.");
BigInteger primeCandidate;
Random rand = new Random();
do
{
primeCandidate = new BigInteger(numBits, rand);
} while (!primecheck.prime(primeCandidate));
return primeCandidate;
}
}
Code for primecheck:
import java.math.BigInteger;
import java.util.Random;
public class primecheck
{
public static void main(String args[])
{
try
{
BigInteger primeCandidate = new BigInteger(args[0].trim());
if (prime(primeCandidate))
System.out.println("True");
else
System.out.println("False");
}
catch (Exception e)
{
System.out.println("You must enter a positive integer.");
}
}
public static boolean prime(BigInteger n) throws Exception
{
if (n.compareTo(BigInteger.ZERO) == -1)
throw new Exception("You must enter a positive integer.");
else if (n.equals(BigInteger.ZERO) || n.equals(BigInteger.ONE))
return false;
int maxIterations = 1000;
BigInteger a;
for (int i = 0; i < maxIterations; i++)
{
a = randomBase(n);
a = a.modPow(n.subtract(BigInteger.ONE), n);
if (!a.equals(BigInteger.ONE))
return false;
}
return true;
}
private static BigInteger randomBase(BigInteger n)
{
Random rand = new Random();
BigInteger a;
do
{
a = new BigInteger(n.bitLength(), rand);
} while ( !(BigInteger.ONE.compareTo(a) <= 0 && a.compareTo(n) < 0) );
return a;
}
}