File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public class Board implements Cloneable {
2+ public static final int NO_OF_ROWS = 8 ;
3+ public static final int NO_OF_COLUMNS = 8 ;
4+
5+ private final Cell [][] board ;
6+
7+ public Board () {
8+ this .board = new Cell [NO_OF_ROWS ][NO_OF_COLUMNS ];
9+
10+ for (int row = NO_OF_ROWS - 1 ; row >= 0 ; row --) {
11+ for (int col = NO_OF_COLUMNS - 1 ; col >= 0 ; col --) {
12+ if ((row + col ) % 2 == 0 ) {
13+ board [row ][col ] = new Cell ("WHITE" );
14+ } else {
15+ board [row ][col ] = new Cell ("BLACK" );
16+ }
17+ }
18+ }
19+ }
20+
21+ public void print () {
22+ for (int row = 0 ; row < NO_OF_ROWS ; row ++) {
23+ for (int col = 0 ; col < NO_OF_COLUMNS ; col ++) {
24+ System .out .print (board [row ][col ] + " " );
25+ }
26+ System .out .println ();
27+ }
28+ }
29+
30+ @ Override
31+ public Object clone () {
32+ Object obj = null ;
33+ try {
34+ obj = super .clone ();
35+ } catch (CloneNotSupportedException e ) {
36+ e .printStackTrace ();
37+ }
38+ return obj ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ public class Cell implements Cloneable {
2+ private String color ;
3+
4+ public Cell (String color ) {
5+ this .color = color ;
6+
7+ // Make it time consuming task.
8+ try {
9+ Thread .sleep (100 );
10+ } catch (InterruptedException e ) {
11+ }
12+ }
13+
14+ public String getColor () {
15+ return color ;
16+ }
17+
18+ @ Override
19+ public String toString () {
20+ return color .substring (0 , 1 );
21+ }
22+
23+ @ Override
24+ public Object clone () {
25+ Object obj = null ;
26+ try {
27+ obj = super .clone ();
28+ } catch (CloneNotSupportedException e ) {
29+ e .printStackTrace ();
30+ }
31+ return obj ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+
2+ public class Main {
3+
4+ public static void main (String [] args ) {
5+ // Get the start time
6+ long startTime = System .currentTimeMillis ();
7+
8+ Board chessBoard = new Board ();
9+
10+ // Get the end time
11+ long endTime = System .currentTimeMillis ();
12+
13+ System .out .println ("Time taken to create a board: " + (endTime - startTime ) + " millis" );
14+
15+ // Print the board
16+ chessBoard .print ();
17+
18+ System .out .println ();
19+
20+ // Get the start time
21+ startTime = System .currentTimeMillis ();
22+
23+ Board checkersBoard = (Board ) chessBoard .clone ();
24+
25+ // Get the end time
26+ endTime = System .currentTimeMillis ();
27+
28+ System .out .println ("Time taken to clone a board: " + (endTime - startTime ) + " millis" );
29+
30+ checkersBoard .print ();
31+ }
32+
33+ }
You can’t perform that action at this time.
0 commit comments