Create a class called Lab8b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today.

Respuesta :

Answer:

Attached is the full questions:

import java.util.*;

public class Lab8b

{

public static int[][] random(int N,int start,int end){

int[][] array = new int[N][N];

Random r= new Random();

for(int i=0;i<N;i++){

for(int j=0;j<N;j++){

int rand = r.nextInt(end-start) + start;

/* generate random number from start(inclusive) to end(exclusive)*/

array[i][j]=rand;

System.out.print(array[i][j]+" ");

}

System.out.println();

}

return array;

}

public static int rowSum(int[][] a,int i){

int answer=0;

//iterate over the row and calculate sum

for(int j=0;j<a[i].length;j++)

answer+=a[i][j];

return answer;

}

public static int colSum(int[][] a,int j){

int answer=0;

//iterate over the column and calculate sum

for(int i=0;i<a.length;i++)

answer+=a[i][j];

return answer;

}

public static boolean isSquare(int[][] a){

int rowlength=a[0].length;

//iterate over each row and check if number of columns are same

for(int i=0;i<a[0].length;i++){

if(a[i].length!=rowlength)

return false;

}

return true;

}

public static boolean isLatin(int[][] a){

if(!isSquare(a))return false;

HashSet<Integer> set=new HashSet();

//check rowwise first if the numbers are all distinct and in sequence

for(int i=0;i<a.length;i++){

//populate hashset the numbers between 1 and n

for(int c=1;c<=a.length;c++)

set.add(c);

/*remove from hashset and check if it empties the set

if yes the it is a latin row

else return false

*/

for(int j=0;j<a.length;j++){

if(!set.contains(a[i][j])) return false;

set.remove(a[i][j]);

}

if(set.size()!=0) return false;

}

//check columnwise first if the numbers are all distinct and in sequence

for(int i=0;i<a.length;i++){

//populate hashset the numbers between 1 and n

for(int c=1;c<=a.length;c++)

set.add(c);

/*remove from hashset and check if it empties the set

if yes the it is a latin column

else return false

*/

for(int j=0;j<a.length;j++){

if(!set.contains(a[j][i])) return false;

set.remove(a[j][i]);

}

if(set.size()!=0) return false;

}

return true;

}

public static void main(String[] args) {

int[][] ar= random(5 , 1 ,11);

System.out.println("Testing random array");

System.out.println("rowSum "+rowSum(ar,1)); //the indexing starts from 0

System.out.println("colSum "+colSum(ar,1)); //the indexing starts from 0

System.out.println("isSquare? " + isSquare(ar));

System.out.println("isLatin? " + isLatin(ar));

int[][] allNeg={{-10,-12,-3},{-4,-5,-6,-8},{-7,-8}};

int[][] nonSquare={{1,2,3},{4,5},{3,1,2}};

int[][] latin={{1,2,3},{2,3,1},{3,1,2}};

int[][] nonLatin={{2,1,3},{2,3,1},{3,1,2}};

System.out.println("Testing allNeg array");

System.out.println("rowSum "+rowSum(allNeg,1)); //the indexing starts from 0

System.out.println("colSum "+colSum(allNeg,1)); //the indexing starts from 0

System.out.println("isSquare? " + isSquare(allNeg));

System.out.println("isLatin? " + isLatin(allNeg));

System.out.println("Testing nonSquare array");

System.out.println("rowSum "+rowSum(nonSquare,1)); //the indexing starts from 0

System.out.println("colSum "+colSum(nonSquare,1)); //the indexing starts from 0

System.out.println("isSquare? " + isSquare(nonSquare));

System.out.println("isLatin? " + isLatin(nonSquare));

System.out.println("Testing latin array");

System.out.println("rowSum "+rowSum(latin,1)); //the indexing starts from 0

System.out.println("colSum "+colSum(latin,1)); //the indexing starts from 0

System.out.println("isSquare? " + isSquare(latin));

System.out.println("isLatin? " + isLatin(latin));

System.out.println("Testing nonlatin array");

System.out.println("rowSum "+rowSum(nonLatin,1)); //the indexing starts from 0

System.out.println("colSum "+colSum(nonLatin,1)); //the indexing starts from 0

System.out.println("isSquare? " + isSquare(nonLatin));

System.out.println("isLatin? " + isLatin(nonLatin));

}

}

PART 2

import java.util.*;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number M (number of rows and columns for array) ");

int M=sc.nextInt(); //take input

int[][] ar= new int[M][M];

System.out.print("+"); //print starting border

for(int i=0;i<M;i++)

System.out.print("-+");

System.out.println();

for(int i=0;i<M;i++){ //start from 0

System.out.print("|"); //print starting pipe Character

for(int j=1;j<=M;j++){ //start from 1

if((i+j)%M==0) //if sum of i&j equals M print M, i.e. last number

System.out.print( M+"|");

else

System.out.print( (i+j)%M +"|");//else print rotated number

}

System.out.println();

}

System.out.print("+"); //print ending border

for(int i=0;i<M;i++)

System.out.print("-+");

System.out.println();

}

}

Ver imagen zuwairahdahir