Printing Gray code through modifying Samuel Beckett's stage instructions This is exercise 2.3.24. from the book Computer Science An Interdisciplinary Approach by Sedgewick &amp; Wayne:
Modify Beckett to print the Gray code.
Beckett refers to the following program within the text:
public class Beckett
{
    public static void moves(int n, boolean enter)
    {
        if (n == 0) return;
        moves(n-1, true);
        if (enter) System.out.println(&quot;enter &quot; + n);
        else       System.out.println(&quot;exit &quot; + n);
        moves(n-1, false);
    }
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        moves(n, true);
    }
}

As stated in the text:

The playwright Samuel Beckett wrote a play called Quad that had the following property:
starting with an empty stage, characters enter and exit one at a time
so that each subset of characters on the stage appears exactly once.

The above program tries to simulate the above-explained stage instructions. Here is my attempt at turning Beckett into a program that produces Gray code:
public class exercise2_3_24 
{
    public static String swap(String s, String t, int n)
    {
        n = n-1;
        return s.substring(0,n) + t + s.substring(n+1);
    }
    public static void beckett(String s, int n)
    {
        if (n == 0) return;
        beckett(s, n-1);
        System.out.println(swap(s, &quot;1&quot;, n));
        s = swap(s, &quot;1&quot;, n);
        beckett(s, n-1);
    }
    public static void graycodes(int n)
    {
        String s = &quot;&quot;;
        for (int i = 0; i &lt; n; i++)
            s += &quot;0&quot;;
        System.out.println(s);
        beckett(s, n);
    }
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        graycodes(n);
    }
}

Is there any way that I can improve my program?
Thanks for your attention.
I have some suggestions for your code.
Always add curly braces to loop &amp; if
In my opinion, it's a bad practice to have a block of code not surrounded by curly braces; I saw so many bugs in my career related to that, if you forget to add the braces when adding code, you break the logic / semantic of the code.
Use java.lang.StringBuilder to concatenate String in a loop.
It's generally more efficient to use the builder in a loop, since the compiler is unable to make it efficient in a loop; since it creates a new String each iteration. There are lots of good explanations with more details on the subject.
before
String s = &quot;&quot;;
for (int i = 0; i &lt; n; i++) {
   s += &quot;0&quot;;
}

after
StringBuilder s = new StringBuilder();
for (int i = 0; i &lt; n; i++) {
   s.append(&quot;0&quot;);
}

Don’t use a loop when you want to repeat a string of x elements
The API that java give you offer a method to repeat the character x times; you can use java.lang.String#repeat (Java 11+)
before
StringBuilder s = new StringBuilder();
for (int i = 0; i &lt; n; i++) {
   s.append(&quot;0&quot;);
}

after
String s = &quot;0&quot;.repeat(n);

Extract the expression to variables when used multiple times.
In your code, you can extract the similar expressions into variables; this will make the code shorter and easier to read.
before
beckett(s, n - 1);
System.out.println(swap(s, &quot;1&quot;, n));
s = swap(s, &quot;1&quot;, n);
beckett(s, n - 1);

after
int previousNumber = n - 1;
beckett(s, previousNumber);
System.out.println(swap(s, &quot;1&quot;, n));
s = swap(s, &quot;1&quot;, n);
beckett(s, previousNumber);

Uses the increment and decrement operators when possible
Try to use the x++ / ++x, x-- or --x instead of x = x + 1 since they are more widely used and make the code easier to read.
before
n = n - 1;

after
n--;