Advertisement
calcpage

StdOut.java

Jun 12th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.75 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac StdOut.java
  3.  *  Execution:    java StdOut
  4.  *
  5.  *  Writes data of various types to standard output.
  6.  *
  7.  *************************************************************************/
  8.  
  9. import java.io.OutputStreamWriter;
  10. import java.io.PrintWriter;
  11. import java.io.UnsupportedEncodingException;
  12. import java.util.Locale;
  13.  
  14. /**
  15.  *  <i>Standard output</i>. This class provides methods for writing strings
  16.  *  and numbers to standard output.
  17.  *  <p>
  18.  *  For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
  19.  *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
  20.  */
  21. public final class StdOut {
  22.  
  23.     // force Unicode UTF-8 encoding; otherwise it's system dependent
  24.     private static final String UTF8 = "UTF-8";
  25.  
  26.     // assume language = English, country = US for consistency with StdIn
  27.     private static final Locale US_LOCALE = new Locale("en", "US");
  28.  
  29.     // send output here
  30.     private static PrintWriter out;
  31.  
  32.     // this is called before invoking any methods
  33.     static {
  34.         try {
  35.             out = new PrintWriter(new OutputStreamWriter(System.out, UTF8), true);
  36.         }
  37.         catch (UnsupportedEncodingException e) { System.out.println(e); }
  38.     }
  39.  
  40.     // singleton pattern - can't instantiate
  41.     private StdOut() { }
  42.  
  43.     // close the output stream (not required)
  44.    /**
  45.      * Close standard output.
  46.      */
  47.     public static void close() {
  48.         out.close();
  49.     }
  50.  
  51.    /**
  52.      * Terminate the current line by printing the line separator string.
  53.      */
  54.     public static void println() {
  55.         out.println();
  56.     }
  57.  
  58.    /**
  59.      * Print an object to standard output and then terminate the line.
  60.      */
  61.     public static void println(Object x) {
  62.         out.println(x);
  63.     }
  64.  
  65.    /**
  66.      * Print a boolean to standard output and then terminate the line.
  67.      */
  68.     public static void println(boolean x) {
  69.         out.println(x);
  70.     }
  71.  
  72.    /**
  73.      * Print a char to standard output and then terminate the line.
  74.      */
  75.     public static void println(char x) {
  76.         out.println(x);
  77.     }
  78.  
  79.    /**
  80.      * Print a double to standard output and then terminate the line.
  81.      */
  82.     public static void println(double x) {
  83.         out.println(x);
  84.     }
  85.  
  86.    /**
  87.      * Print a float to standard output and then terminate the line.
  88.      */
  89.     public static void println(float x) {
  90.         out.println(x);
  91.     }
  92.  
  93.    /**
  94.      * Print an int to standard output and then terminate the line.
  95.      */
  96.     public static void println(int x) {
  97.         out.println(x);
  98.     }
  99.  
  100.    /**
  101.      * Print a long to standard output and then terminate the line.
  102.      */
  103.     public static void println(long x) {
  104.         out.println(x);
  105.     }
  106.  
  107.    /**
  108.      * Print a short to standard output and then terminate the line.
  109.      */
  110.     public static void println(short x) {
  111.         out.println(x);
  112.     }
  113.  
  114.    /**
  115.      * Print a byte to standard output and then terminate the line.
  116.      */
  117.     public static void println(byte x) {
  118.         out.println(x);
  119.     }
  120.  
  121.    /**
  122.      * Flush standard output.
  123.      */
  124.     public static void print() {
  125.         out.flush();
  126.     }
  127.  
  128.    /**
  129.      * Print an Object to standard output and flush standard output.
  130.      */
  131.     public static void print(Object x) {
  132.         out.print(x);
  133.         out.flush();
  134.     }
  135.  
  136.    /**
  137.      * Print a boolean to standard output and flush standard output.
  138.      */
  139.     public static void print(boolean x) {
  140.         out.print(x);
  141.         out.flush();
  142.     }
  143.  
  144.    /**
  145.      * Print a char to standard output and flush standard output.
  146.      */
  147.     public static void print(char x) {
  148.         out.print(x);
  149.         out.flush();
  150.     }
  151.  
  152.    /**
  153.      * Print a double to standard output and flush standard output.
  154.      */
  155.     public static void print(double x) {
  156.         out.print(x);
  157.         out.flush();
  158.     }
  159.  
  160.    /**
  161.      * Print a float to standard output and flush standard output.
  162.      */
  163.     public static void print(float x) {
  164.         out.print(x);
  165.         out.flush();
  166.     }
  167.  
  168.    /**
  169.      * Print an int to standard output and flush standard output.
  170.      */
  171.     public static void print(int x) {
  172.         out.print(x);
  173.         out.flush();
  174.     }
  175.  
  176.    /**
  177.      * Print a long to standard output and flush standard output.
  178.      */
  179.     public static void print(long x) {
  180.         out.print(x);
  181.         out.flush();
  182.     }
  183.  
  184.    /**
  185.      * Print a short to standard output and flush standard output.
  186.      */
  187.     public static void print(short x) {
  188.         out.print(x);
  189.         out.flush();
  190.     }
  191.  
  192.    /**
  193.      * Print a byte to standard output and flush standard output.
  194.      */
  195.     public static void print(byte x) {
  196.         out.print(x);
  197.         out.flush();
  198.     }
  199.  
  200.    /**
  201.      * Print a formatted string to standard output using the specified
  202.      * format string and arguments, and flush standard output.
  203.      */
  204.     public static void printf(String format, Object... args) {
  205.         out.printf(US_LOCALE, format, args);
  206.         out.flush();
  207.     }
  208.  
  209.    /**
  210.      * Print a formatted string to standard output using the specified
  211.      * locale, format string, and arguments, and flush standard output.
  212.      */
  213.     public static void printf(Locale locale, String format, Object... args) {
  214.         out.printf(locale, format, args);
  215.         out.flush();
  216.     }
  217.  
  218.     // This method is just here to test the class
  219.     public static void main(String[] args) {
  220.  
  221.         // write to stdout
  222.         StdOut.println("Test");
  223.         StdOut.println(17);
  224.         StdOut.println(true);
  225.         StdOut.printf("%.6f\n", 1.0/7.0);
  226.     }
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement