Sahil-Pattni
10/12/2016 - 7:25 PM

ArrayBuilder

ArrayBuilder

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by spattni on 10/12/16.
 */
public class ArrayBuilder {

    private char baseLetter;
    private int n, m;

    public ArrayBuilder(char baseLetter, int n, int m) {
        this.baseLetter = baseLetter;
        this.n = n;
        this.m = m;
    }

    public char getBaseLetter() {
        return this.baseLetter;
    }

    public int getRows() {
        return this.n;
    }

    public int getColumns() {
        return this.m;
    }

    public char[][] build() {
        char[][] letterArray = new char[getRows()][getColumns()];

        letterArray[0][0] = baseLetter;

        for (int y = 0; y < letterArray.length; y++) {
            for (int x = 0; x < letterArray[y].length; x++) {
                if (!(y == 0 && x == 0)) {
                    char assign = baseLetter;

                    if (baseLetter >= 'A' && baseLetter <= 'Z') {
                        if (assign + (y + x) <= 'Z') {
                            assign += (y + x);
                            letterArray[y][x] = assign;
                        }

                        else {
                            assign -= 26-(y+x);
                            letterArray [y][x] = assign;
                        }
                    }

                    else if (baseLetter >= 'a' && baseLetter <= 'z') {
                        if (assign + (y+x) <= 'z') {
                            assign += (y+x);
                            letterArray [y][x] = assign;
                        }
                        else {
                            assign -= 26-(y+x);
                            letterArray [y][x] = assign;
                        }
                    }
                }
            }
        }

        return letterArray;
    }

    public char[][] cloner(char[][] input) {
        if (input == null) {
            return null;
        }

        char[][] result = new char[input.length][];
        for (int r = 0; r < input.length; r++) {
            result[r] = input[r].clone();
        }

        return result;
    }

    public char[][] getLetterArray() {
        return cloner(build());
    }

    public void printLetterArray() {
        System.out.println(Arrays.deepToString(getLetterArray()));
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        System.out.println("Enter your base letter");
        char baseLetter = s.next().charAt(0);
        System.out.println("Enter the number of rows");
        int rows = s.nextInt();
        System.out.println("Enter the number of columns");
        int columns = s.nextInt();

        ArrayBuilder xxx = new ArrayBuilder(baseLetter, rows, columns);

        xxx.printLetterArray();
    }
}