/** * This class demonstrates an clear and simple use of comments * with a single method that displays primes. * @author bederson */ public class CommentsGood { /** * displayPrimes prints the prime numbers between 2 and the parameter * @param maxNumber The largest possible prime number to display */ public void displayPrimes(int maxNumber) { int[] array; // An array of integers used to compute primes // Size of array is maxNumber + 1 instead of maxNumber // because we want to map a number x with array entry array[x] array = new int[maxNumber + 1]; // Initialize array starting at 2 for (int i=2; i<=maxNumber; i++) { array[i] = i; } // Compute the primes by removing multiple of primes (those set // to zero). The limit could have been maxNumber but is more efficient // to go to up to the sqrt(maxNumber). for (int i=2; i<=(int)Math.sqrt(maxNumber); i++) { for (int j=2*i; j <= maxNumber; j+=i) { array[j] = 0; } } // Display the primes for (int i=2; i<=maxNumber; i++) { if (array[i] != 0) { System.out.println(array[i]); } } } /** * A simple class tester * @param args Unused */ static public void main(String[] args) { CommentsGood good = new CommentsGood(); good.displayPrimes(20); // Simple test case } }