Write a java program to create a 2-dimensional array - have the row size and the
ID: 3766863 • Letter: W
Question
Write a java program to create a 2-dimensional array - have the row size and the column size declared as class constants. Your program should use subarray processing and modularity. Have methods to:
Fill a 2-dimensional array with random numbers (range MIN-MAX, declared as class constants MIN = -10 and MAX = 50).
Return true if all the elements of a 2-dimensional array are positive and false otherwise (boolean method, named allPositive). This method should be invoked first for the entire table and next, repeatedly, in a loop, for subarrays of the original table. Consider first how many parameters have to be passed.
Print a 2-dimensional array; invoke it first to display the original table and invoke it again after each method call to allPositive to display the subarray checked. Again, consider first how many parameters have to be passed.
Generate output similar to the sample output below.
Explanation / Answer
import java.util.*;
class arr_2d_positive
{
static final int r=3;
static final int c=3;
static int a[][]=new int[r][c];
public static void fill()
{
int i,j,max=50,min=-10;
Random ran=new Random();
int n = max - min + 1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
a[i]
[j]=ran.nextInt()%n;
}
}
}
public static boolean allPositive()
{
int i,j;
boolean rr=true;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]%2!=0)
rr=false;
}
}
return rr;
}
public static void main(String args[])
{
fill();
boolean ans=allPositive();
if(ans==true)
{
System.out.println("All
numbers are Positive");
}
else
{
System.out.println("All
numbers are not Positive");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.