Skip to content

MultiverseLearningProducts/JSE-U5-Async-Activities

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unit 5: 2D Arrays and Grid-Based Programming - Starter Template

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.

Project Structure

unit-5-work/
├── src/main/java/
│   └── Battleship.java (Starter template with TODOs)
├── README.md
└── .gitignore

Your Task: Mapping a Battleship Grid

Objective: Use a 2D array to model a grid and write a simple algorithm to find a target within it.

What You Need to Implement

  1. 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];
  2. 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)
  3. Display the grid

    // TODO: Display the grid
    // Hint: Print row/column headers and iterate through the grid
  4. 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

Expected Output

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

Learning Objectives

  • 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

Key Concepts to Remember

1. 2D Array Declaration

char[][] grid = new char[5][5];  // 5 rows, 5 columns

2. Nested Loop Pattern

for (int row = 0; row < grid.length; row++) {
    for (int col = 0; col < grid[row].length; col++) {
        // Process grid[row][col]
    }
}

3. Coordinate System

  • 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

4. Grid Operations

  • Access: grid[row][col]
  • Set: grid[row][col] = 'S';
  • Check: if (grid[row][col] == 'S')

How to Run

Compilation

# Compile the Java file
javac src/main/java/Battleship.java

Execution

# Run the program
java -cp src/main/java Battleship

Success Criteria

✅ 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

Tips for Success

  1. Start Simple: Begin with just declaring the 2D array
  2. Test Incrementally: Add one feature at a time and test
  3. Use Nested Loops: Remember the pattern for 2D array iteration
  4. Check Bounds: Make sure your array indices are valid
  5. Print Debug Info: Use System.out.println to see what's happening

Common Pitfalls

  • Array Index Out of Bounds: Remember arrays are 0-indexed
  • Wrong Loop Bounds: Use grid.length for rows, grid[row].length for columns
  • Coordinate Confusion: Remember grid[row][col] not grid[col][row]
  • Missing Return: Don't forget to return after finding the ship

Next Steps

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! 🚢

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published