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

a) Write a Java program that counts and outputs the number of times a zero occur

ID: 3721648 • Letter: A

Question

a) Write a Java program that counts and outputs the number of times a zero occurs in an array of maximum 50 integers. Write some array examples to test your program.

b)Write a method sumArrays that receives two one-dimensional arrays list1 and list2 of integers as parameters and returns the sum of the all of the values in list1 plus the sum of all of the values in list2. Now write a main program to call sumArrays. Pass arrays arr1 and arr2 as arguments, and print the sum that is returned by sumArrays.

c) Write a Java program to find and display the largest and smallest values in a two- dimensional (m x n) integer array.

Please try to answer all the questions.

Explanation / Answer

ArrayElementTest.java

public class ArrayElementTest {

public static void main(String[] args) {

int a[]={4,5,6,9,0,1,2,0,3,0};

System.out.println("Number of zeros: "+getZeroCount(a));

int c[]={1,2,3,4,5};

System.out.println("Sum Of teo arrays: "+sumArrays(a, c));

int b[][]={{1,2,3},{4,5,8},{-2,5,6}};

displayMaxMinValues(b);

}

public static int getZeroCount(int a[]){

int c=0;

for(int i=0;i<a.length;i++){

if(a[i]==0){

c++;

}

}

return c;

}

public static int sumArrays(int a[], int b[]) {

int sum = 0;

for(int i=0;i<a.length;i++){

sum+=a[i];

}

for(int i=0;i<b.length;i++){

sum+=b[i];

}

return sum;

}

public static void displayMaxMinValues(int a[][]){

int max=a[0][0],min=a[0][0];

for(int i=0;i<a.length;i++){

for(int j=0;j<a[i].length;j++){

if(min>a[i][j])

min = a[i][j];

if(max<a[i][j])

max = a[i][j];

}

}

System.out.println("Min: "+min);

System.out.println("Max: "+max);

}

}

Output:

Number of zeros: 3
Sum Of teo arrays: 45
Min: -2
Max: 8

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