Thinking in Java Fourth Edition Bruce Eckel Chapter 2 Exercise 1

Exercise 1: Write a program that uses the “short” and normal form of print statement.

Solution:

package net.mindview.util;
import java.io.*;

public class Print
{
    // Print with a newline:
    public static void print(Object obj)
    {
        System.out.println(obj);
    }
    // Print a newline by itself:
    public static void print() {
        System.out.println();
    }
    // Print with no line break:
    public static void printnb(Object obj)
    {
        System.out.print(obj);
    }
    // The new Java SE5 printf() (from C):
    public static PrintStream
    printf(String format, Object... args)
    {
        return System.out.printf(format, args);
    }
}
import static net.mindview.util.Print.*;

public class PrintTest
{
    public static void main(String[] args)
    {
        print("Hello, from short form.");
        System.out.println("Hello from long form.");
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *