"Small" Program Challenge.

Discussion in 'Java' started by Daniel Pitts, Jun 13, 2012.

  1. Daniel Pitts

    Daniel Pitts Guest

    I saw a challenge Roedy posted on cljh, and I thought I might have a
    slightly more interesting one.

    Write a Java program which outputs "Hello World" followed by a new line
    (and nothing else).

    Now, do it using as few characters in the .java source code as possible.

    I've got mine down to 61 characters. See if you can match that.
     
    Daniel Pitts, Jun 13, 2012
    #1
    1. Advertising

  2. Daniel Pitts

    Daniel Pitts Guest

    On 6/13/12 1:45 PM, Daniel Pitts wrote:
    > I saw a challenge Roedy posted on cljh, and I thought I might have a
    > slightly more interesting one.
    >
    > Write a Java program which outputs "Hello World" followed by a new line
    > (and nothing else).
    >
    > Now, do it using as few characters in the .java source code as possible.
    >
    > I've got mine down to 61 characters. See if you can match that.


    Hint, the following is 82 characters, if you remove line wrapping. Where
    do I trim the 21 characters?

    "class M{public static void main(String[]args){System.out.println("Hello
    World");}}"
     
    Daniel Pitts, Jun 13, 2012
    #2
    1. Advertising

  3. Daniel Pitts

    Stefan Ram Guest

    Daniel Pitts <> writes:
    >>Write a Java program which outputs "Hello World" followed by a new line

    (...)
    >"class M{public static void main(String[]args){System.out.println("Hello World");}}"


    »System.out.println("Hello World");« does not output "Hello
    World" followed by a new line, but "Hello World" followed by
    the line separator string. The line separator string is
    defined by the system property line.separator, and is not
    necessarily a single newline character ('\n').
     
    Stefan Ram, Jun 13, 2012
    #3
  4. Daniel Pitts

    Stefan Ram Guest

    Daniel Pitts <> writes:
    >Where do I trim the 21 characters?


    You can trim the »args« to »a«. Possibly, some earlier JDKs allowed
    omission of the main method, but a recent JDK seems to require it.
     
    Stefan Ram, Jun 13, 2012
    #4
  5. Daniel Pitts

    Daniel Pitts Guest

    On 6/13/12 2:06 PM, Stefan Ram wrote:
    > Daniel Pitts<> writes:
    >>> Write a Java program which outputs "Hello World" followed by a new line

    > (...)
    >> "class M{public static void main(String[]args){System.out.println("Hello World");}}"

    >
    > »System.out.println("Hello World");« does not output "Hello
    > World" followed by a new line, but "Hello World" followed by
    > the line separator string. The line separator string is
    > defined by the system property line.separator, and is not
    > necessarily a single newline character ('\n').
    >

    I didn't say a "new line" character. However, print("Hello World\n") is
    the same length. My intent was line separator, however if you choose to
    interpret it the other way, there is no benefit or penalty.
     
    Daniel Pitts, Jun 14, 2012
    #5
  6. Daniel Pitts

    Daniel Pitts Guest

    On 6/13/12 2:29 PM, Stefan Ram wrote:
    > Daniel Pitts<> writes:
    >> Where do I trim the 21 characters?

    >
    > You can trim the »args« to »a«.

    Ah, yes. That was just habit on my part.

    > Possibly, some earlier JDKs allowed
    > omission of the main method, but a recent JDK seems to require it.
    >


    The JDK doesn't require anything of a class. java on the other hand
    goes through a specific sequence when asked to "run" a Java program.

    My smallest program is still 61 characters long, the example I posted,
    after replacing args with a, is 79.
     
    Daniel Pitts, Jun 14, 2012
    #6
  7. Daniel Pitts

    Daniel Pitts Guest

    On 6/13/12 4:19 PM, Daniel Pitts wrote:
    > On 6/13/12 2:29 PM, Stefan Ram wrote:
    >> Daniel Pitts<> writes:
    >>> Where do I trim the 21 characters?

    >>
    >> You can trim the »args« to »a«.

    > Ah, yes. That was just habit on my part.
    >
    >> Possibly, some earlier JDKs allowed
    >> omission of the main method, but a recent JDK seems to require it.
    >>

    >
    > The JDK doesn't require anything of a class. java on the other hand goes
    > through a specific sequence when asked to "run" a Java program.

    Ah, although now I see reports that Java 7 does some validation before
    some of that sequence. So, my 61 character source compiles fine, but
    won't run on Java 7.

    I wonder why they bothered.
     
    Daniel Pitts, Jun 14, 2012
    #7
  8. Daniel Pitts <> wrote:

    (snip)
    > I didn't say a "new line" character. However, print("Hello World\n") is
    > the same length. My intent was line separator, however if you choose to
    > interpret it the other way, there is no benefit or penalty.


    Note that there is no requirement that the host system even use
    a newline character. There are systems that keep track of lines
    by length.

    Now, the C tradition of using '\n' as a line terminator, even
    on systems that don't store files that way, isn't completely gone
    in Java. Writing a "\n" will likely generate a new line even on
    systems that don't use a newline character.

    -- glen
     
    glen herrmannsfeldt, Jun 14, 2012
    #8
  9. Daniel Pitts

    markspace Guest

    On 6/13/2012 1:52 PM, Daniel Pitts wrote:
    > On 6/13/12 1:45 PM, Daniel Pitts wrote:
    >> I saw a challenge Roedy posted on cljh, and I thought I might have a
    >> slightly more interesting one.
    >>
    >> Write a Java program which outputs "Hello World" followed by a new line
    >> (and nothing else).
    >>
    >> Now, do it using as few characters in the .java source code as possible.
    >>
    >> I've got mine down to 61 characters. See if you can match that.

    >
    > Hint, the following is 82 characters, if you remove line wrapping. Where
    > do I trim the 21 characters?
    >
    > "class M{public static void main(String[]args){System.out.println("Hello
    > World");}}"



    This is a good one, although the options for really trimming things down
    in surprising ways is absent in Java.

    Another good one is to write a Java program that prints its own source
    text. No fair using an external file, of course.
     
    markspace, Jun 14, 2012
    #9
  10. On 13/06/2012 4:45 PM, Daniel Pitts wrote:
    > I saw a challenge Roedy posted on cljh, and I thought I might have a
    > slightly more interesting one.
    >
    > Write a Java program which outputs "Hello World" followed by a new line
    > (and nothing else).
    >
    > Now, do it using as few characters in the .java source code as possible.
    >
    > I've got mine down to 61 characters. See if you can match that.


    class X{static{System.out.println("Hello World");for(;;);}}

    is 59 characters.

    Hey, you didn't say it has to actually *terminate*! ;)

    --
    public final class JSnarker
    extends JComponent
    A JSnarker is an NNTP-aware component that asynchronously provides
    snarky output when the Ego.needsPuncturing() event is fired in cljp.
     
    javax.swing.JSnarker, Jun 14, 2012
    #10
  11. Daniel Pitts

    Roedy Green Guest

    On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
    <> wrote, quoted or indirectly
    quoted someone who said :

    >
    >I've got mine down to 61 characters. See if you can match that.


    here's the obvious solution at 88 chars:

    public class C{public static void main(String[]
    a){System.out.println("Hello World");}}

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Controlling complexity is the essence of computer programming.
    ~ Brian W. Kernighan 1942-01-01
    ..
     
    Roedy Green, Jun 14, 2012
    #11
  12. Daniel Pitts

    Paul Cager Guest

    On Jun 13, 9:45 pm, Daniel Pitts
    <> wrote:
    > I saw a challenge Roedy posted on cljh, and I thought I might have a
    > slightly more interesting one.
    >
    > Write a Java program which outputs "Hello World" followed by a new line
    > (and nothing else).
    >
    > Now, do it using as few characters in the .java source code as possible.
    >
    > I've got mine down to 61 characters. See if you can match that.


    You may also find some of the challenges on http://codegolf.stackexchange.com/
    interesting.
     
    Paul Cager, Jun 14, 2012
    #12
  13. On 2012-06-13, Daniel Pitts <> wrote:
    > I saw a challenge Roedy posted on cljh, and I thought I might have a
    > slightly more interesting one.
    >
    > Write a Java program which outputs "Hello World" followed by a new line
    > (and nothing else).
    >
    > Now, do it using as few characters in the .java source code as possible.


    How much are you permitted to offload to the launcher?

    Trivial example of offloading:

    class A{public static void main(String[] a){System.out.print(a[0]);}}
    (69 chars)

    with launch instructions:

    run like this (bash command line example shown, other launch
    environments will have other ways of expressing the newline)

    $ java A "Hello World
    > "

    $


    And how much can you offload to a hypothetical "library" function that
    just happens to do exactly what you want?

    class B{public static void main(String[] a){L.f();}}
    (52 chars)

    Cheers,

    Bent.
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Jun 14, 2012
    #13
  14. Daniel Pitts

    Hiram Hunt Guest

    "Roedy Green" <> wrote in message
    news:...
    > On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
    > <> wrote, quoted or indirectly
    > quoted someone who said :
    >
    >>
    >>I've got mine down to 61 characters. See if you can match that.

    >
    > here's the obvious solution at 88 chars:
    >
    > public class C{public static void main(String[]
    > a){System.out.println("Hello World");}}


    No need for public on class.

    -- Hiram Hunt ()
     
    Hiram Hunt, Jun 14, 2012
    #14
  15. Daniel Pitts

    Hiram Hunt Guest

    "Hiram Hunt" <> wrote in message
    news:4fd9d7d1$0$1727$c3e8da3$...
    >
    > "Roedy Green" <> wrote in message
    > news:...
    >> On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
    >> <> wrote, quoted or indirectly
    >> quoted someone who said :
    >>
    >>>
    >>>I've got mine down to 61 characters. See if you can match that.

    >>
    >> here's the obvious solution at 88 chars:
    >>
    >> public class C{public static void main(String[]
    >> a){System.out.println("Hello World");}}

    >
    > No need for public on class.
    >
    > -- Hiram Hunt ()


    Sorry, I think I missed your point that this was just the obvious
    solution. Other posts are already public-less on class.

    -- Hiram Hunt ()
     
    Hiram Hunt, Jun 14, 2012
    #15
  16. Daniel Pitts

    Stefan Ram Guest

    Bent C Dalager <> writes:
    >$ java A "Hello World
    >> "


    A mere »System.out.println()« will suffice with

    java A -Dline.separator="Hello World
    "

    , however, some shells might not treat the embedded line
    separator as intended.
     
    Stefan Ram, Jun 14, 2012
    #16
  17. Daniel Pitts

    Stefan Ram Guest

    Daniel Pitts <> writes:
    >I've got mine down to 61 characters. See if you can match that.


    Here is a new variant of the above challenge:

    Write a java program (source code) with less than 4000
    characters and a java command line with less than 1000
    characters that writes »Hello World« followed by a newline
    character and nothing else, but does so in a somewhat
    surprising or unusual way.

    My entry:

    public class Main
    { public static void main( final java.lang.String[] args )
    { System.out.println(); System.out.print( '\n' ); }}

    java -Dline.separator="Hello World" Main
     
    Stefan Ram, Jun 14, 2012
    #17
  18. Daniel Pitts

    Daniel Pitts Guest

    On 6/14/12 11:04 AM, Stefan Ram wrote:
    > Daniel Pitts<> writes:
    >> I've got mine down to 61 characters. See if you can match that.

    >
    > Here is a new variant of the above challenge:
    >
    > Write a java program (source code) with less than 4000
    > characters and a java command line with less than 1000
    > characters that writes »Hello World« followed by a newline
    > character and nothing else, but does so in a somewhat
    > surprising or unusual way.
    >
    > My entry:
    >
    > public class Main
    > { public static void main( final java.lang.String[] args )
    > { System.out.println(); System.out.print( '\n' ); }}
    >
    > java -Dline.separator="Hello World" Main
    >


    A slightly obfuscated program which illustrates a few surprising things.

    public class Hello {
    static Object left = "Top", right = "Bottom";
    static Object top = "Left";
    static Object bottom = "Right";

    public static void main(String[] args) throws Exception {
    for (int i = 0; i < 2; ++i) {
    s.out.print(new Hello());
    y(left, right, top, bottom);
    }
    }

    public String toString() {
    try {
    top = "value";
    bottom = "count";
    return "enumeration" + top + bottom;
    } finally {
    return getClass().getName();
    }
    }

    static <T extends java.lang.reflect.AccessibleObject> T t(T t) {
    t.setAccessible(true); return t;}

    static <T> void y(T l, T r, T... os) throws Exception {
    for (Object o : os) {
    x(l, o).set(l, x(l, o).get(r));
    }
    }

    private static java.lang.reflect.Field x(Object l, Object o) throws
    NoSuchFieldException {
    return t(l.getClass().getDeclaredField(o.toString()));
    }

    {
    left = toString();
    right = " W" + b + 'r' + a + "d\n";
    }

    static Object a = "l";
    static Object b = "o";
    static System s;
    }
     
    Daniel Pitts, Jun 14, 2012
    #18
  19. On 14 Jun 2012 18:04:46 GMT, -berlin.de (Stefan Ram)
    wrote:

    >Daniel Pitts <> writes:
    >>I've got mine down to 61 characters. See if you can match that.

    >
    > Here is a new variant of the above challenge:
    >
    > Write a java program (source code) with less than 4000
    > characters and a java command line with less than 1000
    > characters that writes »Hello World« followed by a newline
    > character and nothing else, but does so in a somewhat
    > surprising or unusual way.
    >
    > My entry:
    >
    >public class Main
    >{ public static void main( final java.lang.String[] args )
    > { System.out.println(); System.out.print( '\n' ); }}
    >
    >java -Dline.separator="Hello World" Main


    The IOCCC (International Obfuscated C Code Contest) has been
    running yearly for twenty years. Is someone trying to start an IOJJJ
    (International Obfuscated Java Jungle of Junk?)?

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Jun 14, 2012
    #19
  20. Daniel Pitts

    Daniel Pitts Guest

    On 6/14/12 11:04 AM, Stefan Ram wrote:
    > Daniel Pitts<> writes:
    >> I've got mine down to 61 characters. See if you can match that.

    >
    > Here is a new variant of the above challenge:
    >
    > Write a java program (source code) with less than 4000
    > characters and a java command line with less than 1000
    > characters that writes »Hello World« followed by a newline
    > character and nothing else, but does so in a somewhat
    > surprising or unusual way.
    >
    > My entry:
    >
    > public class Main
    > { public static void main( final java.lang.String[] args )
    > { System.out.println(); System.out.print( '\n' ); }}
    >
    > java -Dline.separator="Hello World" Main
    >

    Another lesson: Exceptions, and the deprecated (and highly dangerous)
    Thread.stop(Throwable) method.

    import java.awt.EventQueue;
    import java.io.PrintStream;
    import java.lang.reflect.InvocationTargetException;

    public class Hello extends Exception {
    public static void main(String[] args) throws Exception {
    Thread.setDefaultUncaughtExceptionHandler(
    new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) {
    e.printStackTrace(System.out);
    }
    });
    final Thread thread = Thread.currentThread();
    EventQueue.invokeAndWait(new Runnable() {
    public void run() {
    thread.stop(new Hello());
    }
    });
    }

    public void printStackTrace(PrintStream s) {
    s.print("Hello World");
    }
    }
     
    Daniel Pitts, Jun 15, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    535
  2. Andr? Roberge

    Linguistic challenge: name this program

    Andr? Roberge, Dec 5, 2004, in forum: Python
    Replies:
    1
    Views:
    269
    Phil Schmidt
    Dec 5, 2004
  3. Azolex
    Replies:
    3
    Views:
    272
    Azolex
    Apr 5, 2006
  4. Chris Logan

    Tutorial challenge program help

    Chris Logan, Sep 9, 2009, in forum: Ruby
    Replies:
    19
    Views:
    225
    Walton Hoops
    Sep 10, 2009
  5. Rui Maciel
    Replies:
    1
    Views:
    202
    Gert-Jan de Vos
    Apr 13, 2013
Loading...

Share This Page