Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // JUnit test class:
- // https://pastebin.com/yCnsggc2
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class SquareWithDiagonalImpl {
- // Hard-coded implementation:
- public static void main(String[] args) {
- System.out.println("00000");
- System.out.println("01000");
- System.out.println("00200");
- System.out.println("00030");
- System.out.println("00004");
- }
- public static void simplisticImplementation_withSeparatePrintStatements(final int size) {
- for (int lineIndex = 0; lineIndex <= size; ++lineIndex) {
- for (int columnIndex = 0; columnIndex <= size; ++columnIndex) {
- if (lineIndex == columnIndex) {
- System.out.print(lineIndex);
- } else {
- System.out.print("0");
- }
- }
- System.out.println();
- }
- }
- public static void simplisticImplementation_withTernaryOperator(final int size) {
- for (int lineIndex = 0; lineIndex <= size; ++lineIndex) {
- for (int columnIndex = 0; columnIndex <= size; ++columnIndex) {
- System.out.print((lineIndex == columnIndex) ? lineIndex : "0");
- }
- System.out.println();
- }
- }
- public static void streamImplementation(final int size) {
- IntStream.rangeClosed(0, size).mapToObj((lineIndex) ->
- IntStream.rangeClosed(0, size).mapToObj((columnIndex) ->
- (lineIndex == columnIndex) ? String.valueOf(lineIndex) : "0"
- ).collect(Collectors.joining())
- ).forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement