Home > Back-end >  How do I bring over an Array from another class?
How do I bring over an Array from another class?

Time:11-12

I'm completely new to Java, and doing some online courses. I was doing this task when the online instructions did the task under a single Main class. I was trying to do some testing as I like to try keep my main class simple and was wondering how I could go about bringing about different arrays and method from another class over to my main class.

import java.util.Scanner;

public class Arrays {

    public static Scanner scan = new Scanner(System.in);

    public static int[] getIntegers(int number) {

        System.out.println("Please enter "   number   " numbers\r");
        int[] entered = new int[number];
        for(int i = 0; i < entered.length; i  ) {
            entered[i] = scan.nextInt();
        }
        return entered;
    }

    public static void printArray(int[] entered) {
        for(int i = 0; i < entered.length; i  ) {
            System.out.println("Element "   i   ", typed value was "   entered[i]);
        }
    }

    public static int[] sortIntegers(int[] entered) {
        int[] sortedArray = new int[entered.length];
        for(int i = 0; i < entered.length; i  ) {
            sortedArray[i] = entered[i];
        }

        boolean flag = true;
        int temp;
        while(flag) {
            flag = false;
            for(int i = 0; i < sortedArray.length - 1; i  ) {
                if(sortedArray[i] < sortedArray[i   1]) {
                    temp = sortedArray[i];
                    sortedArray[i] = sortedArray[i   1];
                    sortedArray[i   1] = temp;
                    flag = true;

                }
            }
        }
        return sortedArray;
    }
}

And my Main class.

public class Main {

    public static void main (String[] args) {


        int[] myIntegers = getIntegers(5);
        int[] sorted = sortIntegers(myIntegers);
        printArray(myIntegers);
        printArray(sorted);


    }
}

IntelliJ allows me to just import static className over but I want to try it without a method that just imports the class over by using OOP standards.

as you can see I want to bring over getIntegers, printArray, and sortIntegers.

CodePudding user response:

You can have the Main class inherit the Arrays class by using the extends but Java doesn't allow multiple inheritance so you wouldn't be able to extend any other classes. Aside from the fact that inheritance should be done where the parent and child class share a logical relationship.

Instead you can create a generic interface and the Arrays class can implement all the methods that you already have in there. Then the Main class can implement the interface (i.e. IterableInterface) and you can even specify which concrete type you're using and what ivars it should contain, but that's more work than what you're probably trying to achieve.

Honestly, importing a class like is no problem. Often times you will find that the disadvantage of OOP is that inheritance in cases where composition or aggregation should be used. This is not applicable in this case because an import is more appropriate but be aware of it.

CodePudding user response:

I recommend you to learn java modifiers. It will be helpful to you for your future. In this case. you can use instance method instead of static method. example :-

public class Main {
public static void main(String[] args) {
    Arrays arrays = new Arrays();
    int[] myIntegers = arrays.getIntegers(5);
    int[] sorted = arrays.sortIntegers(myIntegers);
    arrays.printArray(myIntegers);
    arrays.printArray(sorted);
}

}

class Arrays {

public static Scanner scan = new Scanner(System.in);

public  int[] getIntegers(int number) {

    System.out.println("Please enter "   number   " numbers\r");
    int[] entered = new int[number];
    for(int i = 0; i < entered.length; i  ) {
        entered[i] = scan.nextInt();
    }
    return entered;
}

public void printArray(int[] entered) {
    for(int i = 0; i < entered.length; i  ) {
        System.out.println("Element "   i   ", typed value was "   entered[i]);
    }
}

public int[] sortIntegers(int[] entered) {
    int[] sortedArray = new int[entered.length];
    for(int i = 0; i < entered.length; i  ) {
        sortedArray[i] = entered[i];
    }

    boolean flag = true;
    int temp;
    while(flag) {
        flag = false;
        for(int i = 0; i < sortedArray.length - 1; i  ) {
            if(sortedArray[i] < sortedArray[i   1]) {
                temp = sortedArray[i];
                sortedArray[i] = sortedArray[i   1];
                sortedArray[i   1] = temp;
                flag = true;

            }
        }
    }
    return sortedArray;
}

}

CodePudding user response:

tl;dr

Re-design your app to use objects.

  • Write a class for state management.
  • Write a class for the user-interface.
  • Write a class to run the show, to hold instances of the state manager and the user-interface objects, to contain the main method.

To answer your query, “How do I bring over an Array from another class?”:

  • Related