publicclassPrimePagePrinter{ int pageNumber = 1; //PAGENUMBER int pageOffset = 1; //PAGEOFFSET int rowLength = 50; //RR int colLength = 4;//CC int M = 1000; finalint WW = 10; finalint ORDMAX = 30; int P[];
publicclassPrimePagePrinter{ int pageNumber = 1; //PAGENUMBER int pageOffset = 1; //PAGEOFFSET int rowLength = 50; //RR int colLength = 4;//CC int M = 1000; finalint WW = 10; finalint ORDMAX = 30; int P[];
privateint rowPerPage = 50; //RR clearer than rowLength privateint colPerPage = 4; //CC clearer than colLength privateint numbersPerPage; // in printPrimes(), rowPerPage * colPerPage numbers will be printed // final int WW = 10; // final int ORDMAX = 30; not used in print // why name does not contain 'prime': the class defines the context of the code // P => int data[]; // 但是可以作为外界参数传入,同一个printer可以在生命周期内打印多个不同的数组,因此不需要作为类的成员变量
// has default, also has way to change publicSetPrintStream(PrintStream printStream){ this.printStream = printStream; }
publicprint(int[] data){ // <= M & M = data.length-1 => < data.length // pageOffset => firstIndexOnPage; 是每一行的开头, // 类似内存的定位方式,再加上偏移量才是数据的最终位置,所以pageOffset的隐藏含义是每页的开头 int pageNumber = 1; for (int firstIndexOnPage = 0; firstIndexOnPage < data.length; firstIndexOnPage += numbersPerPage) { // 计算出每页最后一个坐标,不应当超过数组最大下标 int lastIndexOnPage = Math.min(data.length-1, firstIndexOnPage + numbersPerPage-1); printHeader(pageHeader,pageNumber); printPage(firstIndexOnPage, lastIndexOnPage, data); // really for page to end printStream.println("\f"); pageNumber += 1; } // while (this.pageOffset < data.length) { // this.printHeader(); // this.printPage(); // this.pageOffset += this.numbersPerPage; // this.pageNumber += 1; // } }
// change to private because it is for print privateprintHeader(String pageHeader, int pageNumber){ // System.out.println("The First " + this.data.length +1 + // " Prime Numbers --- Page " + this.pageNumber); // System.out.println(""); printStream.println(pageHeader + " --- Page " + pageNumber); printStream.println(""); } privateprintPage(int firstIndexOnPage, int lastIndexOnPage,int[] data){ // for (int firstIndex = this.pageOffset; firstIndex<this.pageOffset + this.rowLength; firstIndex += 1) { // // this lastIndex is for all the page so should be seperated from firstIndex // int lastIndex = Math.min(data.length-1, firstIndex + ((this.colPerPage -1) * this.rowLength)); // // no, actually (this.colPerPage -1) * this.rowLength is first index of last row // this.printPrimes(firstIndex, lastIndex, data); 此处还是没有把页和行分开 // System.out.println("\f"); // } // 计算出最后一行的开头坐标,作为每一行开头坐标的 最大值 int firstIndexOfLastRowOnPage = firstIndexOnPage + rowPerPage - 1; for (int firstIndexOnRow = firstIndexOnPage; firstIndexOnRow <= firstIndexOfLastRowOnPage; firstIndexOnRow+=1) { printRow(firstIndexOnRow, lastIndexOnPage, data); printStream.println(""); } }
// last index should be smaller than data length // from printPrims => printRow privateprintRow(int firstIndexOnRow, int lastIndexOnPage, int[] data){ // 首尾已经被确定,打印的就是指定的 原代码中的 ROWOFFSET + C * this.rowPerPage for (int index = firstIndex; index<=lastIndex; index += this.rowLength) System.out.format("%10d", this.data[index]); System.out.println(""); } }
printRow函数的另一种实现:
1 2 3 4 5 6 7 8 9 10 11
privateprintRow(int firstIndexOnRow, int lastIndexOnPage, int[] data){ // 含义更清晰 C => column for (int column = 0; column < colPerPage; column +=1) { //firstIndexOnRow => x, column => y // index就是原代码里 ROWOFFSET + C * this.rowPerPage int index = firstIndexOnRow + column * rowPerPage; if (index < lastIndexOnPage) printStream.format("%10d", this.data[index]); // or if index > lastIndexOnPage return } }