Using Java write all 4 methods in one class
1) Write a value-returning method that returns the number of elements in an integer array.
2) Write a void method that multiples by 2 all the elements in an array of float.
3) Write a value- returning method that returns the product of all elements in an integer array.
4) Write a method that returns the total # of elements greater or equal to 90 in an array of integers.
Please upvote if you are able to understand this and if there is any query do mention it in the comment section.
CODE:
public class Main
{
public static void main(String[] args) {
int numInt[] = {102, 100, 60, 50, 120, 140, 80,
40};//creating an array of integer
double numFLoat[] = {1.2, 2.2, 4.4, 5.78, 11.24,
98.22};//creating an array of double
System.out.println("Number of elements in array:
");
System.out.println(numElems(numInt));//calling the method
numElems
System.out.println("After multiplying the each element of array
with 2: ");
mul(numFLoat);//calling the method numFloat
System.out.println("The product of all elements of array: ");
System.out.println(prod(numInt));//calling the method prod
System.out.println("Elements of array which are greater then or
equal to 90");
System.out.println(total(numInt));//calling the method total
}
public static int numElems(int arr[]) {//method for
finding length of integer array
int len = arr.length;
return len;
}
public static void mul(double arr[]) {//method for
multiplying double array with 2
double res = 0.0;
for (int i = 0; i < arr.length; i++) {
res = 2 * arr[i];
System.out.println(res);
}
}
public static int prod(int arr[]) {//method for
product of each array element
int res = 1;
for (int i = 0; i < arr.length; i++) {
res = res * arr[i];
}
return res;
}
public static int total(int arr[]) {//method for
finding elements greater then 90
for (int i = 0; i < arr.length; i++) {
if(arr[i] >= 90) {
System.out.println(arr[i]);//printing elements
}
}
return 0;
}
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.