This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Write a program which outputs its own source code.

 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone have any idea for this? Someone asked this on another forum, so I was curious what people on here thought.... I don't know why people would want to do that, but okay.... :p.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Bear, I forgot about that rule . And yeah this is completely useless, but I'm curious about it....

The only thing I think of is making the entire thing 1 giant string(useless) or making a print statement after each line(useless).... The dude said the code doesn't have to work either, just code, so I figure the first selection but whyyyyyyyyyyyyyyy!!!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a name for programs like these: "quine". See Quine (computing) for example - that page has a Java example. If you search for that term you'll probably find plenty of others.

(You can also write a program that reads its own source code and prints it out, but I believe that's considered cheating ).
 
Rancher
Posts: 43011
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should resist the temptation to follow the link Matthew posted, but instead try to do it yourself. The learning effect of doing it yourself far surpasses the knowledge gained by being told how to do it.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You should resist the temptation to follow the link Matthew posted, but instead try to do it yourself. The learning effect of doing it yourself far surpasses the knowledge gained by being told how to do it.




this isn't for me, it's for someone else :P. I could care less, I was just stumped as how to do it, or WHY to do it :P.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Orsaw wrote:I don't know why people would want to do that, but okay.... :p.


Just because it's interesting. People try all kinds of new and strange things just because they are curious and they like solving puzzles. Why do people make art?
 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a kind of measurement of the power of a programming language if such a quine can be written in it.
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To take the challenge one step further (and make the resulting source code even more obscure), try to write the shortest source code that prints itself. I think I have seen a Java solution that does this in less than 200 characters.
 
Ivan Jozsef Balazs
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> whyyyyyyyyyyyyyyy!

George Mallory

George Herbert Leigh Mallory (18 June 1886 – 8 or 9 June 1924) was an English mountaineer who took part in the first three British expeditions to Mount Everest in the early 1920s.

Mallory is famously quoted as having replied to the question "Why do you want to climb Mount Everest?" with the retort "Because it's there", which has been called "the most famous three words in mountaineering".
 
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:To take the challenge one step further (and make the resulting source code even more obscure), try to write the shortest source code that prints itself. I think I have seen a Java solution that does this in less than 200 characters.


Just came across a new language that lets you do it in seven:

chicken

See here
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaja Shinde

Your code only works if:

a) you have the source file
b) the source file is in a VERY specific place.

and in any case, as Matthew wrote back in November, a file that reads its own source code is considered cheating for this challenge.
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger

You are right Sir.
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is source code of the C Program to display its own source code as its output.The C program is successfully compiled and run on a Linux system. The program output is also shown below.

It outputs its own source code

 
fred rosenberger
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ujjwal soni wrote:Here is source code of the C Program to display its own source code as its output.


Are you saying you could send me your compiled program, I could drop it anywhere on my box, run it, and it would work?

Would your program work on YOUR box if you moved it to a new directory?
 
ujjwal soni
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it will work
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ujjwal soni wrote:Yes, it will work



I think you are missing the point. Will your program work, if you only have the binary? You need to compile your program, and then run it without access to the source code. The binary needs to be able to regenerate it's own source code.

If the OP was looking for your answer, it wouldn't be such a difficult problem to solve, would it?

Henry
 
fred rosenberger
lowercase baba
Posts: 12766
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your program opens the .c file, reads it line by line, and prints each line. If the .c file isn't there, it won't work. If you give me your binary (and ONLY your binary) and I run it, it won't work.

If you were to edit your .c file, you would print out the new contents of the file, not the code that actually created the binary.

In fact, the 4th post in this thread even states:

(You can also write a program that reads its own source code and prints it out, but I believe that's considered cheating ).

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting....
 
Ranch Hand
Posts: 789
Python C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well...there would be three ways to do it. First, read the source file and print it. Second, read the source code stored as data in the executable and print it. Third, decompile or disassemble the executable and create source code and print it.

If the first way is cheating, the second is in principle the same thing.

Decompiling would give you something that would "print itself" but it would likely not be the actual source code that was entered. You could run it once, and then compile that output and see if the two matched the second time. I think they probably would.

Disassembling - this would work and would be easy. If you start with assembly language you can know where the opcodes begin and end, and every opcode translates directly to a statement in assembly language. So just read the executable and convert the opcodes back to mnemonics.

 
Evacuate the building! Here, take this tiny ad with you:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!