jeudi 10 avril 2014

Trying to pass an array to my class constructor


Vote count:

0




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



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(int [] originalArray[]);
Mergesort m = new Mergesort(int[] 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;
}


asked 2 mins ago






Aucun commentaire:

Enregistrer un commentaire