Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question involves reasoning about arrays of integers. You will write methods in a class named ArrayTester. Write a static method getColumn, which returns a

This question involves reasoning about arrays of integers. You will write methods in a class named ArrayTester.

  1. Write a static method getColumn, which returns a one-dimensional array containing the elements of a single column in a two-dimensional array. The elements in the returned array should be in the same order as they appear in the given column. The notation arr2D [r] [c] represents the array element at row r and column c.

The following code segment initializes an array and calls the getColumn method.

int [] [] arr2D = { {

0,

1,

2

},

{

3,

4,

5

},

{

6,

7,

8

},

{

9,

5,

3

} };

int [] result = ArrayTester.getColumn (arr2D, 1);

When the code segment has completed execution, the variable result will have the following contents.

result: {1, 4, 7, 5}

  1. Write a static method getRow, which returns a one-dimensional array containing the elements of a single row in a two-dimensional array. The elements in the returned array should be in the same order as they appear in the given row. The notation arr2D [r] [c] represents the array element at row r and column c.

int [] result = ArrayTester.getRow (arr2D, 1);

When the code segment has completed execution, the variable result will have the following contents.

result: {3, 4, 5}

  1. Complete these two methods in the program:
  • hasAllValues
  • containsDuplicates
  1. Write the static method isLatin, which returns true if a given two-dimensional square array is a Latin square, and otherwise, returns false.

A two-dimensional square array of integers is a Latin square if the following conditions are true.

  • The first row has no duplicate values. Must use method containsDuplicates in part 3.
  • All values in the first row of the square appear in each row of the square. Must use hasAllValues in part 3.
  • All values in the first row of the square appear in each column of the square. Must use hasAllValues in part 3.

This is the given code:

import java.util.Arrays;

public class ArrayTester { /** Returns an array containing the elements of column c of arr2D in the same order as they appear in arr2D. * Precondition: c is a valid column index in arr2D. * Postcondition: arr2D is unchanged. */ public static int [] getColumn(int [][] arr2D, int c) { /* your code */ }

/** Returns an array containing the elements of row c of arr2D in the same order as they appear in arr2D. * Precondition: r is a valid column index in arr2D. * Postcondition: arr2D is unchanged. */ public static int [] getRow(int [][] arr2D, int c) { /* your code */ }

/** Returns true if and only if every value in arr1 appears in arr2. * Precondition: arr1 and arr2 have the same length. * Postcondition: arr1 and arr2 are unchanged. */ public static boolean hasAllValues(int [] arr1, int [] arr2) { /* your code */ }

/** Returns true if arr contains any duplicate values; * false otherwise. */ public static boolean containsDuplicates(int[] arr) { /* your code */ }

/** Returns true if square is a Latin square ; false otherwise. * Precondition: square has an equal number of rows and columns. * square has at least one row. */ public static boolean isLatin(int[][] square) { /* your code */ }

/********************** Test *************************/

public static void main(String[] args) { int [] [] arr2D = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 5, 3} }; int [] result = ArrayTester.getColumn (arr2D, 1); System.out.println(Arrays.toString (result));

int [] result = ArrayTester.getColumn (arr2D, 1); System.out.println(Arrays.toString (result));

int [] [] square1 = { {1, 2, 3}, {2, 3, 1}, {3, 1, 2} }; int [] [] square2 = { {10, 30, 20, 0}, {0, 20, 30, 10}, {30, 0, 10, 20}, {20, 10, 0, 30} }; int [] [] square3 = { {1, 2, 1}, {2, 1, 1}, {1, 1, 2} }; int [] [] square4 = { {1, 2, 3}, {3, 1, 2}, {7, 8, 9} }; int [] [] square5 = { {1, 2}, {1, 2}};

System.out.println(isLatin (arr2D) + " " + isLatin (square1) + " " + isLatin (square2) + " " + isLatin (square3) + " " + isLatin (square4) + " " + isLatin (square5)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

Students also viewed these Databases questions

Question

=+2 Is the decision sustainable in the long run?

Answered: 1 week ago