Source of PrintTrace.java


  1: import java.util.Scanner;
  2: import java.util.Arrays;

  4: /**
  5:  * A class for printing partially sorted arrays.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class PrintTrace {

 11:     /**
 12:      * Print the elements of an array in a nice table.
 13:      *
 14:      * @param arr   the array to print
 15:      */
 16:     public static void printArray(int[] arr) {
 17:         printArray(arr, 0, arr.length);
 18:     }

 20:     /** number of numbers printed per line */
 21:     private static final int PER_LINE = 8;

 23:     /** number of spaces per number */
 24:     private static final int SPACES = 80 / PER_LINE;

 26:     /** format string for numbers */
 27:     private static final String NUM_FORMAT = "%" + SPACES + "d";

 29:     /** format string for place fillers */
 30:     private static final String STR_FORMAT = "%" + SPACES + "s";

 32:     /**
 33:      * Print an array showing only the numbers in one part of it.
 34:      * Numbers before the starting point/after the ending point
 35:      * are replaced with "filler".
 36:      *
 37:      * @param arr   the array to print
 38:      * @param lo    positions less than lo are "filled"
 39:      * @param hi    positions hi and greater are "filled"
 40:      */
 41:     public static void printArray(int[] arr, int lo, int hi) {
 42:         int thisLine = 0;
 43:             for (int i = 0; i < arr.length; i++) {
 44:            if (thisLine == PER_LINE) {
 45:               System.out.println();
 46:               thisLine = 0;
 47:            }
 48:            if (lo <= i && i < hi) {
 49:               System.out.printf(NUM_FORMAT, arr[i]);
 50:            } else {
 51:               System.out.printf(STR_FORMAT, "...");
 52:            }
 53:            ++thisLine;
 54:             }
 55:         System.out.println();
 56:     }

 58: }