Java Program: (name this Lab13_Problem4) In main()construct a two-dimensional ar
ID: 3769837 • Letter: J
Question
Java Program: (name this Lab13_Problem4)
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}};
Also in main(), declare a Boolean variable bDups which will be used to store the return value of the method below.
Write a method fbDupRows() which checks each row against the other rows in the matrix and indicates whether there are duplicates.
A duplicate row is identified when you compare a row against the row 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.
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.
In main(), examine the return value and display either:
Duplicate Rows Found
or
No Duplicate Rows Found
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class DuplicateRows
{
public static boolean fbDupRows(int[][] arr)
{
int count = 0;
int row = arr.length;
int col = arr[0].length;
boolean result = false;
for(int i=1;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(arr[i-1][j]==arr[i][j])
count++;
}
if(count==col)
result = true;
else
count = 0;
}
return result;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[][] iVals = {{1,2,3,4}, {5,6,7,8}, {9,10,10,12}, {9,10,11,12}, {1,27,3,4}};
boolean bDups = fbDupRows(iVals);
if(bDups==true)
System.out.println("Duplicate Rows Found");
else
System.out.println("Duplicate Rows not Found");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.