# 6: Strings, Arrays and Collections

## 6 Strings, Arrays and Collections

### 6.1 Print 2D Array

The following code creates a 3 x 3 tic-tac-toe grid filled with Xs.

``` 1: void tictactoe()
2:    {
3:       String [][] grid = new String[3][3];
4:
5:       for(int i = 0; i<grid.length; i++)
6:       {
7: 	  for (int j = 0; j<grid.length; j++)
8: 	  {
9: 	      grid[i][j] = "X";
10: 	  }
11:       }
12:       printArray(grid);
13:
14:    }
```

Write a method that will print a 2D array of any size. Test it using the 3 x 3 tic-tac-toe grid given above

#### 6.1.1 Example

```1: tictactoe()
2:  *** Output ***
3:  | X | X | X |
4:  | X | X | X |
5:  | X | X | X |
```

### 6.2 Chess Board

Create an 8 x 8 Array to represent a chess board. Print out the array with alternate ‘X’ and ‘O’ entries as shown in the example:

#### 6.2.1 Example

``` 1: chess();
2:  *** Output ***
3:  | X | O | X | O | X | O | X | O |
4:  | O | X | O | X | O | X | O | X |
5:  | X | O | X | O | X | O | X | O |
6:  | O | X | O | X | O | X | O | X |
7:  | X | O | X | O | X | O | X | O |
8:  | O | X | O | X | O | X | O | X |
9:  | X | O | X | O | X | O | X | O |
10:  | O | X | O | X | O | X | O | X |
```

### 6.3 Digits to Words

Write a method that will convert digits into words

#### 6.3.1 Example

```1: System.out.println(digitsToWords("35001922"));
2:  *** Output ***
3: Three Five Oh Oh One Nine Two Two
```

### 6.4 Time to Words

Write a method that will convert time to words. For example, 4:10 is “Ten past four” and 4:55 is “Five to Five”

#### 6.4.1 Example

``` 1: System.out.println(timeToWords("2:05"));
2: System.out.println(timeToWords("2:47"));
3: System.out.println(timeToWords("12:00"));
4: System.out.println(timeToWords("2:00"));
5: System.out.println(timeToWords("12:30"));
6: System.out.println(timeToWords("4:29"));
7: System.out.println(timeToWords("7:25"));
8: System.out.println(timeToWords("12:45"));
9: System.out.println(timeToWords("12:55"));
10:  *** Output ***
11: five past two
12: thirteen minutes to two
13: twelve o'clock
14: two o'clock
15: half past past twelve
16: twenty nine minutes past four
17: twenty five past seven
18: Quarter to one
19: five to one
```

### 6.5 Morse Code Arrays

Write a program using arrays that translates plain text into morse code.

 A B C D E F G .- -… -.-. -.. . ..-. –. H I J K L M …. .. .— -.- .-.. — N O P Q R S T -. — .–. –.- .-. … – U V W X Y Z ..- …- .– -..- -.– __..

#### 6.5.1 Example

```1: String s = "I never saw a purple cow"
2:
3: morse(s)
4:  *** Output ***
5: .. / -. . ...- . .-. / ... .- .-- / .- / .--. ..- .-. .--. .-.. . / -.-. --- .--
```

### 6.6 Demorse Code Arrays

Now write a program using arrays that translates morse back to plain text.

#### 6.6.1 Example

```1: String s = ".. -././...-/./.-. .../.-/.-- .- .--./..-/.-./.--./.-../. -.-./---/.--"
2:
3: deMorse(s)
4:  *** Output ***
5: "I never saw a purple cow"
```

### 6.7 Morse Code Hashmap

Rewrite the Morse Code program using Hashmap

#### 6.7.1 Example

```1: String s = "I never saw a purple cow"
2: morseHash(s)
3:  *** Output ***
4: .. / -. . ...- . .-. / ... .- .-- / .- / .--. ..- .-. .--. .-.. . / -.-. --- .--
```

### 6.8 Demorse Code Hashmap

Rewrite the Demorse Code program using Hashmap

#### 6.8.1 Example

```1: String s = ".. -././...-/./.-. .../.-/.-- .- .--./..-/.-./.--./.-../. -.-./---/.--"
2: deMorseHash(s)
3:  *** Output ***
4: "I never saw a purple cow"
```