A Java Problem. Util2D creates an manages a 2d array of ints. It has a construct
ID: 3801155 • Letter: A
Question
A Java Problem. Util2D creates an manages a 2d array of ints. It has a constructor that takes a 2d array as a parameter and assigns it to the instance variable. You write the whole class
Provide the constructor
Provide these methods
1. getSmallest() Gets the smallest integer in the array
2. numberOfEvenInColumn(int column) Gets the number of even integers in the specified column. Remember both positive and negative numbers can be even.
3. last() Gets the element in the last column of the last row
4. contains(int target) Returns true if the target is in the array, otherwise false
------------------------------------------------------------------------------------------------------------
Util2DTester.java
Explanation / Answer
Util2D.java
public class Util2D {
//Declaring an 2-Dimensional array
int nos[][];
//Parameterized constructor
public Util2D(int[][] nos) {
super();
this.nos = nos;
}
//This method will find the smallest element in the array
public int getSmallest()
{
int small=nos[0][0];
for(int i=0;i<nos.length;i++)
{
for(int j=0;j<nos[0].length;j++)
{
if(small>nos[i][j])
small=nos[i][j];
}
}
return small;
}
/* This method will count the number of even elements in the user given column
* @params : column of type int
* @return : integer
*/
public int numberOfEvenInColumn(int column)
{ int even_count=0;
for(int i=0;i<nos.length;i++)
{
if(nos[i][column]%2==0)
even_count++;
}
return even_count;
}
/* This method will return the last element in the last row last column
* @params : no params
* @return : integer
*/
public int last()
{
return nos[nos.length-1][nos[0].length-1];
}
/* This method will check whether an element is found in the array or not
* @params : column of type int
* @return : boolean
*/
public boolean contains(int target)
{
int count=0;
for(int i=0;i<nos.length;i++)
{
for(int j=0;j<nos[0].length;j++)
{
if(target==nos[i][j])
{
return true;
}
}
}
return false;
}
}
___________________
Util2DTester.java
public class Util2DTester
{
public static void main(String[] args)
{
int[][] numbers = {
{-5, 8, 6, 3, 11},
{3, -6, -2, 5, -9},
{1, 2, 5, 7, 6}
};
Util2D util = new Util2D(numbers);
System.out.println("Smallest: " + util.getSmallest());
System.out.println("Expected: -9");
System.out.println("Even in column#1: " + util.numberOfEvenInColumn(1));
System.out.println("Expected: 3");
System.out.println("Even in column#0: " + util.numberOfEvenInColumn(0));
System.out.println("Expected: 0");
System.out.println("Last: " + util.last());
System.out.println("Expected: 6");
System.out.println("Contains: " + util.contains(8));
System.out.println("Expected: true");
System.out.println("Contains: " + util.contains(15));
System.out.println("Expected: false");
int[][] numbers2 = {
{5, 3, 2, 4},
{6, 1, 7, 8}
};
util = new Util2D(numbers2);
System.out.println("Smallest: " + util.getSmallest());
System.out.println("Expected: 1");
System.out.println("Last: " + util.last());
System.out.println("Expected: 8");
}
}
___________________
Output:
Smallest: -9
Expected: -9
Even in column#1: 3
Expected: 3
Even in column#0: 0
Expected: 0
Last: 6
Expected: 6
Contains: true
Expected: true
Contains: false
Expected: false
Smallest: 1
Expected: 1
Last: 8
Expected: 8
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.