Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a class called Lab9.java with the following static methods: (a) int[][]

ID: 3771786 • Letter: 1

Question

1. Write a class called Lab9.java with the following static methods: (a) int[][] random(int N, int start, int end): returns an N-by-N matrix of random integers ranging from start to end; (b) int rowSum(int[][] a, int i): returns the sum of the elements in row i of the 2-D array a; (c) int colSum(int[][] a, int j): returns the sum of the elements in column j of the 2-D array a; (d) boolean isSquare(int[][] a): returns true if the 2-D array a is square (i.e. the number of rows and columns are the same); (e) boolean isLatin(int[][] a): returns true if the 2-D array a is a Latin square (i.e. an n-byn matrix such that each row and each column contains the values from 1 through n with no repeats); (f) main(String[] args): the main method should test each method above on ve randomly generated matrices and also these ones: int[][] allneg = { {-10,-12,-3}, {-4,-5,-6,-8}, {-7,-8} }; int[][] nonsquare = { {1,2,3}, {4,5}, {6,7,8,9} }; int[][] latin = { {1,2,3}, {2,3,1}, {3,1,2} }; int[][] notlatin = { {2,1,3}, {2,3,1}, {3,1,2} }; 2. Write a program Dungeon.java that takes 2 integer and 1 double (from 0 to 1 - representing a probability) inputs from System.in: M, N, and p and produces an M-by-N boolean array where each entry repre-sents a room in a dungeon that is occupied (i.e., marked true) with probability p. We wish to represent a dungeon game: true cells represent rooms that are occupied by a terrible monster and false cells represent safe rooms. Print out the array using an ‘m’ for monster and a period for safe rooms (note: you just have to print this as a well formatted grid, you don’t have to make a new array). Part 2: replace each safe room with the number of neighboring monsters (above, below, left, right, or diagonal) and print out the solution (again, you just have to print this as a grid using formatting). Try to write your code so that you have as few special cases as possible to deal with, by using an (M + 2)-by-(N + 2) boolean array. Below is an example grid of a dungeon, and its solution. Optional: The goal of this game is undetermined and beyond this exercise, but it might be fun to come up with some goals and features to add to your program (for example, we could add treasure). m . . . . . . . . m . . . . . . m . . . . . . . . . . . m m . . . m m m . . m . . . . m . . . . . . m 1 0 0 0 1 1 1 1 m 1 1 0 0 0 1 m 2 3 3 0 0 1 2 3 3 2 3 m m 0 0 2 m m m 1 2 m 3 0 0 2 m 4 2 1 1 1 1

Explanation / Answer


public class Dungeon {
    private boolean[][] isRoom;      
    private boolean[][] isCorridor;  
    private int N;                   


    public Dungeon(char[][] board) {
        N = board.length;
        isRoom     = new boolean[N][N];
        isCorridor = new boolean[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if      (board[i][j] == '.') isRoom[i][j] = true;
                else if (board[i][j] == '+') isCorridor[i][j] = true;
            }
        }
    }

  
    public int size()
    {
    return N;
    }


    public boolean isCorridor(Site v)
    {
        int i = v.i();
        int j = v.j();
        if (i < 0 || j < 0 || i >= N || j >= N)
        return false;
        return isCorridor[i][j];
    }


    public boolean isRoom(Site v)
    {
        int i = v.i();
        int j = v.j();
        if (i < 0 || j < 0 || i >= N || j >= N) return false;
        return isRoom[i][j];
    }

  
    public boolean isWall(Site v)
    {
        return (!isRoom(v) && !isCorridor(v));
    }

    public boolean isLegalMove(Site v, Site w) {
        int i1 = v.i();
        int j1 = v.j();
        int i2 = w.i();
        int j2 = w.j();
        if (i1 < 0 || j1 < 0 || i1 >= N || j1 >= N) return false;
        if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) return false;
        if (isWall(v) || isWall(w)) return false;
        if (Math.abs(i1 - i2) > 1) return false;
        if (Math.abs(j1 - j2) > 1) return false;
        if (isRoom(v) && isRoom(w)) return true;
        if (i1 == i2)               return true;
        if (j1 == j2)               return true;

        return false;
    }

}