In java Write a method called maxValue that takes an integer
In java.
Write a method called maxValue that takes an integer array argument and returns the maximum value in the array. You do not need to include anything in the main method, but you may use main to test whether your maxValue method works correctly before submitting.
public class Main {
// FIX ME: write the maxValue method definition here. The maxValue method takes an integer array argument
// and returns the maximum value. Note that your method must work for both all positive or all negative numbers.
public static void main (String args[]) {
int [] arr = {21, 34, 9};
// TO DO (optional): It is recommended that you test your maxValue method here before submitting.
return;
}
}
Solution
Main.java
public class Main {
// FIX ME: write the maxValue method definition here. The maxValue method takes an integer array argument
// and returns the maximum value. Note that your method must work for both all positive or all negative numbers.
public static int maxValue(int a[]){
int max = a[0];
for(int i=0; i<a.length; i++){
if(max < a[i]){
max = a[i];
}
}
return max;
}
public static void main (String args[]) {
int [] arr = {21, 34, 9};
// TO DO (optional): It is recommended that you test your maxValue method here before submitting.
int max = maxValue(arr);
System.out.println(\"Max vallue is \"+max);
return;
}
}
Output:
Max vallue is 34
