pls hel meee!! TwoDimArr<String> arr= new TwoDimArr<String> (3,5,\"cell\"); arr.
ID: 3914064 • Letter: P
Question
pls hel meee!!
TwoDimArr<String> arr= new TwoDimArr<String> (3,5,"cell");
arr.set(2,1,"tissue");
System.out.println(arr.get(2,1));
System.out.println(arr.get(2,3));
System.out.println(arr.countOccurences("cell"));
TwoDimArr <Double> arr2= new TwoDimArr <Double>(2,3,4.5);
//---------------------------------------------------------------------------------------
output
tissue
cell
14
*The shown constructor takes the dimension information and the initial value of all elements of the array as a parameter.
set:put data to given index
get:return to data from given index
contOccurance:how many times pass the given paremeter in matrix
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
TwoDimArr.java
==============
public class TwoDimArr<T> {
private T[][] data;
private int rows;
private int cols;
public TwoDimArr(int rows, int cols, T initial){
this.rows = rows;
this.cols = cols;
data = (T[][])new Object[rows][cols];
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
data[i][j] = initial;
}
public void set(int r, int c, T value){
if(r >= 0 && r < rows && c >= 0 && c < cols)
data[r][c] = value;
}
public T get(int r, int c){
if(r >= 0 && r < rows && c >= 0 && c < cols)
return data[r][c];
else
return null;
}
public int countOccurences(T value){
int count = 0;
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
{
if(data[i][j].equals(value))
count++;
}
return count;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.