/*************************************************************************
* Compilation: javac StdOut.java
* Execution: java StdOut
*
* Writes data of various types to standard output.
*
*************************************************************************/
importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.io.UnsupportedEncodingException;importjava.util.Locale;/*** Standard output. This class provides methods for writing strings
* and numbers to standard output.
*
* For additional documentation, see Section 1.5 of
* Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
*
*@authorRobert Sedgewick
*@authorKevin Wayne*/
public final classStdOut {//force Unicode UTF-8 encoding; otherwise it's system dependent
private static final String CHARSET_NAME = "UTF-8";//assume language = English, country = US for consistency with StdIn
private static final Locale LOCALE =Locale.US;//send output here
private staticPrintWriter out;//this is called before invoking any methods
static{try{
out= new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true);
}catch(UnsupportedEncodingException e) { System.out.println(e); }
}//don't instantiate
privateStdOut() { }//close the output stream (not required)
/*** Close standard output.*/
public static voidclose() {
out.close();
}/*** Terminate the current line by printing the line separator string.*/
public static voidprintln() {
out.println();
}/*** Print an object to standard output and then terminate the line.*/
public static voidprintln(Object x) {
out.println(x);
}/*** Print a boolean to standard output and then terminate the line.*/
public static void println(booleanx) {
out.println(x);
}/*** Print a char to standard output and then terminate the line.*/
public static void println(charx) {
out.println(x);
}/*** Print a double to standard output and then terminate the line.*/
public static void println(doublex) {
out.println(x);
}/*** Print a float to standard output and then terminate the line.*/
public static void println(floatx) {
out.println(x);
}/*** Print an int to standard output and then terminate the line.*/
public static void println(intx) {
out.println(x);
}/*** Print a long to standard output and then terminate the line.*/
public static void println(longx) {
out.println(x);
}/*** Print a short to standard output and then terminate the line.*/
public static void println(shortx) {
out.println(x);
}/*** Print a byte to standard output and then terminate the line.*/
public static void println(bytex) {
out.println(x);
}/*** Flush standard output.*/
public static voidprint() {
out.flush();
}/*** Print an Object to standard output and flush standard output.*/
public static voidprint(Object x) {
out.print(x);
out.flush();
}/*** Print a boolean to standard output and flush standard output.*/
public static void print(booleanx) {
out.print(x);
out.flush();
}/*** Print a char to standard output and flush standard output.*/
public static void print(charx) {
out.print(x);
out.flush();
}/*** Print a double to standard output and flush standard output.*/
public static void print(doublex) {
out.print(x);
out.flush();
}/*** Print a float to standard output and flush standard output.*/
public static void print(floatx) {
out.print(x);
out.flush();
}/*** Print an int to standard output and flush standard output.*/
public static void print(intx) {
out.print(x);
out.flush();
}/*** Print a long to standard output and flush standard output.*/
public static void print(longx) {
out.print(x);
out.flush();
}/*** Print a short to standard output and flush standard output.*/
public static void print(shortx) {
out.print(x);
out.flush();
}/*** Print a byte to standard output and flush standard output.*/
public static void print(bytex) {
out.print(x);
out.flush();
}/*** Print a formatted string to standard output using the specified
* format string and arguments, and flush standard output.*/
public static voidprintf(String format, Object... args) {
out.printf(LOCALE, format, args);
out.flush();
}/*** Print a formatted string to standard output using the specified
* locale, format string, and arguments, and flush standard output.*/
public static voidprintf(Locale locale, String format, Object... args) {
out.printf(locale, format, args);
out.flush();
}//This method is just here to test the class
public static voidmain(String[] args) {//write to stdout
StdOut.println("Test");
StdOut.println(17);
StdOut.println(true);
StdOut.printf("%.6f\n", 1.0/7.0);
}
}