|
| 1 | +/****************************************************************************** |
| 2 | + * Compilation: javac StdOut.java |
| 3 | + * Execution: java StdOut |
| 4 | + * Dependencies: none |
| 5 | + * |
| 6 | + * Writes data of various types to standard output. |
| 7 | + * |
| 8 | + ******************************************************************************/ |
| 9 | + |
| 10 | +import java.io.OutputStreamWriter; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import java.io.UnsupportedEncodingException; |
| 13 | +import java.util.Locale; |
| 14 | + |
| 15 | +/** |
| 16 | + * This class provides methods for printing strings and numbers to standard output. |
| 17 | + * <p> |
| 18 | + * <b>Getting started.</b> |
| 19 | + * To use this class, you must have {@code StdOut.class} in your |
| 20 | + * Java classpath. If you used our autoinstaller, you should be all set. |
| 21 | + * Otherwise, either download |
| 22 | + * <a href = "https://introcs.cs.princeton.edu/java/code/stdlib.jar">stdlib.jar</a> |
| 23 | + * and add to your Java classpath or download |
| 24 | + * <a href = "https://introcs.cs.princeton.edu/java/stdlib/StdOut.java">StdOut.java</a> |
| 25 | + * and put a copy in your working directory. |
| 26 | + * <p> |
| 27 | + * Here is an example program that uses {@code StdOut}: |
| 28 | + * <pre> |
| 29 | + * public class TestStdOut { |
| 30 | + * public static void main(String[] args) { |
| 31 | + * int a = 17; |
| 32 | + * int b = 23; |
| 33 | + * int sum = a + b; |
| 34 | + * StdOut.println("Hello, World"); |
| 35 | + * StdOut.printf("%d + %d = %d\n", a, b, sum); |
| 36 | + * } |
| 37 | + * } |
| 38 | + * </pre> |
| 39 | + * <p> |
| 40 | + * <b>Differences with System.out.</b> |
| 41 | + * The behavior of {@code StdOut} is similar to that of {@link System#out}, |
| 42 | + * but there are a few technical differences: |
| 43 | + * <ul> |
| 44 | + * <li> {@code StdOut} coerces the character-set encoding to UTF-8, |
| 45 | + * which is a standard character encoding for Unicode. |
| 46 | + * <li> {@code StdOut} coerces the locale to {@link Locale#US}, |
| 47 | + * for consistency with {@link StdIn}, {@link Double#parseDouble(String)}, |
| 48 | + * and floating-point literals. |
| 49 | + * <li> {@code StdOut} <em>flushes</em> standard output after each call to |
| 50 | + * {@code print()} so that text will appear immediately in the terminal. |
| 51 | + * </ul> |
| 52 | + * <p> |
| 53 | + * <b>Reference.</b> |
| 54 | + * For additional documentation, |
| 55 | + * see <a href="https://introcs.cs.princeton.edu/15inout">Section 1.5</a> of |
| 56 | + * <em>Computer Science: An Interdisciplinary Approach</em> |
| 57 | + * by Robert Sedgewick and Kevin Wayne. |
| 58 | + * |
| 59 | + * @author Robert Sedgewick |
| 60 | + * @author Kevin Wayne |
| 61 | + */ |
| 62 | +public final class StdOut { |
| 63 | + |
| 64 | + // force Unicode UTF-8 encoding; otherwise it's system dependent |
| 65 | + private static final String CHARSET_NAME = "UTF-8"; |
| 66 | + |
| 67 | + // assume language = English, country = US for consistency with StdIn |
| 68 | + private static final Locale LOCALE = Locale.US; |
| 69 | + |
| 70 | + // send output here |
| 71 | + private static PrintWriter out; |
| 72 | + |
| 73 | + // this is called before invoking any methods |
| 74 | + static { |
| 75 | + try { |
| 76 | + out = new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true); |
| 77 | + } |
| 78 | + catch (UnsupportedEncodingException e) { |
| 79 | + System.out.println(e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // don't instantiate |
| 84 | + private StdOut() { } |
| 85 | + |
| 86 | + /** |
| 87 | + * Terminates the current line by printing the line-separator string. |
| 88 | + */ |
| 89 | + public static void println() { |
| 90 | + out.println(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Prints an object to this output stream and then terminates the line. |
| 95 | + * |
| 96 | + * @param x the object to print |
| 97 | + */ |
| 98 | + public static void println(Object x) { |
| 99 | + out.println(x); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Prints a boolean to standard output and then terminates the line. |
| 104 | + * |
| 105 | + * @param x the boolean to print |
| 106 | + */ |
| 107 | + public static void println(boolean x) { |
| 108 | + out.println(x); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Prints a character to standard output and then terminates the line. |
| 113 | + * |
| 114 | + * @param x the character to print |
| 115 | + */ |
| 116 | + public static void println(char x) { |
| 117 | + out.println(x); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Prints a double to standard output and then terminates the line. |
| 122 | + * |
| 123 | + * @param x the double to print |
| 124 | + */ |
| 125 | + public static void println(double x) { |
| 126 | + out.println(x); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Prints an integer to standard output and then terminates the line. |
| 131 | + * |
| 132 | + * @param x the integer to print |
| 133 | + */ |
| 134 | + public static void println(float x) { |
| 135 | + out.println(x); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Prints an integer to standard output and then terminates the line. |
| 140 | + * |
| 141 | + * @param x the integer to print |
| 142 | + */ |
| 143 | + public static void println(int x) { |
| 144 | + out.println(x); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Prints a long to standard output and then terminates the line. |
| 149 | + * |
| 150 | + * @param x the long to print |
| 151 | + */ |
| 152 | + public static void println(long x) { |
| 153 | + out.println(x); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Prints a short integer to standard output and then terminates the line. |
| 158 | + * |
| 159 | + * @param x the short to print |
| 160 | + */ |
| 161 | + public static void println(short x) { |
| 162 | + out.println(x); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Prints a byte to standard output and then terminates the line. |
| 167 | + * <p> |
| 168 | + * To write binary data, see {@link BinaryStdOut}. |
| 169 | + * |
| 170 | + * @param x the byte to print |
| 171 | + */ |
| 172 | + public static void println(byte x) { |
| 173 | + out.println(x); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Flushes standard output. |
| 178 | + */ |
| 179 | + public static void print() { |
| 180 | + out.flush(); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Prints an object to standard output and flushes standard output. |
| 185 | + * |
| 186 | + * @param x the object to print |
| 187 | + */ |
| 188 | + public static void print(Object x) { |
| 189 | + out.print(x); |
| 190 | + out.flush(); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Prints a boolean to standard output and flushes standard output. |
| 195 | + * |
| 196 | + * @param x the boolean to print |
| 197 | + */ |
| 198 | + public static void print(boolean x) { |
| 199 | + out.print(x); |
| 200 | + out.flush(); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Prints a character to standard output and flushes standard output. |
| 205 | + * |
| 206 | + * @param x the character to print |
| 207 | + */ |
| 208 | + public static void print(char x) { |
| 209 | + out.print(x); |
| 210 | + out.flush(); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Prints a double to standard output and flushes standard output. |
| 215 | + * |
| 216 | + * @param x the double to print |
| 217 | + */ |
| 218 | + public static void print(double x) { |
| 219 | + out.print(x); |
| 220 | + out.flush(); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Prints a float to standard output and flushes standard output. |
| 225 | + * |
| 226 | + * @param x the float to print |
| 227 | + */ |
| 228 | + public static void print(float x) { |
| 229 | + out.print(x); |
| 230 | + out.flush(); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Prints an integer to standard output and flushes standard output. |
| 235 | + * |
| 236 | + * @param x the integer to print |
| 237 | + */ |
| 238 | + public static void print(int x) { |
| 239 | + out.print(x); |
| 240 | + out.flush(); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Prints a long integer to standard output and flushes standard output. |
| 245 | + * |
| 246 | + * @param x the long integer to print |
| 247 | + */ |
| 248 | + public static void print(long x) { |
| 249 | + out.print(x); |
| 250 | + out.flush(); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Prints a short integer to standard output and flushes standard output. |
| 255 | + * |
| 256 | + * @param x the short integer to print |
| 257 | + */ |
| 258 | + public static void print(short x) { |
| 259 | + out.print(x); |
| 260 | + out.flush(); |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * Prints a byte to standard output and flushes standard output. |
| 265 | + * |
| 266 | + * @param x the byte to print |
| 267 | + */ |
| 268 | + public static void print(byte x) { |
| 269 | + out.print(x); |
| 270 | + out.flush(); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * Prints a formatted string to standard output, using the specified format |
| 275 | + * string and arguments, and then flushes standard output. |
| 276 | + * |
| 277 | + * |
| 278 | + * @param format the <a href = "http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">format string</a> |
| 279 | + * @param args the arguments accompanying the format string |
| 280 | + */ |
| 281 | + public static void printf(String format, Object... args) { |
| 282 | + out.printf(LOCALE, format, args); |
| 283 | + out.flush(); |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * Prints a formatted string to standard output, using the locale and |
| 288 | + * the specified format string and arguments; then flushes standard output. |
| 289 | + * |
| 290 | + * @param locale the locale |
| 291 | + * @param format the <a href = "http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">format string</a> |
| 292 | + * @param args the arguments accompanying the format string |
| 293 | + */ |
| 294 | + public static void printf(Locale locale, String format, Object... args) { |
| 295 | + out.printf(locale, format, args); |
| 296 | + out.flush(); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Unit tests some of the methods in {@code StdOut}. |
| 301 | + * |
| 302 | + * @param args the command-line arguments |
| 303 | + */ |
| 304 | + public static void main(String[] args) { |
| 305 | + |
| 306 | + // write to stdout |
| 307 | + StdOut.println("Test"); |
| 308 | + StdOut.println(17); |
| 309 | + StdOut.println(true); |
| 310 | + StdOut.printf("%.6f\n", 1.0/7.0); |
| 311 | + } |
| 312 | + |
| 313 | +} |
0 commit comments