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:     /**
 59:      * Print out this array, treating it as a heap.
 60:      *
 61:      * @param arr the array/heap to print
 62:      */
 63:     public static void printHeap(int[] arr) {
 64:         printHeap(arr, 0, arr.length);
 65:     }

 67:     /**
 68:      * Print out part of this array, treating it as a heap.
 69:      *
 70:      * @param arr the array/heap to print from
 71:      * @param lo the lowest (inclusive) index to print
 72:      * @param hi the highest (exclusive) index to print
 73:      */
 74:     public static void printHeap(int[] arr, int lo, int hi) {
 75:         int thisLine = 0;
 76:         int width = 80;
 77:         for (int i = 0; i < arr.length; ++i) {
 78:             if (i == thisLine) {
 79:                 System.out.println();
 80:                 thisLine = thisLine * 2 + 1;
 81:                 width /= 2;
 82:             }
 83:             if (lo <= i && i < hi) {
 84:                 System.out.printf("%" + width + "d%" + width + "s", 
 85:                         arr[i], "");
 86:            } else {
 87:                 System.out.printf("%" + width + "s%" + width + "s", 
 88:                         "...", "");
 89:            }
 90:         }
 91:         System.out.println();
 92:     }

 94: }