diff --git a/PrintPrimes.java b/PrintPrimes.java index c4be32e..73874a9 100644 --- a/PrintPrimes.java +++ b/PrintPrimes.java @@ -1,17 +1,15 @@ +//A" - Dominic Daniel Kent - 260491325 + public class PrintPrimes { int numberOfPrimes; - int RR; - int CC; - int WW; - int ORDMAX; + int rowsPerPage; + int columnsPerPage; int listOfPrimes[]; - public PrintPrimes(int numberOfPrimes, int RR, int CC, int WW, int ORDMAX) { + public PrintPrimes(int numberOfPrimes, int rowsPerPage, int columnsPerPage) { this.numberOfPrimes = numberOfPrimes; - this.RR = RR; - this.CC = CC; - this.WW = WW; - this.ORDMAX = ORDMAX; + this.rowsPerPage = rowsPerPage; + this.columnsPerPage =columnsPerPage; this.listOfPrimes = new int[numberOfPrimes + 1]; } @@ -19,21 +17,16 @@ public PrintPrimes(int numberOfPrimes, int RR, int CC, int WW, int ORDMAX) { public static void main(String[] args) { PrintPrimes printPrimes = new PrintPrimes(300, 50, 4, 10, 30); printPrimes.calculatePrimes(); - printPrimes.printPrimes(); + printPrimes.print(); } public void calculatePrimes() { - /* Two is the only even prime. All other prime numbers are odd. - * To simplify the code, we simply add 2 as a prime number, and - * delegate the task of finding all odd prime numbers to a helper - * function. - */ listOfPrimes[1] = 2; calculateOddPrimes(); } private void calculateOddPrimes() { - boolean JPRIME; + boolean isPRIME; int N; int MULT[] = new int[ORDMAX + 1]; @@ -51,34 +44,34 @@ private void calculateOddPrimes() { } N = 2; JPRIME = true; - while (N < ORD && JPRIME) { + while (N < ORD && isPRIME) { while (MULT[N] < J) MULT[N] = MULT[N] + listOfPrimes[N] + listOfPrimes[N]; if (MULT[N] == J) - JPRIME = false; + isPRIME = false; N = N + 1; } - } while (!JPRIME); + } while (!isPRIME); listOfPrimes[primesFoundSoFar] = J; } } - public void printPrimes() { + public void print() { int PAGENUMBER = 1; int PAGEOFFSET = 1; while (PAGEOFFSET <= numberOfPrimes) { System.out.println("The First " + numberOfPrimes + " Prime Numbers --- Page " + PAGENUMBER); System.out.println(""); - for (int ROWOFFSET = PAGEOFFSET; ROWOFFSET < PAGEOFFSET + RR; ROWOFFSET++){ - for (int C = 0; C < CC;C++) - if (ROWOFFSET + C * RR <= numberOfPrimes) - System.out.format("%10d", listOfPrimes[ROWOFFSET + C * RR]); + for (int ROWOFFSET = PAGEOFFSET; ROWOFFSET < PAGEOFFSET + rowsPerPage; ROWOFFSET++){ + for (int column = 0; column < columnsPerPage ; column++) + if (ROWOFFSET + column * rowsPerPage <= numberOfPrimes) + System.out.format("%10d", listOfPrimes[ROWOFFSET + column * rowsPerPage]); System.out.println(""); } System.out.println("\f"); PAGENUMBER = PAGENUMBER + 1; - PAGEOFFSET = PAGEOFFSET + RR * CC; + PAGEOFFSET = PAGEOFFSET + rowsPerPage * columnsPerPage; } } }