Declare a two arrays called array A and array B that holds integer and the size
ID: 3768097 • Letter: D
Question
Declare a two arrays called array A and array B that holds integer and the size of each array is given by the user. The main method calls a method called fill Array(...) of type void to fill an array with a set of generated random integers between low and high (both inclusive). This method should be called twice to fill array A and then array B. (the values of High and low are read in the main method. The main method calls another method called print Array (...) of type void to display the elements of the array 5 elements per lines. This method should be called twice to print array A and then array B. The main method calls another method called count (...) of type integer to count and return the total number of integers greater than a value enter by the user. Then the main methods calls another method called is Same(...) of type boolean to check if the two arrays arrayA and arrayB have the same element or not. The main method calls a method called find Average(...) of type double that calculates and returns average of all the values in arrayA. Then, main method calls a method called aboveAverage(...) of type integer that finds and returns how many numbers in arrayB are above the average of the elements of arrayB.Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class ArrayService {
/**
* @param args
*/
static int arrayA[], arrayB[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner;
try {
scanner = new Scanner(System.in);
System.out.print("Enter the low:");
int low = scanner.nextInt();
System.out.print("Enter the high:");
int high = scanner.nextInt();
arrayA = fillArray(low, high);
arrayB = fillArray(low, high);
System.out.print(" arrayA:");
printArray(arrayA);
System.out.print(" arrayB:");
printArray(arrayB);
System.out
.println(" Total no of integers greater than value of arrayA:"
+ count(arrayA));
System.out.println("arrayA and arrayB are same?" + isSame());
double averageA = findAverage();
System.out.println("Average of arrayA :" + averageA);
System.out.println("No of element above the Average in arrayB :"
+ aboveAverage(averageA));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* @param low
* @param high
* @return
*/
public static int[] fillArray(int low, int high) {
System.out.println("Enter size of array:");
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
Random random = new Random();
int a[] = new int[size];
for (int i = 0; i < size; i++)
a[i] = random.nextInt(high - low) + low;
return a;
}
/**
* @param a
*/
public static void printArray(int a[]) {
for (int i = 0; i < a.length; i++) {
if (i != 0 && i % 5 == 0) {
System.out.println(a[i] + " ");
} else {
System.out.print(a[i] + " ");
}
}
}
/**
* @param a
* @return
*/
public static int count(int a[]) {
int count = 0;
System.out.print(" Enter the element:");
Scanner scanner = new Scanner(System.in);
int element = scanner.nextInt();
for (int i = 0; i < a.length; i++) {
if (a[i] > element)
count++;
}
return count;
}
/**
* @return
*/
public static boolean isSame() {
boolean flag = false;
for (int i = 0; i < arrayA.length; i++) {
for (int j = 0; j < arrayB.length; j++)
if (arrayA[i] == arrayB[j]) {
flag = true;
break;
} else {
flag = false;
}
}
return flag;
}
/**
* @return
*/
public static double findAverage() {
int sum = 0;
for (int i = 0; i < arrayA.length; i++) {
sum += arrayA[i];
}
return (double) sum / (double) arrayA.length;
}
/**
* @param averageA
* @return
*/
public static int aboveAverage(double averageA) {
int count = 0;
for (int i = 0; i < arrayB.length; i++) {
if (arrayB[i] > averageA)
count++;
}
return count;
}
}
OUTPUT:
Enter the low:2
Enter the high:15
Enter size of array:
10
Enter size of array:
10
arrayA:
11 9 13 7 8
4 2 9 9 12
arrayB:
2 13 8 7 7
10 3 14 13 2
Enter the element:11
Total no of integers greater than value of arrayA:2
arrayA and arrayB are same?false
Average of arrayA :8.4
No of element above the Average in arrayB :4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.