JAVA Assignment: Need Help! Create a project called Inlab10 and make a class cal
ID: 3809789 • Letter: J
Question
JAVA Assignment: Need Help!
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 ints 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 in any column has the consecutive values '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. By Thursday night at 11:59 pm, submit the files Data.java and Driver.java into the appropriate D2L dropbox folder.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 :3
Enter the column size :3
Displaying the 2-D Array:
6 0 4
5 7 4
3 2 0
Displaying the sum of Row 1:10
Displaying the sum of Row 2:16
Displaying the sum of Row 3:5
There is no consecutive 1 , 2 , 3 elements in the array
____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.