Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 2. (question2.java) (10 marks) Write a class that has the following met

ID: 3898357 • Letter: Q

Question





Question 2. (question2.java) (10 marks) Write a class that has the following methods. Use nested for loops for all of these array operations: publie statie void printarraytint111 a): print the values stored in the array public static int sunint11 al : compute and return the sum of the values in the array publ ic statie int [ ] [] sumt int [ ] [ ] «, int( ] ti ?: compute the sum of two arrays and return a new amay with those values. Remember, each value in array a gets added to the corresponding value in array b. Assume that the arrays are the same size public static boolean isFound (int11 int n: return true if the value n is found in the array. Returm false ifit is not found. In your main method prompt the user to enter two integers that will be the rows and columns for two 2D arrays. Create the arrays according to the input dimensions and fill them with random values from 0 to 15. Then prompt for a third integer that will be used to test your isFound) method. Remember, to do all input and output in your main) method. Pass the input to your methods and collect the results using assignment statements. You don't need to do any input checking. Assume the user enters appropriate values. I suggest your write and test your methods one at a time. You can get part marks for the methods that work. ava question2 Enter two integera for oMs and columns of the azzays:3 First array 13 10 Second array Sum first azrays 93 Sum second array: Sum of fdest array+second azeay 12 12 15 10 18 18 Enter an integeE to Search for:3 found in first array?: trae found in second array?: falae java question2 Enter two integers for rows and columns of the arrays:2 First array 10 10 15 Second array: 10 13 Sum first array: 72 Sum second array: 57 Sum of Esrat arraysecond array: 10 16 20 21 Enter an integer to search for: 0 found in fiest azray?: true ound in second areay?: true

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


import java.util.Random;
import java.util.Scanner;

public class question2 {
public static void printArray(int [][] a)
{
for(int i= 0; i < a.length; i++)
{
for(int j = 0; j < a[0].length; j++)
{
System.out.printf("%4d", a[i][j]);
}
System.out.println();
}

System.out.println();
}

public static int sum(int[][] a)
{
int s = 0;
for(int i= 0; i < a.length; i++)
{
for(int j = 0; j < a[0].length; j++)
{
s += a[i][j];
}
}

return s;
}

public static int[][] sum(int[][] a, int[][] b)
{
int r = a.length;
int c = a[0].length;
int[][] s = new int[r][c];

for(int i= 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{

s[i][j] = a[i][j] + b[i][j];
}
}

return s;
}

public static boolean isFound(int[][] a, int n)
{
for(int i= 0; i < a.length; i++)
{
for(int j = 0; j < a[0].length; j++)
{
if(a[i][j] == n) //found
return true;
}
}
return false;//not found
}

public static int[][] generateMat(int rows, int cols)
{
Random rand = new Random();
int[][] a = new int[rows][cols];
for(int i= 0; i < a.length; i++)
{
for(int j = 0; j < a[0].length; j++)
{
a[i][j] = 1 + rand.nextInt(15);
}
}
return a;
}

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int rows, cols, n;
int[][] a, b, c;

System.out.print("Enter two integers for rows and cols of the arrays: ");
rows = keyboard.nextInt();
cols = keyboard.nextInt();

a = generateMat(rows, cols);
b = generateMat(rows, cols);

System.out.println("First array");
printArray(a);

System.out.println("Second array");
printArray(b);

System.out.println("Sum first array: " + sum(a));
System.out.println("Sum second array: " + sum(b));
System.out.println("Sum of first and second array:");
c = sum(a, b);
printArray(c);

System.out.print("Enter an integer to search for: ");
n = keyboard.nextInt();
System.out.println(n + " is found in first array?: " + isFound(a, n));
System.out.println(n + " is found in second array?: " + isFound(b, n));


}
}


output
====
Enter two integers for rows and cols of the arrays: 3 4
First array
3 4 12 2
1 1 14 11
4 6 14 4

Second array
3 15 1 10
2 12 3 11
15 13 7 15

Sum first array: 76
Sum second array: 107
Sum of first and second array:
6 19 13 12
3 13 17 22
19 19 21 19

Enter an integer to search for: 4
4 is found in first array?: true
4 is found in second array?: false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote