CSSSKL161 Lab 9 University of Washington B othe Autumn 2015 1. Write a class cal
ID: 3864199 • Letter: C
Question
CSSSKL161 Lab 9 University of Washington B othe Autumn 2015 1. Write a class called Lab9, java with the following static methods: a int DJ O 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 calSum(int CJ CJ a, int j returns the sum of the clements in co mn j of the 2-D array a (d) boolean iesquare (int Cl CJ a): returns true if the 2-D array a is square (i.e. the number of rows and columns are the samek (e) boolean isLatin int a) returns true if the 2-D array a is a square (i.e. an n-byra matrix such that each row and each column contains the values from 1 through n with no repeats) (f) main (String Cl args): the main method should test, each method above on five randomly generated matrices and also the ones int 0 allneg 12 {-4,-5, -6,-8 1-7 int CJ O nonsquare 1 11,2,3 14,51, 16,7, 8,91 int [10 latin. 11,2, 12,3, 1 Y, (3,1,2) int 0 notlatin 12 12,3,1 13,1,2) 2. Write a program Dungeon .java that takes 2 integer and 1 double (from 0 to 1-representing a probability inputs from Systen.in: M, N, and p and produces an M-by-N boolcan array where each entry rcprc- 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 roomus that are occupied by a terrible monster and false cells represent safe rooms. Print out the array using an im for monster and a period for safe rooms inote: you just have to print this as a well formatted grid, yau doll t have to ake a new array) Part 2: replace each safe room with the nuaber 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 yo r program (for example, we could add trcasure) 10 00 1 1 1 1 1 1 0 0 0 1 2 3 3 0 0 1 2 3 3 2 3 m 00 2 m ma m 1 2 m 3 0 0 2 m 4 2 1 1 1Explanation / Answer
Answer to first question and its parts :
import java.util.Random;
public class Lab9 {
public static int[][] random(int N, int start, int end)
{
Random rm = new Random();
int[][] matrix = new int[N][N];
//Initialize all the array with a value between start , end (including both ends)
for(int i =0; i<N; i++)
for(int j=0;j<N;j++)
{
int num = start + rm.nextInt(end-start+1); //rm.nextInt generates a number between 0 to input(not inclusive)
matrix[i][j] = num;
}
return matrix;
}
// returns sum of all the elements in that row
public static int rowSum(int[][]a, int i)
{
int sum = 0;
int rowIndex = i - 1;
for(int col=0; col<a[rowIndex].length;col++)
{
sum += a[rowIndex][col];
}
return sum;
}
//Returns sum of all elements in the column
// if there is no column in some rows, we are skipping that row
public static int colSum(int[][]a, int j)
{
int sum =0;
int colIndex = j-1;
for(int row=0; row<a.length;row++)
{
if(a[row].length >= j)
sum += a[row][colIndex];
}
return sum;
}
// check each row size is equal to number of rows.
public static boolean isSquare(int[][] a)
{
boolean ret = true;
for(int row =0; row < a.length ; row++)
if(a.length != a[row].length)
{
ret = false;
break;
}
return ret;
}
// Latin means : it should be perfect square
// All the values should be between 1 to rowsize
// 1 to rowsize value should repeat only once in a row
public static boolean isLatin(int[][] a)
{
boolean ret = true;
for(int i=0;i<a.length;i++)
{
int[] count = new int[a.length];
if(a.length != a[i].length)
{
ret =false;
break;
}
for(int j=0;j<a[i].length;j++)
{
if((a[i][j] > a.length) || (a[i][j] <= 0 )|| count[a[i][j]-1] != 0)
{
ret = false;
break;
}
count[a[i][j]-1] = 1;
}
if(!ret)
break;
}
return ret;
}
public static void main(String[] args) {
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[][] nonlatin = {{0,1,3},{2,3,1},{3,1,2}};
int[][] randomArray1 = random(5,6,10);
int[][] randomArray2 = random(4,6,12);
int[][] randomArray3 = random(7,3,10);
int[][] randomArray4 = random(4,4,9);
System.out.println("Results of running methods on allneg");
System.out.println("Sum of 3rd row: " + rowSum(allneg, 3));
System.out.println("Sum of 3rd column: " + colSum(allneg,3));
System.out.println("is a square ? " + isSquare(allneg));
System.out.println("is a Latin ? " + isLatin(allneg));
System.out.println("Results of running methods on nonsquare");
System.out.println("Sum of 3rd row: " + rowSum(nonsquare, 1));
System.out.println("Sum of 3rd column: " + colSum(nonsquare,1));
System.out.println("is a square ? " + isSquare(nonsquare));
System.out.println("is a Latin ? " + isLatin(nonsquare));
System.out.println("Results of running methods on latin");
System.out.println("Sum of 3rd row: " + rowSum(latin, 1));
System.out.println("Sum of 3rd column: " + colSum(latin,1));
System.out.println("is a square ? " + isSquare(latin));
System.out.println("is a Latin ?" + isLatin(latin));
System.out.println("Results of running methods on nonlatin");
System.out.println("Sum of 3rd row: " + rowSum(nonlatin, 1));
System.out.println("Sum of 3rd column: " + colSum(nonlatin,1));
System.out.println("is a square ?" + isSquare(nonlatin));
System.out.println("is a Latin ?" + isLatin(nonlatin));
System.out.println("Results of running methods on randomArray1");
System.out.println("Sum of 3rd row: " + rowSum(randomArray1, 1));
System.out.println("Sum of 3rd column: " + colSum(randomArray1,1));
System.out.println("is a square ? " + isSquare(randomArray1));
System.out.println("is a Latin ? " + isLatin(randomArray1));
System.out.println("Results of running methods on randomArray2");
System.out.println("Sum of 3rd row: " + rowSum(randomArray2, 1));
System.out.println("Sum of 3rd column: " + colSum(randomArray2,1));
System.out.println("is a square ? " + isSquare(randomArray2));
System.out.println("is a Latin ? " + isLatin(randomArray2));
System.out.println("Results of running methods on randomArray3");
System.out.println("Sum of 3rd row: " + rowSum(randomArray3, 1));
System.out.println("Sum of 3rd column: " + colSum(randomArray3,1));
System.out.println("is a square ? " + isSquare(randomArray3));
System.out.println("is a Latin ? " + isLatin(randomArray3));
System.out.println("Results of running methods on randomArray4");
System.out.println("Sum of 3rd row: " + rowSum(randomArray4, 1));
System.out.println("Sum of 3rd column: " + colSum(randomArray4,1));
System.out.println("is a square ? " + isSquare(randomArray4));
System.out.println("is a Latin ? " + isLatin(randomArray4));
}
}