SHARE
TWEET

Untitled

advictoriam Jan 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class SquareBoard
  4. {
  5.    private String[][] board;
  6.    private int length;
  7.    
  8.    /**
  9.       Constructs an empty square game board.
  10.       @param sideLength the length of each side
  11.    */
  12.    public SquareBoard(int sideLength)
  13.    {
  14.       length = sideLength;
  15.       board = new String[length][length];
  16.       // Fill with spaces
  17.       for (int i = 0; i < length; i++)
  18.          for (int j = 0; j < length; j++)
  19.             board[i][j] = " ";
  20.    }
  21.  
  22.    /**
  23.       Creates a string representation of the board, such as
  24.       |x o|
  25.       | x |
  26.       | o |.
  27.       @return the string representation
  28.    */
  29.    public String toString()
  30.    {
  31.       String r = "";
  32.       for (int i = 0; i < length; i++)
  33.       {
  34.          r = r + "|";
  35.          for (int j = 0; j < length; j++)
  36.             r = r + board[i][j];
  37.          r = r + "|\n";
  38.       }
  39.       return r;
  40.    }  
  41.  
  42.    /**
  43.       Fills the borders of this board with "*" strings.
  44.    */
  45.    public void fillBorders()
  46.    {
  47.       for(int i = 0; i < length; i++)
  48.       {
  49.          board[0][i] = "*";
  50.          board[length-1][i] = "*";
  51.       }
  52.      
  53.       for(int i = 0; i < length; i++)
  54.       {
  55.          board[i][0] = "*";
  56.          board[i][length-1] = "*";
  57.       }
  58.    }
  59.    
  60.    // This method is used to check your work.
  61.  
  62.    public static void main(String[] args)
  63.    {
  64.       Scanner in = new Scanner(System.in);
  65.       int n = in.nextInt();  
  66.       SquareBoard board = new SquareBoard(n);
  67.       board.fillBorders();
  68.       System.out.println(board.toString());
  69.    }  
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top