-1

I am having trouble passing an array to my sorting classes because i need to sort the same array with two different algorithms. I am getting the error

Multiple markers at this line
- Syntax error on token(s), misplaced 
 construct(s)
- Syntax error on token(s), misplaced 
 construct(s)
- The constructor Mergesort() is undefined
- Syntax error on token "originalArray", delete 
 this token

The code

class sorterProgram {

public static void main(String args[]) {
    //Declares instances of the sorting classes

    int[] originalArray = new int[500];
    for (int i = 0; i < 500; i++) {
        originalArray[i] = (int) Math.round(Math.random() * 100);
    }
    Quicksort q = new Quicksort(originalArray);
    Mergesort m = new Mergesort(originalArray);
    //declares keyboard to accept user input for type of sort
    Scanner keyboard = new Scanner(System.in);
    //choice set as one so the do-while and if statements will start
    int choice = 1;
    // loop that does sorting untill the user is done


    do {
        System.out.println("Enter the # to start the sort of a 500 Element Array: \n1: Quicksort then Mergesort \n2: Exit");
        //only works if the user chooses the correct numbers
        if (1 == choice || choice == 2) {
            choice = keyboard.nextInt();
        }
        switch (choice) {
        case 1:
            System.out.println("Before Quicksort");
            q.print();
            long timeQuicksort = System.nanoTime();
            q.quicksort();
            long completedInQuicksort = System.nanoTime() - timeQuicksort;
            System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("After Quicksort ");
            q.print();
            System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("Before Mergesort");
            m.print();
            long timeMergesort = System.nanoTime();
            m.sort();
            long completedInMergesort = System.nanoTime() - timeMergesort;
            System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("After Mergesort ");
            m.print();

            System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            System.out.println("Time took to complete Quicksort (nanoseconds): "+ completedInQuicksort);
            System.out.println("Time took to complete Mergesort (nanoseconds): "+ completedInMergesort);
            break;
        case 2:
            System.out.println("Thanks for using the Quicksort and Mergesort");

        }

    } while (choice != 2);

}
}

and these are my two sorter constructors

class Quicksort {
int array[];
int size;

public Quicksort(int[] n) {
    size = n.length;
    // create array for merge sorting with size n+1
    array = new int[n.length + 1];
    // assign value into the array
    for (int i = 0; i < n.length; i++) {
        n[i] = array[i];
    }

class Mergesort {

private int size;
private int[] array;
private int[] tempMergeArray;

public Mergesort(int[] n) {

    size = n.length;
    // create array for merge sorting with size n+1
    array = new int[n.length + 1];
    // assign value into the array
    for (int i = 0; i < n.length; i++) {
        n[i] = array[i];
    }

    // set the last value as a big value so the sorting ends properly
    array[n.length] = 99999;
}

EDIT: i have it passing now but they are just passing zeros

Ryan_lx
  • 13
  • 4

2 Answers2

1

When passing arrays as variables into functions and constructors there is the same syntax as if you were passing in a normal variable.

Correct:

int[] myArray = new int[12];
myObject.myFunction(myArray);

Incorrect:

int[] myArray = new int[12];
myObject.myFunction(int[] myArray[]);

You may want to review this, it will help you with using arrays in java.

Also, you may want to make sure that they are in the same package. Based on your lack of a package my.package; it looks like you have the classes in a default package. This is bad because then you cannot import them from a seperate package ex. classes in org.myamazingapplication cannot do import myclass (assuming myclass is in the default package). Also many apis such as bukkit require a package to be used.

john01dav
  • 1,842
  • 1
  • 21
  • 40
0

Change

Quicksort q = new Quicksort(int [] originalArray[]);
Mergesort m = new Mergesort(int[] originalArray[] );

to

Quicksort q = new Quicksort(originalArray);
Mergesort m = new Mergesort(originalArray);

and inside both of your constructors, change

for (int i = 0; i < n.length; i++) {
    n[i] = array[i];
}

to

for (int i = 0; i < n.length; i++) {
    array[i] = n[i];
}
JoeC
  • 1,850
  • 14
  • 12