// FigurePrinter.java // A solution to CS 10, assignment 4, part 1, Fall 2008 // cmc, updated 11/1/08 /** Generic figure printer. Maintains a specific char to use for printing the figure, and an int number of spaces to offset the figure from the left margin. */ public class FigurePrinter implements Comparable { private char symbol; private int offset; /** Constructor assigns character, and offset amount. @param symbol the character symbol to represent the figure. @param offset the offset (leading spaces) from the left margin. */ public FigurePrinter(char symbol, int offset) { this.symbol = symbol; this.offset = offset; } /** No-argument constructor sets symbol to '*' and offset to 0. */ public FigurePrinter() { this('*', 0); } /** Accessor for the symbol this object uses to print figures. @return the character symbol. */ public char getSymbol() { return symbol; } /** Sets the symbol this object uses to print figures. @param newSymbol the new character to use for printing. */ public void setSymbol(char newSymbol) { symbol = newSymbol; } /** Accessor for the offset this object uses to print figures. @return the offset amount. */ public int getOffset() { return offset; } /** Sets the offset - number of spaces from the left margin. @param newOffset the amount by which to offset the figure. */ public void setOffset(int newOffset) { offset = newOffset; } /** Prints the figure to System.out.
Note: this class just prints one symbol, not an actual figure. Subclasses are expected to override this method to print more meaningful figures. */ public void print() { shift(); System.out.println(symbol); } /** Prints the figure after a specified number of newlines. @param lines the number of lines to skip before printing. */ public void printAt(int lines) { for (int i = 0; i < lines; i++) System.out.println(); print(); } /** Checks if this object is identical to the other object. @param other the other object. @return true if the other object is a FigurePrinter and its symbol and offset both match this object's symbol and offset. */ public boolean equals(Object other) { if (other == this) return true; if (other == null || other.getClass() != this.getClass()) return false; FigurePrinter o = (FigurePrinter)other; return symbol == o.symbol && offset == o.offset; } /** Compares this figure printer to other figure printer, in terms of offset. @param other another FigurePrinter object. @return 0 if offset of this object equals other offset,
> 0 if offset of this object > other offset, or
< 0 if offset of this object < other offset. */ public int compareTo(FigurePrinter other) { return offset - other.offset; } /** Utility method prints the offset number of spaces, but does not print a newline character. Note this method is "protected" - it may be used by subclasses. */ protected void shift() { spaces(offset); } /** Utility method prints a specified number of spaces, but not a newline character. Note this method is "protected" - it may be used by subclasses. @param n the number of spaces to print. */ protected void spaces(int n) { for (int i = 0; i < n; i++) System.out.print(' '); } }