current position:Home>Design a mathutils Java class file

Design a mathutils Java class file

2022-02-02 20:59:31 CSDN Q & A

(1) Design static functions to maximize getMax , Pass in 1 individual int Array type parameter ,(2) Design static function to find the minimum value getMin , Pass in 1 individual int Array type parameter , return int Type data (3) Design static function inversion reverse, Pass in 1 individual int Array type parameter , Invert the data in the array and return the array to (4) Design static functions getIndex Find the subscript position corresponding to the data in the current array , Pass in 1 individual int Array type parameters and int Type data , Return the index of the data in the array to




Refer to the answer 1:
public class MathUtils {    /** * (1)  Design static functions to maximize  getMax  Pass in 1 individual int Array type parameter , return int Type data such as  [12345]  return  5 (10 branch ) * @param numbers * @return */    public static int  getMax(int[] numbers){        int rs = 0;        if (numbers==null)            return rs;        for (int i = 0; i < numbers.length; i++) {            if (i==0)                rs=  Math.max(numbers[i],numbers[i+1]);            else                rs=  Math.max(numbers[i],rs);        }        return rs;    }    /** * (2)  Design static function to find the minimum value getMin ,  Pass in 1 individual int Array type parameter , return int Type data  ,  Such as [1,2,3,4,5]  return  1 (10 branch ) * @param numbers * @return */    public static int  getMin(int[] numbers){        int rs = 0;        if (numbers==null)            return rs;        for (int i = 0; i < numbers.length; i++) {            if (i==0)                rs=  Math.min(numbers[i],numbers[i+1]);            else                rs=  Math.min(numbers[i],rs);        }        return rs;    }    /** * (3)  Design static function inversion reverse, Pass in 1 individual int Array type parameter , Invert the data in the array and return the array to , Such as [1,2,3,4,5] ,  return [5,4,3,2,1] (10 branch ) * @param numbers numbers * @return */    public static int[] doReverse(int[] numbers){        if (numbers==null)            return new int[]{};        int[] rs = new int[numbers.length];        int temp=0;        for (int i = numbers.length-1; i >= 0; i--) { 
                rs[temp] = numbers[i];            temp++;        }        return rs;    }    /** * (4)  Design static functions getIndex Find the subscript position corresponding to the data in the current array , Pass in 1 individual int Array type parameters and int Type data , Return the index of the data in the array to , Such as , stay [11,22,33,44,55]  lookup  55 ,  return   Subscript  4. (10 branch ) */    public static int getIndex(int[] numbers,int number){        for (int i = 0; i < numbers.length; i++) {            if (numbers[i] == number){                return i;            }        }        return -1;    }    /** * (5)  Design static functions to exchange the contents of the array swap, Pass in 1 individual int Array type parameter , No return value , Such as incoming [88,99] ,  call swap The contents of the last array are  [99,88] (10 branch ) */    public static void swap(int[] numbers){        int temp;        for (int i = 0; i < numbers.length-1; i++) {            temp = numbers[i];            numbers[i] = numbers[i+1];            numbers[i+1] = temp;        }    }    /** * (6)  Design static function to count the sum of data in the array  getSum, Pass in 1 individual int Array type parameter , Returns the sum of the data in the array , Such as  [1,2,3,4,5]  return  15 (10 branch ) */    public static int getSum(int[] numbers){        int sum = 0;        for (int number : numbers) {            sum += number;        }        return sum;    }    /** * (7)  Design static function to count the average value of data in the array  getAvg,  Pass in 1 individual int Array type parameter , Returns the average value of the data in the array , Such as  [1,2,3,4,5]  return  3 (10 branch ) */    public static float getAvg(int[] numbers) {        int sum = 0;        for (int number : numbers) {            sum += number;        }        return (float)sum/numbers.length;    }    /** *(8)  Extended function realization  :  Design static function sorting sort Method , Pass in 1 individual int Array type parameter , No return value   Such as  [ 10,2,8,5,7] ,  call sort after  [2,5,7,8,10] (20 branch ) */    public static int[] doSort(int[] numbers) {        Arrays.sort(numbers);        return numbers;    }    public static void main(String[] args) {        int[] a = new int[]{
   
   
   
   11,20,2,6,7};        //1        System.out.println(getMax(a));        //2        System.out.println(getMin(a));        //3        System.out.println(Arrays.toString(doReverse(a)));        //4        System.out.println(getIndex(a,11));        //5        int[] b = new int[]{
   
   
   
   11,20};        System.out.println(Arrays.toString(b));        //6        System.out.println(getSum(a));        //7        System.out.println(getAvg(a));        //8        System.out.println(Arrays.toString(doSort(a)));    }}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022059307446.html

Random recommended