What should I do if I want to print out a method coded by one class from another class?

Asked 2 years ago, Updated 2 years ago, 23 views

There's a class called ArrayUtils and a class called ArrayUtils Tester. The ArrayUtils class requires coding methods and printing them using SOP in ArrayUtilsTester, but I did everything else, but it's blocked at the last part where I have to merge the two arrays to create a new array...I can't print it out like before...And I don't know if that merge method is right, because you've been changing it all over the place... In case the code gets too long (already long), I take away the ones that I've already successfully printed, but I'm supposed to get the minimum, maximum, specific number, etc. of each array.Both the size of the arrays and the values output according to that size must be random numbers between 0 and 100.


    import java.util.Random;

    public class ArrayUtils 
    {
    private int[] arr;

    public ArrayUtils(int sizeOfArray)
    {
        arr = new int [sizeOfArray];
        Random random = new Random();

        for (sizeOfArray = 0; sizeOfArray < arr.length; sizeOfArray++)
        {
            arr [sizeOfArray] = random.nextInt(101);
        }

    }
    public int[] mergeArrays(int[] arr1, int[] arr2) 
    {
        int length = arr1.length+arr2.length;  
        int [] arrSum = new int[length];

        for(int i=0; i < length; i++) 
        {
            if(i < arr1.length) 
            {
                arrSum[i] = arr1[i];   
            }
            else 
            {
                arrSum[i] = arr2[i - arr1.length];
            }
        }
        return arrSum;  
    } 
} 

    import java.util.Arrays;
    import java.util.Random;

    public class ArrayUtilsTester
    {
        public static void main(String[] args)
        {
            Random random = new Random();
            int sizeOfArray = random.nextInt(101);

            ArrayUtils arr1 = new ArrayUtils(sizeOfArray);
            ArrayUtils arr2 = new ArrayUtils(sizeOfArray);
            ArrayUtils arr3 = new ArrayUtils(sizeOfArray);  

            System.out.println(...)
         }
    }

java

2022-09-22 14:20

1 Answers

There are two simple ways to think about it, and I think the results will come out as intended.

int[] arrSum = arr1.mergeArrays(arr2.arr, arr3.arr);
public int[] getArr() {
    return arr;
}


int[] arrSum = arr.mergeArrays(arr2.getArr(), arr3.getArr());


2022-09-22 14:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.