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

In java. Write a method called maxValue that takes an integer array argument and

ID: 3941112 • Letter: I

Question

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;
   }

}

Explanation / Answer

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

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