This is a starter template for learning 2D arrays and grid-based programming. Complete the TODO items in Battleship.java to implement a Battleship grid mapping system.
unit-5-work/
├── src/main/java/
│ └── Battleship.java (Starter template with TODOs)
├── README.md
└── .gitignore
Objective: Use a 2D array to model a grid and write a simple algorithm to find a target within it.
-
Declare and initialize a 5x5 2D array of characters
// TODO: Declare and initialize a 5x5 2D array of characters // Hint: Use char[][] grid = new char[5][5];
-
Populate the grid with water ('~') and place a ship ('S')
// TODO: Populate the grid with water ('~') and place a ship ('S') // Hint: Use nested loops to fill with '~', then place 'S' at coordinates (2, 3)
-
Display the grid
// TODO: Display the grid // Hint: Print row/column headers and iterate through the grid
-
Search for the ship using nested loops
// TODO: Search for the ship using nested loops // Hint: Use nested for loops to find the 'S' and print its coordinates
When completed, your program should produce output like this:
=== Battleship Grid Mapping ===
Using 2D arrays to model a grid and find targets
--- Initializing Grid ---
Grid initialized with water ('~') and ship ('S') at (2, 3)
--- Grid Display ---
0 1 2 3 4
0 ~ ~ ~ ~ ~
1 ~ ~ ~ ~ ~
2 ~ ~ ~ S ~
3 ~ ~ ~ ~ ~
4 ~ ~ ~ ~ ~
--- Searching for Ship ---
Ship found at row 2, column 3!
=== Key Learning Points ===
1. 2D arrays are perfect for representing grids
2. Nested loops allow systematic traversal of 2D arrays
3. Coordinate systems help locate elements in grids
4. Grid-based algorithms are fundamental in programming
5. 2D arrays are used in games, spreadsheets, and image processing
- Understand 2D array declaration and initialization
- Learn nested loop iteration through 2D arrays
- Practice grid-based algorithms
- Master coordinate system navigation
- Apply 2D arrays to real-world scenarios
char[][] grid = new char[5][5]; // 5 rows, 5 columnsfor (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
// Process grid[row][col]
}
}- Row Index: First dimension (vertical position)
- Column Index: Second dimension (horizontal position)
- Origin: (0, 0) is the top-left corner
- Bounds: Valid indices are 0 to length-1
- Access:
grid[row][col] - Set:
grid[row][col] = 'S'; - Check:
if (grid[row][col] == 'S')
# Compile the Java file
javac src/main/java/Battleship.java# Run the program
java -cp src/main/java Battleship✅ 2D array is properly declared and initialized
✅ Grid is populated with water ('~') and ship ('S')
✅ Nested loops iterate through all grid positions
✅ Ship coordinates are correctly identified and printed
✅ Code compiles and runs without errors
✅ Program produces the expected output
- Start Simple: Begin with just declaring the 2D array
- Test Incrementally: Add one feature at a time and test
- Use Nested Loops: Remember the pattern for 2D array iteration
- Check Bounds: Make sure your array indices are valid
- Print Debug Info: Use System.out.println to see what's happening
- Array Index Out of Bounds: Remember arrays are 0-indexed
- Wrong Loop Bounds: Use
grid.lengthfor rows,grid[row].lengthfor columns - Coordinate Confusion: Remember
grid[row][col]notgrid[col][row] - Missing Return: Don't forget to return after finding the ship
After completing this task, you'll understand:
- How to work with 2D arrays
- Nested loop patterns for grid traversal
- Coordinate systems and bounds checking
- Basic grid-based algorithms
This foundation will prepare you for more advanced topics like:
- 3D arrays and multi-dimensional data
- Graph algorithms and adjacency matrices
- Image processing and pixel manipulation
- Game development and spatial algorithms
Good luck with your implementation! 🚢