Question

/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

/**
* A collection of methods related to multi-dimensional arrays.
*/
public class Array2Lab {

/**
* Return whether k is in list.
* Precondition: the elements of list are not null.
* @param list the array to be searched.
* @param k the number to search for.
* @return true if k is an element of list, and false otherwise.
*/
public static boolean contains(Object[][] list, Object k) {
return false;
}
  
/**
* Create a String that has the contents of list. Each row of list should be on a separate line,
* separated by spaces, and surrounded by { and }.
* The first line of of the output should start with an additional {, each other line of the output
* should start with a space, and the last line of the output should have an additional {.
* For example: {{1, 2}, {3, 4}} should be formatted in the String as:
* {{1 2}
* {3 4}}
* @param list the array to print.
* @return the elements of the array, separated by spaces, and surrounded by { and }, with each row on a separate line.
*/
public static String arrayToString(int[][] list) {
return null;
}

/**
* Create a new array that "flips" a rectangular region of matrix about the diagonal. So, each row of the output should be a column of the input.
* Precondition: The region of matrix[rowStart...rowEnd][colStart...colEnd] exists.
* @param matrix the input two dimensional array.
* @param rowStart the index of the first row of the rectangular region to flip.
* @param rowEnd the index of the last row of the rectangular region to flip.
* @param colStart the index of the first column of the rectangular region to flip.
* @param colEnd the index of the last column of the rectangular region to flip.
*/
public static int[][] transpose(int[][] matrix, int rowStart, int rowEnd, int colStart, int colEnd) {
return null;
}

/**
* Creates a new array that is a copy of the input matrix, except that one row and one column have been removed.
* Precondition: the row index is between 0 (inclusive) and the number of rows of matrix (not inclusive)
* @param matrix the input two dimensional array
* @param row the index of the row to remove
* @param col the index of the column to remove
*/
public static int[][] removeRowAndCol(int[][] matrix, int row, int col) {
return null;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER...

OUTPUT SCREEN SHOT:-

COPY CODE:-

//Below is your code: -

/**
* A collection of methods related to multi-dimensional arrays.
*/
public class Array2Lab {

/**
* Return whether k is in list. Precondition: the elements of list are not null.
*
* @param list the array to be searched.
* @param k the number to search for.
* @return true if k is an element of list, and false otherwise.
*/
public static boolean contains(Object[][] list, Object k) {
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list[i].length; j++) {
if (list[i][j].equals(k)) {
return true;
}
}
}
return false;
}

/**
* Create a String that has the contents of list. Each row of list should be on
* a separate line, separated by spaces, and surrounded by { and }. The first
* line of of the output should start with an additional {, each other line of
* the output should start with a space, and the last line of the output should
* have an additional {. For example: {{1, 2}, {3, 4}} should be formatted in
* the String as: {{1 2} {3 4}}
*
* @param list the array to print.
* @return the elements of the array, separated by spaces, and surrounded by {
* and }, with each row on a separate line.
*/
public static String arrayToString(int[][] list) {
String output = "{\n";
for (int i = 0; i < list.length; i++) {
output += " {";// added space as per question
for (int j = 0; j < list[i].length; j++) {
output = output + list[i][j] + " ";
}
output = output.substring(0, output.length() - 1);
output += "}\n";
}
output += "}";
return output;
}

/**
* Create a new array that "flips" a rectangular region of matrix about the
* diagonal. So, each row of the output should be a column of the input.
* Precondition: The region of matrix[rowStart...rowEnd][colStart...colEnd]
* exists.
*
* @param matrix the input two dimensional array.
* @param rowStart the index of the first row of the rectangular region to flip.
* @param rowEnd the index of the last row of the rectangular region to flip.
* @param colStart the index of the first column of the rectangular region to
* flip.
* @param colEnd the index of the last column of the rectangular region to
* flip.
*/
public static int[][] transpose(int[][] matrix, int rowStart, int rowEnd, int colStart, int colEnd) {
int transpose[][] = new int[matrix.length][];
for (int i = 0; i < matrix.length; i++) {
transpose[i] = new int[matrix[i].length];
for (int j = 0; j < matrix[i].length; j++) {
transpose[i][j] = matrix[i][j];
}
}
for (int c = rowStart; c < rowEnd - 1; c++) {
for (int d = colStart; d < colEnd - 1; d++) {
transpose[d][c] = matrix[c][d];
}
}

return transpose;
}

/**
* Creates a new array that is a copy of the input matrix, except that one row
* and one column have been removed. Precondition: the row index is between 0
* (inclusive) and the number of rows of matrix (not inclusive)
*
* @param matrix the input two dimensional array
* @param row the index of the row to remove
* @param col the index of the column to remove
*/
public static int[][] removeRowAndCol(int[][] matrix, int row, int col) {
int[][] newArr = new int[matrix.length - 1][];
int newArrRow = 0;
for (int i = 0; i < matrix.length; ++i) {
if (i == row)
continue;
newArr[newArrRow] = new int[matrix[i].length - 1];
int newArrCol = 0;
for (int j = 0; j < matrix[i].length; ++j) {
if (j == col)
continue;

newArr[newArrRow][newArrCol] = matrix[i][j];
++newArrCol;
}
++newArrRow;
}
return newArr;
}

public static void main(String[] args) {
Integer[][] arr = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 } };
int[][] arr1 = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 } };
System.out.println("Is 17 present in array: " + contains(arr, 17));
System.out.println("Is 21 present in array: " + contains(arr, 21));
System.out.println("Arrays to String representation: ");
System.out.println(arrayToString(arr1));
int[][] transposedArray = transpose(arr1, 1, 5, 1, 5);
System.out.println("Arrays to String representation after transpose: ");
System.out.println(arrayToString(transposedArray));

int[][] removedRowAndColArr = removeRowAndCol(arr1, 2, 3);
System.out.println("Arrays to String representation after removing row 2 and col 4: ");
System.out.println(arrayToString(removedRowAndColArr));
}
}

Output

Is 17 present in array: true
Is 21 present in array: false
Arrays to String representation:
{
{1 2 3 4 5}
{6 7 8 9 10}
{11 12 13 14 15}
{16 17 18 19 20}
}
Arrays to String representation after transpose:
{
{1 2 3 4 5}
{6 7 12 17 10}
{11 8 13 18 15}
{16 9 14 19 20}
}
Arrays to String representation after removing row 2 and col 4:
{
{1 2 3 5}
{6 7 8 10}
{16 17 18 20}
}

Add a comment
Know the answer?
Add Answer to:
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

    Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /**    Class to read and process the contents of a standard CSV file */ public class CSVReader {...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • /** * Class that defines a method which performs matrix addition. This gives students their first...

    /** * Class that defines a method which performs matrix addition. This gives students their first practice at creating and using * the code needed for higher dimensional arrays. * * @author Matthew Hertz */ public class MatrixAddition { /** * Given two equally-sized matrices, return a newly allocated matrix containing the result of adding the two.<br/> * Precondition: a &amp; b must have the same number of rows and each row will have the same number of columns. You...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • 1) Define a 2 dimensional arrays of doubles (3 rows by 3 columns) and 2) Read...

    1) Define a 2 dimensional arrays of doubles (3 rows by 3 columns) and 2) Read values from the user. 3) Print the numbers entered by the user in row major order 4) Print the numbers entered by the user in column major order import java.util.*; public class XXX_Chapter83 { public static void main(String[] args) { //creates array and stores values with input from user printArrayRowMajor (board); printArrayColumnMajor (board); } public static void printArrayRowMajor (int [] [] board) { //prints...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT