Skip to main content
added 9 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Your use of the temp variable makes your code particularly infuriating to follow:

Your use of the temp makes your code particularly infuriating to follow:

Your use of the temp variable makes your code particularly infuriating to follow:

Simplification
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
import java.io.PrintStream;
import java.util.Arrays;

public class Phi {
    /**
     * Produces a string of spaces that has the character <code>c</code>
     * at the three specified indices, followed by a newline.
     */
    private static String row(char c, int left, int mid, int right) {
        char[] line = new char[right + 2];
        Arrays.fill(line, ' ');
        line[left] = line[mid] = line[right] = c;
        line[right + 1] = '\n';
        return new String(line);
    }

    public static String toString(char c, int size) {
        StringBuilder out = new StringBuilder();
        int halfWidth = (size + 1) / 2;

        // Top stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        // Body
        for (int row = 2; row <<= (size + 2) / 2; row++) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }
        for (int row = halfWidth; row >= 2; row--) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }

        // Bottom stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        return out.toString();
    }

    public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
        System.out.printf("\nNumber = %d\n%s", size, toString('*', size));
    }
}
import java.io.PrintStream;
import java.util.Arrays;

public class Phi {
    /**
     * Produces a string of spaces that has the character <code>c</code>
     * at the three specified indices, followed by a newline.
     */
    private static String row(char c, int left, int mid, int right) {
        char[] line = new char[right + 2];
        Arrays.fill(line, ' ');
        line[left] = line[mid] = line[right] = c;
        line[right + 1] = '\n';
        return new String(line);
    }

    public static String toString(char c, int size) {
        StringBuilder out = new StringBuilder();
        int halfWidth = (size + 1) / 2;

        // Top stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        // Body
        for (int row = 2; row < (size + 2) / 2; row++) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }
        for (int row = halfWidth; row >= 2; row--) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }

        // Bottom stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        return out.toString();
    }

    public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
        System.out.printf("\nNumber = %d\n%s", size, toString('*', size));
    }
}
import java.util.Arrays;

public class Phi {
    /**
     * Produces a string of spaces that has the character <code>c</code>
     * at the three specified indices, followed by a newline.
     */
    private static String row(char c, int left, int mid, int right) {
        char[] line = new char[right + 2];
        Arrays.fill(line, ' ');
        line[left] = line[mid] = line[right] = c;
        line[right + 1] = '\n';
        return new String(line);
    }

    public static String toString(char c, int size) {
        StringBuilder out = new StringBuilder();
        int halfWidth = (size + 1) / 2;

        // Top stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        // Body
        for (int row = 2; row <= size / 2; row++) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }
        for (int row = halfWidth; row >= 2; row--) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }

        // Bottom stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        return out.toString();
    }

    public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
        System.out.printf("\nNumber = %d\n%s", size, toString('*', size));
    }
}
Avoid passing PrintStream
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
import java.io.PrintStream;
import java.util.Arrays;

public class Phi {
    /**
     * Produces a string of spaces that has the character <code>c</code>
     * at the three specified indices, followed by a newline.
     */
    private static String row(char c, int left, int mid, int right) {
        char[] line = new char[right + 1];2];
        Arrays.fill(line, ' ');
        line[left] = line[mid] = line[right] = c;
        line[right + 1] = '\n';
        return new String(line);
    }

    public static voidString printPhitoString(PrintStream out, char c, int size) {
        StringBuilder out = new StringBuilder();
        int halfWidth = (size + 1) / 2;

        // Top stem
        out.printlnappend(row(c, halfWidth, halfWidth, halfWidth));

        // Body
        for (int row = 2; row < (size + 2) / 2; row++) {
            out.printlnappend(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }
        for (int row = halfWidth; row >= 2; row--) {
            out.printlnappend(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }

        // Bottom stem
        out.printlnappend(row(c, halfWidth, halfWidth, halfWidth)); 

        return out.toString();
    }

    public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
        System.out.printf("\nNumber = %d\n"%d\n%s", size);
        printPhi(System.out, toString('*', Integer.parseInt(args[0]size));
    }
}
import java.io.PrintStream;
import java.util.Arrays;

public class Phi {
    /**
     * Produces a string of spaces that has the character <code>c</code>
     * at the three specified indices.
     */
    private static String row(char c, int left, int mid, int right) {
        char[] line = new char[right + 1];
        Arrays.fill(line, ' ');
        line[left] = line[mid] = line[right] = c;
        return new String(line);
    }

    public static void printPhi(PrintStream out, char c, int size) {
        int halfWidth = (size + 1) / 2;

        // Top stem
        out.println(row(c, halfWidth, halfWidth, halfWidth));

        // Body
        for (int row = 2; row < (size + 2) / 2; row++) {
            out.println(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }
        for (int row = halfWidth; row >= 2; row--) {
            out.println(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }

        // Bottom stem
        out.println(row(c, halfWidth, halfWidth, halfWidth));
    }

    public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
        System.out.printf("\nNumber = %d\n", size);
        printPhi(System.out, '*', Integer.parseInt(args[0]));
    }
}
import java.io.PrintStream;
import java.util.Arrays;

public class Phi {
    /**
     * Produces a string of spaces that has the character <code>c</code>
     * at the three specified indices, followed by a newline.
     */
    private static String row(char c, int left, int mid, int right) {
        char[] line = new char[right + 2];
        Arrays.fill(line, ' ');
        line[left] = line[mid] = line[right] = c;
        line[right + 1] = '\n';
        return new String(line);
    }

    public static String toString(char c, int size) {
        StringBuilder out = new StringBuilder();
        int halfWidth = (size + 1) / 2;

        // Top stem
        out.append(row(c, halfWidth, halfWidth, halfWidth));

        // Body
        for (int row = 2; row < (size + 2) / 2; row++) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }
        for (int row = halfWidth; row >= 2; row--) {
            out.append(row(c, halfWidth - row, halfWidth, halfWidth + row));
        }

        // Bottom stem
        out.append(row(c, halfWidth, halfWidth, halfWidth)); 

        return out.toString();
    }

    public static void main(String[] args) {
        int size = Integer.parseInt(args[0]);
        System.out.printf("\nNumber = %d\n%s", size, toString('*', size));
    }
}
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading