开发者

How to run instance methods from main

开发者 https://www.devze.com 2023-04-13 09:34 出处:网络
Sorry but I\'m having a major brain fart here (guess that\'s what a few days of little sleep get you). Without changing anything to static how can I make a main() that will run this.

Sorry but I'm having a major brain fart here (guess that's what a few days of little sleep get you). Without changing anything to static how can I make a main() that will run this.

package merger;

import java.util.Random;

public class another {

    public int[] numbers;
    private final static int size = 100;
    private static int maxNumber = 30;
    private final static int limit = 10;
    public int number;
    private boolean Insertion = false;

    public void arraytosort(){
        numbers = new int[size];
        Random number = new Random();
        for (int i=0; i< numbers.length; i++){
            numbers[i] = number.nextInt(maxNumber);
        }
        test(numbers);
    }

    public void test(int[] array){
        this.numbers = array;
        number = array.length;
        mergesort(0,number - 1);
    }

     public void mergesort(int low, int high){
         if(Insertion || high-low < limit){
             insertionsort(low, high);
             return;
         }
         if (low<high){
             int middle = (low+high) / 2;
             mergesort(low, middle);
             mergesort(middle +1, high);
             merge(low,middle,high);
             return;
         }
     }

     public void merge(int low, int middle, int high){
         int[] temp = new int[number];
         for (int i=low;i<=high; i++){
             temp[i] = numbers[i];
         }
         int i = low;
         int j = middle+1;
         int k = low;
         while (i<=middle || j<=high){
             if (temp[i] <= temp[j]){
                 numbers[k] = temp[i];
                 i++;
             }
             else{
       开发者_Go百科          temp[k] = temp[j];
                 j++;
             }
             k++;
         }
         while (i<=middle){
             temp[k] = temp[i];
             k++;
             i++;
         }
         temp = null;
         return;
             }

     public void insertionsort(int low, int high){
         for(int i=low+1;i<=high;i++){
             int t = numbers[i];
             for(int j = i-1; j>=low; j--){
                 if(t>numbers[j]) break;
                 numbers[j+1] = numbers[j];

             numbers[j+1] = t;
             }
         }
     }

    /**
     * @param args
     */
    public static void main(String[] args){


    }
}

I just need to be able to test it to see if this is working. In my head it seems like it should work.

Thanks


Without changing anything to static how can I make a main() that will run this.

You have to create an instance of the class:

public static void main(String[] args){
    another instance = new another();
    instance.whateverMethodYouLike();
}

BTW, please follow the Java convention and name classes with a capital first letter.


public static void main(String[] args)
{
  another myObject = new another();
  myObject.arraySort(); //will call test which will call mergesort

  myObject.insertionSort(0,myObject.numbers.size()-1); //nothing calls insertion sort

}

PLEASE following code conventions like capitalizing first letter of class name and camelCasing for methods/variables.

If you want to see the sorted output, print the array on screen.

public variables is bad, bad, bad make them private (like numbers[]) in your case...

In the main method you "create" the instance of the object of that class and not directly call methods. It's a 'special' method so to speak different from other public/private/static methods...

I suggest reading up on some elementary java book like thinking in java which is available for free online...

0

精彩评论

暂无评论...
验证码 换一张
取 消