Question: Create a class called Lab9 and in it implement all of the methods below. Also, write a main method with test calls to all of

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

Part 1

  1. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end;

  2. int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array a;

  3. int colSum(int[][] a, int j) returns the sum of the elements in column j of the 2-D array a;

  4. 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);

  5. boolean isLatin(int[][] a) returns true if the 2-D array a is a Latin square (i.e. an n-by-n matrix such that each row and each column contains the values from 1 through n with no repeats);

  6. main(String[] args): the main method should test each method above on five 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} }; 

import java.util.Random; import java.util.Scanner; /** * Write a description of class Lab9 here. * * @author (your name) * @version (a version number or a date) */ public class Lab9 {

public static void main(String [] args) {

// Test random matrix of integers int[][] a = randomMatrix (7, 20, 100); // use the above array to test next rowSum() int rowTotal = rowSum( a, 3); System.out.println(" Sum of row = " + rowTotal);

// use the above array to test next colSum() int columSum = colSum( a, 4); System.out.println(" Sum of columns = " + columSum);

isSquare(a);

int[][] nonsquare = { {1,2,3}, {4,5}, {6,7,8,9} }; int[][] notlatin = { {2,1,3}, {2,3,1}, {3,1,2} }; int[][] latin = { {1,2,3}, {2,3,1}, {3,1,2} }; int[][] allneg = { {-10,-12,-3}, {-4,-5,-6,-8}, {-7,-8} }; int[][] notSquare = { {10, 12, 3, 17}, { 4, 5, 16, 18}, { 7, 9, 10, 45} }; int[][] perfectSquare = { {1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12}, { 13, 14, 15, 16}};

// Your tests, here are 2 to start with isLatin(nonsquare); isSquare(perfectSquare);

}

// (a) int[][] random(int N, int start, int end) returns an N-by-N matrix of // random integers ranging from start to end;

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

// Your logic goes here }

// Generates a new random integer between 0 and end when called to be used in part(a) public static int randomGenerator( int end, int start){

// Your code goes here

}

// (b) int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array a public static int rowSum( int[][] a, int i) {

// Your logic goes here }

// (c) int colSum(int[][] a, int j) returns the sum of the elements in column j of the 2-D array a public static int colSum( int[][] a, int j) {

// Your logic goes here }

// (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) public static boolean isSquare(int[][] a) {

// Your logic goes here

}

// (e) boolean isLatin(int[][] a) returns true if the 2-D array a is a Latin square // (i.e. an n-by-n matrix such that each row and each column contains the values // from 1 through n with no repeats) public static boolean isLatin(int[][] a) {

// Your logic goes here }

// 2. In a different class named ShiftNumbers.java write a program that takes integer M as the number of both rows and columns for your 2D array. // Create the same exact 2D array as displayed in Lab handout.

}

Part 2:

Write a program ShiftNumbers.java that takes integerMas the number of both rows and columns for your 2D array.Create the same exact following 2D array.

Note: The borders are produced at the time of printing. You also need to shift the numbers for each row of the 2D array as displayed below:

 +-+-+-+-+-+ |1|2|3|4|5| +-+-+-+-+-+ |2|3|4|5|1| +-+-+-+-+-+ |3|4|5|1|2| +-+-+-+-+-+ |4|5|1|2|3| +-+-+-+-+-+ |5|1|2|3|4| +-+-+-+-+-

import java.util.Scanner; import java.util.*;

/** * Write a description of class Print2DArray here. * * @author (your name) * @version (a version number or a date) */ public class ShiftNumbers { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter an integer, then press Enter"); int N = keyboard.nextInt(); // 1. Create your array dynamically here

// 2. Now, fill the 2D array with numbers

// 3. Print your 2D array

// 4. print the lower border for(int j = 0; j < digits[0].length; ++j) { System.out.print("+-"); } System.out.println("+");

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!