Learn Java Easy. Chapter 8 Exercises

Chapter 8 Exercises

Exercise 1. Create a 5×5 2D matrix that represents the cells of a prison. Fill all positions with the value 0, indicating that all cells are empty.

 

Solution:

 

public class Main {
    public static void main(String[] args) {
        int[][] cells = new int[5][5];
        
        // Fill the matrix with 0
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j] = 0;
            }
        }
        
        // Print the matrix
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 2. Let’s assume that cells [0][1], [2][2], and [4][3] are occupied by prisoners. Update the matrix from the previous exercise to reflect this using the value 1 for occupied cells.

 

Solution:

 

public class Main {
    public static void main(String[] args) {
        int[][] cells = new int[5][5];
        
        // Fill the matrix with 0
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j] = 0;
            }
        }
        
        // Mark the occupied cells
        cells[0][1] = 1;
        cells[2][2] = 1;
        cells[4][3] = 1;
        
        // Print the matrix
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 3. Count how many cells are occupied in the matrix created in exercise 2.

 

Solution:

 

public class Main {
    public static void main(String[] args) {
        
        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        int counter = 0;
        
        // Count occupied cells
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                if (cells[i][j] == 1) {
                    counter++;
                }
            }
        }
        
        System.out.println("Occupied cells: " + counter);
    }
}

Exercise 4. Release the prisoner from cell [2][2] in the matrix from exercise 2 and update the matrix.

 

Solution:

 

public class Main {
    public static void main(String[] args) {
        
        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        // Release the prisoner
        cells[2][2] = 0;
        
        // Print the matrix
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 5. Find all empty cells in the matrix from exercise 2 and display their positions.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        System.out.println("Empty cells:");

        // Find empty cells
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                if (cells[i][j] == 0) {
                    System.out.println("[" + i + "][" + j + "]");
                }
            }
        }
    }
}

Exercise 6. Transfer a prisoner from cell [4][3] to cell [1][4] in the matrix from exercise 2.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        // Transfer the prisoner
        cells[4][3] = 0;
        cells[1][4] = 1;
        
        // Print the matrix
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 7. Count how many occupied neighboring cells the cell [2][2] has in the matrix from exercise 2.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        int[][] directions = {
            {-1, -1}, {-1, 0}, {-1, 1},
            {0, -1},           {0, 1},
            {1, -1},  {1, 0},  {1, 1}
        };
        
        int x = 2, y = 2;
        int counter = 0;
        
        // Count occupied neighboring cells
        for (int[] dir : directions) {
            int newX = x + dir[0];
            int newY = y + dir[1];
            if (newX >= 0 && newX < cells.length && newY >= 0 && newY < cells[0].length) {
                if (cells[newX][newY] == 1) {
                    counter++;
                }
            }
        }
        
        System.out.println("Occupied neighboring cells of [2][2]: " + counter);
    }
}

๐Ÿ’ก Initialization and Declaration: Always initialize your matrix correctly after declaring it. You can do it in a single line to keep the code clean and easy to read.

๐Ÿ’ก Use of Nested Loops: Use nested loops to traverse and manipulate elements in the matrix. The outer loop iterates through rows and the inner loop iterates through columns. This is essential for performing operations on each element of the matrix.

Exercise 8. Find all prisoners in row 0 of the matrix from exercise 2 and display their positions.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        System.out.println("Prisoners in row 0:");

        for (int j = 0; j < cells[0].length; j++) {
            if (cells[0][j] == 1) {
                System.out.println("[0][" + j + "]");
            }
        }
    }
}

๐Ÿ’ก Check Boundaries: Make sure you don’t exceed the matrix limits. Always verify that your indices are within the valid range.

Exercise 9. Rotate the matrix from exercise 2, 90 degrees clockwise.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        int[][] rotated = new int[cells[0].length][cells.length];
        
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                rotated[j][cells.length - 1 - i] = cells[i][j];
            }
        }
        
        // Print the rotated matrix
        for (int i = 0; i < rotated.length; i++) {
            for (int j = 0; j < rotated[i].length; j++) {
                System.out.print(rotated[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 10. Check if there is an escape pattern in the matrix from exercise 2, defined as three consecutive occupied cells in any row.

 

Solution:

 

public class Main {
    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };
        
        boolean escapeFound = false;
        
        for (int i = 0; i < cells.length && !escapeFound; i++) {
            int consecutive = 0;
            for (int j = 0; j < cells[i].length; j++) {
                if (cells[i][j] == 1) {
                    consecutive++;
                    if (consecutive == 3) {
                        escapeFound = true;
                        break;
                    }
                } else {
                    consecutive = 0;
                }
            }
        }
        
        if (escapeFound) {
            System.out.println("Escape pattern found!");
        } else {
            System.out.println("No escape pattern found.");
        }
    }
}

Did you like it? Donโ€™t keep it to yourself โ€” share it like juicy gossip! ๐Ÿ˜