4

I have been asked in a interview that how to display a entire source program as output when the same program is in execution

  • Generally we use File-handling concept for this , is there any other way to achieve this task

Any suggestion for this ?

Updated Solution : As i researched something i got a solution , we can do it by Quine

  • It executes code and prints the source program , but we need to give entire program as input String array..as @Basile commented below
3
  • 8
    See wikipage on Quine programs Commented Oct 26, 2013 at 5:18
  • 2
    Not quite sure this question deserves many votes (like showing good effort to find solution) if necessary information is easy to find: "self+printing+program" - bing.com/search?q=self+printing+program Commented Oct 26, 2013 at 5:21
  • @BasileStarynkevitch..fine,but It seems that we are giving the source source as pre-defined input Commented Oct 26, 2013 at 5:30

3 Answers 3

1

This is a program that when run will display itself.The secret is to create an array of Strings that contain the linesof the program, except for the Strings in the array itself.So you have a first loop to display the lines of code before thearray of Strings.Then you have a loop to display the array Strings, and then youhave a loop to display the array Strings that represent the remaininglines of code.

Note that the ASCII value for a space character is 32 andthe ASCII value for a double quote mark is 34.The reason for creating 'quote' and 'sixSpaces' below is so that whendisplaying them, is to avoidhaving to use the escape char \, whentrying to display quotes.

public class ProgramThatDisplaysItself {

public static void main(String[] args) {
char space = (char)32;
char quote = (char)34;
String emptyStr = new String();
String spaceStr = String.valueOf(space);
String sixSpaces =
  emptyStr.concat(spaceStr).concat(spaceStr).concat(spaceStr)
          .concat(spaceStr).concat(spaceStr).concat(spaceStr);

String linesOfCode[] = {
  "package programthatdisplaysitself;",
  "",
  "public class ProgramThatDisplaysItself {",
  "",
  "  public static void main(String[] args) {",
  "    char space = (char)32;",
  "    char quote = (char)34;",
  "    String emptyStr = new String();",
  "    String spaceStr = String.valueOf(space);",
  "    String sixSpaces = ",
  "      emptyStr.concat(spaceStr).concat(spaceStr).concat(spaceStr)",
  "              .concat(spaceStr).concat(spaceStr).concat(spaceStr);",
  "",
  "    String linesOfCode[] = {",
  // Note: here's where the String array itself is skipped
  "    };",
  "",
  "    for ( int i = 0; i < 14; i++ ) {",
  "      System.out.println( linesOfCode[i] );",
  "    } // end for i",
  "",
  "    for ( int j = 0; j < linesOfCode.length; j++ ) {",
  "      System.out.println( sixSpaces + quote + linesOfCode[j] + quote + ',' );",
  "    } // end for j",
  "",
  "    for ( int k = 14; k < linesOfCode.length; k++ ) {",
  "      System.out.println( linesOfCode[k] );",
  "    } // end for k",
  "",
  "  } // end main()",
  "",
  "} // end class ProgramThatDisplaysItself",
}; // end linesOfCode[]
//display the lines until the String array elements
for ( int i = 0; i < 14; i++ ) {
  System.out.println( linesOfCode[i] );
} // end for i

//display the String array elements
for ( int j = 0; j < linesOfCode.length; j++ ) {
  System.out.println( sixSpaces + quote + linesOfCode[j] + quote + ',' );
} // end for j

//display the lines after the String array elements
for ( int k = 14; k < linesOfCode.length; k++ ) {
  System.out.println( linesOfCode[k] );
} // end for k

} // end main()

} // end class ProgramThatDisplaysItself
Sign up to request clarification or add additional context in comments.

2 Comments

So if we want to print out source code, we need to write it in array and print correct as per your answer,,Actually its quine type as per @basile commented
I think this the only way
0

Once it's in bytecode I don't think you can get the source code from it. The only way I could see to achieve this is if the program held a string copy of it's code or load the same source from a file used to compile and print it out.

Edit: After checking Quine computer, the example was to use a string inside the program with was a copy of the code.

Comments

0

You can code like this to print the source. Is it what you are looking for?

class A{
    public static void main(String args[]) throws Exception {
        FileReader f = new FileReader(filePathOfThisClass);
        BufferedReader b = new BufferedReader(f);
        String s= null;
        while ((str = b.readLine()) != null)
            System.out.println(s);
    }
}

UPDATE

according to Basile's comment I was curious. and hence i found several options. check this page for example programes from quine page

http://www.nyx.net/~gthompso/self_java.txt

3 Comments

This is the we do it..but i am asking ..is there anyother way
@kark Have you checked the comment of basile?
sorry , i misunderstood your question.. this is the only way I know .. please share if you find any other options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.