Question

In: Computer Science

IN JAVA! Lab 13 Problem 4 (name this Lab13_Problem4) In main()construct a two-dimensional array named iVals...

IN JAVA!

Lab 13 Problem 4 (name this Lab13_Problem4)

  1. In main()construct a two-dimensional array named iVals using this statement (a 5-row, 4-column array):

    int iVals[][] = {{1,2,3,4}, {5,6,7,8}, {9,10,10,12}, {9,10,11,12}, {1,27,3,4}};
  2. Also in main(), declare a Boolean variable bDups which will be used to store the return value of the method below.
  3. Write a method fvDisplay() to display the array, allocating 6 horizontal spaces for each column (or just reuse the method from the previous problem).
  4. Write a method fbDupRows() which checks each row against the other rows in the matrix and indicates whether there are duplicates.
  5. A duplicate row is identified when you compare a row against all the rows below it, column by column. If all the values in each column are the same between each row, then you have a duplicate row. Hint: you need a duplicate counter, which you need to reset to 0 for each row.
  6. When your method completes its examination of the array, return a value of true if any rows are duplicates, false if there are no duplicates.
  7. In main(), examine the return value and display either:

    Duplicate Rows Found

    or

    No Duplicate Rows Found
  8. Test your algorithm by changing the values in the array from the original in step 1 above (which has no duplicate rows).
    1. Make two adjacent rows identical, run the app, and verify it returns duplicates.
    2. Then try two duplicate rows, separated by a non-duplicated row; make sure that also works.

Solutions

Expert Solution

SHORT SUMMARY:

  • Implemented the main() function as per the requirements provided.
  • Provided the source code with three different test cases as mentioned in the question.
  • Given the sample output and screenshot of the source code.

SOURCE CODE:

public class Duplicate2DArray {

    public static void main(String[] args) {

        // TEST CASE 1 - No duplicates

        System.out.println("\nTEST CASE 1");

        int iVals[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 10, 12}, {9, 10, 11, 12}, {1, 27, 3, 4}};

        fvDisplay(iVals);

        boolean bDups = fbDupRows(iVals);

        if (bDups) {

            System.out.println("\nDuplicate Rows Found");

        } else {

            System.out.println("\nNo Duplicate Rows Found");

        }

       

        // TEST CASE 2 - Adjacent Rows identical - 1st and 2nd Row duplicate value

        System.out.println("\nTEST CASE 2");

        int iVals1[][] = {{1, 2, 3, 4}, {1, 2, 3, 4}, {9, 10, 10, 12}, {9, 10, 11, 12}, {1, 27, 3, 4}};

        boolean bDups1 = fbDupRows(iVals1);

        fvDisplay(iVals1);

        if (bDups1) {

            System.out.println("\nDuplicate Rows Found");

        } else {

            System.out.println("\nNo Duplicate Rows Found");

        }

       

        // TEST CASE 3 - 2nd and 4th rows same value

        System.out.println("\nTEST CASE 3");

        int iVals2[][] = {{1, 2, 3, 4}, {5, 6,7, 8}, {9, 10, 10, 12}, {5, 6, 7, 8}, {1, 27, 3, 4}};

        boolean bDups2 = fbDupRows(iVals2);

        fvDisplay(iVals2);

        if (bDups2) {

            System.out.println("\nDuplicate Rows Found");

        } else {

            System.out.println("\nNo Duplicate Rows Found");

        }

    }

    // Display array with 6 horizontal space

    private static void fvDisplay(int[][] iVals) {

        // iterating through rows

        for (int row = 0; row < iVals.length; row++) {

            // iterating through columns

            for (int column = 0; column < iVals[row].length; column++) {

                // Display array values with horizontal space of 6

                System.out.printf("%6d", iVals[row][column]);

            }

            System.out.println("");

        }

    }

    // checks whether there are duplicate rows

    private static boolean fbDupRows(int[][] iVals) {

        // iterating through rows

        for (int row1 = 0; row1 < iVals.length; row1++) {

            // iterating from next rows

            for (int row2 = row1 + 1; row2 < iVals.length; row2++) {

                // counter variable

                int dupCount = 0;

                // iterating through the column

                for (int column = 0; column < iVals[row1].length; column++) {

                    // check the values are equal

                    if (iVals[row1][column] == iVals[row2][column]) {

                        // increment the counter variable

                        dupCount++;

                    }

                }

                // if the counter variable value equals to column duplicate found

                if (dupCount == iVals[row1].length) {

                    return true;

                }

            }

        }

        // when no duplicates found

        return false;

    }

}

CODE SCREENSHOT:

SAMPLE OUTPUT:

******************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

******************************************************************************


Related Solutions

In Java please Your program will sort a two dimensional array (5 * 4) based on...
In Java please Your program will sort a two dimensional array (5 * 4) based on the following: The entire array should be sorted using bubble sort based on the 1st column in ascending order and display the entire array. Reset the array to its original contents. The entire array should again be sorted using selection sort based on the 2nd column in descending order and display the entire array. Reset the array to its original contents. The entire array...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT