import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Routines that make printing a lot more fun.
 *
 * @author Samuel A. Rebelsky
 * @author Yvonne Palm
 * @author Alex Leach
 * @author Jonathan Wellons
 */
public class PrintHelper
{
  /**
   * Lots and lots of spaces.
   */
  public static String spaces = "                                       ";

  /**
   * Print an object to a PrintWriter, centered on a line of a specific
   * width.
   */
  public static void center(PrintWriter out, Object value, int width)
  {
    // An idea: Put width/2 spaces on the left and width-1-width/2 spaces 
    // on right.
    // A problem: If the object requires more than one space, you're screwed.
    // Revised idea: 
    //   (1) Determine the length of "the string". 
    //   (2) Subtract that length from width.
    //   (3) Follow the earlier strategy
    // A concern: What if the string is longer than the width?
    //   (a) Throw an exception.
    //   (b) Truncate the string. [Dictatorial selection]
    //   (c) Just print the string as is.  [Popular Vote]

    // Convert to a string.
    String printMe = value.toString();
    // Determine its length.
    int stringlength = printMe.length();
    // Special case: Too big!
    if (stringlength >= width) {
      out.println(printMe.substring(0,stringlength));
    }
    else {
      // Calculate left and right indents
      width = width - stringlength;
      int leftIndent = width/2;
      int rightIndent = width - leftIndent;
      // Make sure we have enough spaces.
      while (spaces.length() < rightIndent) {
        spaces = spaces + spaces;
      }
      out.println(spaces.substring(0,leftIndent) 
                  + printMe 
                  + spaces.substring(0,rightIndent));
    }
  } // center(PrintWriter, Object, int)

  /**
   * Test some of the methods in this class.
   */
  public static void main(String[] args)
  {
    // Prepare input and screenput.
    BufferedReader keyboard = 
      new BufferedReader(new InputStreamReader(System.in));
    PrintWriter screen = new PrintWriter(System.out, true);

    MyVector vec = new MyVector();
    vec.add("a");
    vec.add("b");
    vec.add("c");

    center(screen, "Hello", 60);
    center(screen, new Rational(16,7), 60);
    center(screen, vec, 60);
    center(screen, "*", 60);
    center(screen, "**", 60);
    center(screen, "***", 60);
    center(screen, "****", 60);
    center(screen, "*****", 60);
    center(screen, "******", 60);
    center(screen, "*******", 60);

    // That's it, we're done.
    System.exit(0);
  } // main(String[])

} // class PrintHelper
