简体   繁体   中英

How do I seperate main method into its own class?

So I wrote this Bubblesort code and everything is working fine, but now I want to split this up into two classes and I can't find out how to do this.

import java.util.Arrays;
public class bubblesorter
{
    public static int[] bubblesort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }

            }
        }
        return zusortieren;
    }
        public static void main(String[] args) {
        int[] unsortiert={1,5,8,2,7,4};
        int[] sortiert=bubblesort(unsortiert);

        for (int i = 0; i<sortiert.length; i++) {
            System.out.print(sortiert[i] + ", ");
        }
    }
}

Thanks for your help.

I think you want something like this:

Main class

public class Main
{
    public static void main(String[] args) {
        int[] unsortiert={1,5,8,2,7,4};
        int[] sortiert=BubbleSort.sort(unsortiert);

        for (int i = 0; i<sortiert.length; i++) {
            System.out.print(sortiert[i] + ", ");
        }
    }
}

Sort class

public class BubbleSort {
    public static int[] sort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }

            }
        }
        return zusortieren;
    }
}

Just copy paste your main into it's own file.

Main.java

public class Main
{
    public static void main(String[] args) {
        int[] unsortiert={1,5,8,2,7,4};
        int[] sortiert=BubbleSort.sort(unsortiert);

        for (int i = 0; i<sortiert.length; i++) {
            System.out.print(sortiert[i] + ", ");
        }
    }
}

BubbleSort.java

public class BubbleSort {
    public static int[] sort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }

            }
        }
        return zusortieren;
    }
}

As long as you refer to both files in the javac call when it comes time to compile, it'll all work nicely. However, for the sake of not being messy, some structure is good to have. Later in your Java experience, you'll be hearing about tools like Maven and Gradle, so I suggest getting in the habit of using their folder formats:

ProjectName/
    src/main/java/
        LuisIsLuis/Awesome/Package/
            Main.java
            BubbleSort.java

I did not follow "pretty package naming" conventions, I figure you'll learn that on your own later. The only thing you need to do with the files themselves is put package LuisIsLuis.Awesome.Package; as the first line in both files when organizing your code in this manner; if you're curious as to why, go look up package naming standards. That being said, your coursework will probably cover that soon.

It is pretty straight forward and depends on what you want to achieve. If you have a helper class with static methods, then add static method BubbleSorter to it and access it like rmpt mentioned above. Otherwise, you can store the method in a separate class BubbleSorter.java and access it via an instance of the class.

public class Main
{
    int[] unsortiert={1,5,8,2,7,4};
    Bubblesorter bubble = new Bubblesorter();
    int [] sortiert = bubble.bubblesort(unsortiert);

    for (int i = 0; i<sortiert.length; i++) {
        System.out.print(sortiert[i] + ", ");
    }
}

public class Bubblesorter
{
    public int[] bubblesort(int[] zusortieren) {
        int temp;
        for(int i=1; i<zusortieren.length; i++) {
            for(int j=0; j<zusortieren.length-i; j++) {
                if(zusortieren[j]<zusortieren[j+1]) {
                    temp=zusortieren[j];
                    zusortieren[j]=zusortieren[j+1];
                    zusortieren[j+1]=temp;
                }
            }
        }
        return zusortieren;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM