CSCE 828 Automata
Riedesel's Class
In Introduction to Theory of Computation by Michael Sipser, Problem 6.1 asks you to give and example in the spirit of the recursion theorem. There are several (much shorter) examples in c/c++, but for you Java addicts (of which I am one) below is an example in Java (written by Scot Anderson) It will produce the program in its entirety including the copyright. If you want to use it for non-commercial or non-student use, please feel free as long as you use the original program with the copyright notice. For those of you students looking to copy an answer, feel free to flunk! BUT do examine the program to get a feel for what is required in Java for this type of problem.
1 //Copyright 2004 Scot Anderson all rights reserved.
2 public class Main {
3 public Main() {}
4 public static void main(String args[]) {
5 String s = " System.out.println(\"//Copyright 2004 Scot Anderson all rights reserved.\");\n"+
6 " System.out.println(\"public class Main {\");\n"+
7 " System.out.println(\" public Main() {}\");\n"+
8 " System.out.println(\" public static void main(String args[]) {\");\n"+
9 " System.out.print(\" String s = \");\n"+
10 " exactprint(s);\n"+
11 " System.out.println(s);\n"+
12 " }\n"+
13 " public static void exactprint(String s) {\n"+
14 " System.out.print('\\\"');\n"+
15 " for (int i=0; i<s.length(); i++) {\n"+
16 " char c = s.charAt(i);\n"+
17 " if (c == '\\\"') {\n"+
18 " System.out.print(\"\\\\\\\"\");\n"+
19 " } else if (c == '\\\\') {\n"+
20 " System.out.print(\"\\\\\\\\\");\n"+
21 " } else if (c == '\\n') {\n"+
22 " System.out.print(\"\\\\n\\\"+\\n \\\"\");\n"+
23 " } else {\n"+
24 " System.out.print(c);\n"+
25 " }\n"+
26 " }\n"+
27 " System.out.println('\\\"'+\";\");\n"+
28 " }\n"+
29 "}";
30 System.out.println("//Copyright 2004 Scot Anderson all rights reserved.");
31 System.out.println("public class Main {");
32 System.out.println(" public Main() {}");
33 System.out.println(" public static void main(String args[]) {");
34 System.out.print(" String s = ");
35 exactprint(s);
36 System.out.println(s);
37 }
38 public static void exactprint(String s) {
39 System.out.print('\"');
40 for (int i=0; i<s.length(); i++) {
41 char c = s.charAt(i);
42 if (c == '\"') {
43 System.out.print("\\\"");
44 } else if (c == '\\') {
45 System.out.print("\\\\");
46 } else if (c == '\n') {
47 System.out.print("\\n\"+\n \"");
48 } else {
49 System.out.print(c);
50 }
51 }
52 System.out.println('\"'+";");
53 }
54 }