Java question Write a method that returns the integer with t

Java question Write a method that returns the integer with the maximum absolute value. In the case of ties, take the integer that appears earliest in the array. For example: [3, -6, 5, 2, 4] => -6 [-5, 2, 5] => -5

Solution

import java.util.*;
class Finder
{

   public int display(int[] b,int limit)

   {
       System.out.println(\"inside the method\");
       int max=0;
       int max_pos=0;
       int x,y;
       for(int j=0;j < limit;j++)
       {

           for(int k=j+1;k < limit;k++)
           {

               if ( Math.abs(b[j]) >= Math.abs(b[k]))
               {
                   int t=b[j];
                   b[j]=b[k];
                   b[k]=t;
               }
              
                              
           }
       }  

       return b[limit-1];  
   }
}
public class Sample
{
   public static void main(String args[])
   {
       Scanner in=new Scanner(System.in);
       int a[]=new int[100];

       System.out.println(\"enter the no of elements in a array\");
       int n=in.nextInt();
       for(int i=0;i<n;i++)
       {
           a[i]=in.nextInt();
       }

       Finder ob=new Finder();
       int r=ob.display(a,n);
       System.out.println(\"the maximum absolute is \" + r);
   }
}

 Java question Write a method that returns the integer with the maximum absolute value. In the case of ties, take the integer that appears earliest in the array

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site