Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.java.programmer

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 06-13-2012, 08:45 PM
Daniel Pitts
Guest
 
Posts: n/a
Default "Small" Program Challenge.

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.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-13-2012, 08:52 PM
Daniel Pitts
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

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");}}"
Reply With Quote
  #3 (permalink)  
Old 06-13-2012, 09:06 PM
Stefan Ram
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

Daniel Pitts <newsgroup.nospam@virtualinfinity.net> 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').

Reply With Quote
  #4 (permalink)  
Old 06-13-2012, 09:29 PM
Stefan Ram
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

Daniel Pitts <newsgroup.nospam@virtualinfinity.net> 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.

Reply With Quote
  #5 (permalink)  
Old 06-13-2012, 11:17 PM
Daniel Pitts
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

On 6/13/12 2:06 PM, Stefan Ram wrote:
> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> 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.

Reply With Quote
  #6 (permalink)  
Old 06-13-2012, 11:19 PM
Daniel Pitts
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

On 6/13/12 2:29 PM, Stefan Ram wrote:
> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> 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.
Reply With Quote
  #7 (permalink)  
Old 06-13-2012, 11:24 PM
Daniel Pitts
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

On 6/13/12 4:19 PM, Daniel Pitts wrote:
> On 6/13/12 2:29 PM, Stefan Ram wrote:
>> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> 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.

Reply With Quote
  #8 (permalink)  
Old 06-14-2012, 12:16 AM
glen herrmannsfeldt
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

Daniel Pitts <newsgroup.nospam@virtualinfinity.net> 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
Reply With Quote
  #9 (permalink)  
Old 06-14-2012, 12:40 AM
markspace
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

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.


Reply With Quote
  #10 (permalink)  
Old 06-14-2012, 01:28 AM
javax.swing.JSnarker
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

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.
Reply With Quote
  #11 (permalink)  
Old 06-14-2012, 03:52 AM
Roedy Green
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
<newsgroup.nospam@virtualinfinity.net> 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
..
Reply With Quote
  #12 (permalink)  
Old 06-14-2012, 09:32 AM
Paul Cager
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

On Jun 13, 9:45*pm, Daniel Pitts
<newsgroup.nos...@virtualinfinity.net> 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.
Reply With Quote
  #13 (permalink)  
Old 06-14-2012, 11:29 AM
Bent C Dalager
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.

On 2012-06-13, Daniel Pitts <newsgroup.nospam@virtualinfinity.net> 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 - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs
Reply With Quote
  #14 (permalink)  
Old 06-14-2012, 12:23 PM
Hiram Hunt
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.


"Roedy Green" <see_website@mindprod.com.invalid> wrote in message
news:jqnit7pv9phoig3t2il0s4jf4s19ot7p3i@4ax.com...
> On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
> <newsgroup.nospam@virtualinfinity.net> 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 (hiramhunt@verizon.net)


Reply With Quote
  #15 (permalink)  
Old 06-14-2012, 12:30 PM
Hiram Hunt
Guest
 
Posts: n/a
Default Re: "Small" Program Challenge.


"Hiram Hunt" <hiramhunt@verizon.net> wrote in message
news:4fd9d7d1$0$1727$c3e8da3$aae71a0a@news.astrawe b.com...
>
> "Roedy Green" <see_website@mindprod.com.invalid> wrote in message
> news:jqnit7pv9phoig3t2il0s4jf4s19ot7p3i@4ax.com...
>> On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
>> <newsgroup.nospam@virtualinfinity.net> 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 (hiramhunt@verizon.net)


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

-- Hiram Hunt (hiramhunt@verizon.net)


Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 12:43 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.