Create a project called Inlab10 and make a class called Data. The only instance
ID: 3811203 • Letter: C
Question
Create a project called Inlab10 and make a class called Data. The only instance variable in Data is a 2D int array. Make the constructor take the number of row and columns and create the array. Make a method that prints the array. Make a method that fills the array with random nits between and including 0 and 9. Make a method that find the sum of each row in the array and returns those values in an array (i.e. an array of row sums). Do not print the sums, return an array of them. Make a method that checks if any column has the consecutive values in 1' 2' 3' anywhere in that column. If some column does, return true. Return false if not. Make a Driver that does some simple tests of your class.Explanation / Answer
Data.java
import java.util.Random;
public class Data {
// Creating an array
int arr[][];
// Parameterized constructor
public Data(int row, int column) {
super();
this.arr = new int[row][column];
}
// Method which will display the array
public void printArray() {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
/*
* Method which will populate the array elements with random values
*/
public void fillArray() {
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
arr[i][j] = r.nextInt((9 - 0) + 1) + 0;
}
}
}
// Displaying the sum of each row in the array
public int[] sumOfEachRow() {
int sum;
int sumArr[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
sum = 0;
for (int j = 0; j < arr[0].length; j++) {
sum += arr[i][j];
}
sumArr[i] = sum;
}
return sumArr;
}
// checking whether any column is having consecutive 1 and 2 and 3
public boolean findConsecutive123() {
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (j < arr[0].length - 3) {
if (arr[i][j] == 1) {
count++;
if (arr[i][j] == 2) {
count++;
if (arr[i][j] == 3) {
count++;
}
}
}
if (count == 3)
return true;
}
}
count = 0;
}
return false;
}
}
__________________
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
//Declaring variables
int row,column;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the row and column size
System.out.print("Enter the row size :");
row=sc.nextInt();
System.out.print("Enter the column size :");
column=sc.nextInt();
//Creating an array
int arr[]=new int[row];
//Creating an Data class object
Data d=new Data(row,column);
//calling the methods
System.out.println("Displaying the 2-D Array:");
d.fillArray();
d.printArray();
arr=d.sumOfEachRow();
for(int i=0;i<row;i++)
{
System.out.println("Displaying the sum of Row "+(i+1)+":"+arr[i]);
}
boolean bool=d.findConsecutive123();
if(bool)
System.out.println("There is consecutive 1 , 2 , 3 elements in the array");
else
System.out.println("There is no consecutive 1 , 2 , 3 elements in the array");
}
}
_______________________
Output:
Enter the row size :5
Enter the column size :5
Displaying the 2-D Array:
5 8 3 3 6
9 4 1 9 8
0 5 7 1 6
1 1 0 2 1
2 8 1 1 1
Displaying the sum of Row 1:25
Displaying the sum of Row 2:31
Displaying the sum of Row 3:19
Displaying the sum of Row 4:5
Displaying the sum of Row 5:13
There is no consecutive 1 , 2 , 3 elements in the array
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.