All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.princeton.cs.introcs.StdOut Maven / Gradle / Ivy

Go to download

Standard input and output libraries from Princeton's "Introduction to Programming in Java" textbook.

The newest version!
package edu.princeton.cs.introcs;

/*************************************************************************
 *  Compilation:  javac StdOut.java
 *  Execution:    java StdOut
 *
 *  Writes data of various types to standard output.
 *
 *************************************************************************/

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.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. * * @author Robert Sedgewick * @author Kevin Wayne */ public final class StdOut { // force Unicode UTF-8 encoding; otherwise it's system dependent private static final String charsetName = "UTF-8"; // assume language = English, country = US for consistency with StdIn private static final Locale US_LOCALE = new Locale("en", "US"); // send output here private static PrintWriter out; // this is called before invoking any methods static { try { out = new PrintWriter(new OutputStreamWriter(System.out, charsetName), true); } catch (UnsupportedEncodingException e) { System.out.println(e); } } // don't instantiate private StdOut() { } // close the output stream (not required) /** * Close standard output. */ public static void close() { out.close(); } /** * Terminate the current line by printing the line separator string. */ public static void println() { out.println(); } /** * Print an object to standard output and then terminate the line. */ public static void println(Object x) { out.println(x); } /** * Print a boolean to standard output and then terminate the line. */ public static void println(boolean x) { out.println(x); } /** * Print a char to standard output and then terminate the line. */ public static void println(char x) { out.println(x); } /** * Print a double to standard output and then terminate the line. */ public static void println(double x) { out.println(x); } /** * Print a float to standard output and then terminate the line. */ public static void println(float x) { out.println(x); } /** * Print an int to standard output and then terminate the line. */ public static void println(int x) { out.println(x); } /** * Print a long to standard output and then terminate the line. */ public static void println(long x) { out.println(x); } /** * Print a short to standard output and then terminate the line. */ public static void println(short x) { out.println(x); } /** * Print a byte to standard output and then terminate the line. */ public static void println(byte x) { out.println(x); } /** * Flush standard output. */ public static void print() { out.flush(); } /** * Print an Object to standard output and flush standard output. */ public static void print(Object x) { out.print(x); out.flush(); } /** * Print a boolean to standard output and flush standard output. */ public static void print(boolean x) { out.print(x); out.flush(); } /** * Print a char to standard output and flush standard output. */ public static void print(char x) { out.print(x); out.flush(); } /** * Print a double to standard output and flush standard output. */ public static void print(double x) { out.print(x); out.flush(); } /** * Print a float to standard output and flush standard output. */ public static void print(float x) { out.print(x); out.flush(); } /** * Print an int to standard output and flush standard output. */ public static void print(int x) { out.print(x); out.flush(); } /** * Print a long to standard output and flush standard output. */ public static void print(long x) { out.print(x); out.flush(); } /** * Print a short to standard output and flush standard output. */ public static void print(short x) { out.print(x); out.flush(); } /** * Print a byte to standard output and flush standard output. */ public static void print(byte x) { 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 void printf(String format, Object... args) { out.printf(US_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 void printf(Locale locale, String format, Object... args) { out.printf(locale, format, args); out.flush(); } // This method is just here to test the class public static void main(String[] args) { // write to stdout StdOut.println("Test"); StdOut.println(17); StdOut.println(true); StdOut.printf("%.6f\n", 1.0/7.0); } } /************************************************************************* * Copyright 2002-2012, Robert Sedgewick and Kevin Wayne. * * This file is part of stdlib-package.jar, which accompanies the textbook * * Introduction to Programming in Java: An Interdisciplinary Approach * by R. Sedgewick and K. Wayne, Addison-Wesley, 2007. ISBN 0-321-49805-4. * * http://introcs.cs.princeton.edu * * * stdlib-package.jar is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * stdlib-package.jar is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with stdlib-package.jar. If not, see http://www.gnu.org/licenses. *************************************************************************/





© 2015 - 2024 Weber Informatics LLC | Privacy Policy