Checkpoint B

A class for making a list of prime numbers.

Number Theory

We are writing a bunch of number-related functions. Even though many of them have nothing to do with prime numbers, the class is called Prime.

Fields

  • int nprimes
  • Array primes

Accessor methods

  • method int getNPrimes(): How many primes are in the array? This should be stored in the nprimes variable.
  • method int getPrime(int k): Get the kth prime, starting with getPrime(0) = 2.

Functions

  • function boolean divides(int k, int n): True when k divides n.
  • function int isqrt(n): Returns the greatest integer k with k*k <= n.
  • function int countDivisors(int n): Returns the number of divisors of n. countDivisors(7) = 2.
  • function Array divisors(int k): An Array containing the divisors of k in ascending order.

More Methods

  • constructor Prime new(int k): Create a Prime object. The object should contain:
    1. an integer: the number of primes found (this will be k);
    2. an array of integers that is filled with the first k prime numbers. When k=2 the array contains only the numbers 2 and 3.
  • method boolean isKnownComposite(int n): True when the number is divisible by some number in the primes list, but is not itself prime.

Testing

class Prime {
  field int nprimes
  field Array primes;

  method int getNPrimes() { return nprimes; }
  method int getPrime(int k) { return primes[k]; }
  // ...
}

Common Issues

  • Constructors need to return this.
Last modified March 18, 2025: Introduction to Jack exercises. (0b3bf06)