Java The method is undefined for this type

I am having a problem with calling a method that is in a different class. This main method is in a class on its own called lab14, and the heapSort() method is in a different class called HeapSort. Both of these classes are in the default package. I am getting the error "The method heapSort(Vector) is undefined for the type Lab14" and I don't understand why, please help.

below is the main method in the lab 14 class

public static void main(String args[]) { Heap myheap = new Heap(); Vector<StudentGPA> vec = new Vector(); int [] br = new int[20]; double [] dr = new double[20]; int i = 0; int j = 0; try { //String inputLine; //stores each line from the file Scanner scanLine; Scanner input = new Scanner(new File("")); while (input.hasNextLine()){ scanLine = new Scanner(input.nextLine()); int id = scanLine.nextInt(); //br[i] = id; String name =; double gpa = scanLine.nextDouble(); //dr[i]= gpa; //myStr.add(name); if(scanLine.hasNext()) { String advisor =; GraduateStudentGPA grad = new GraduateStudentGPA(id,name,gpa,advisor); vec.add(grad); } else { StudentGPA reg = new StudentGPA(id,name,gpa); vec.add(reg); } i++; j++; } input.close(); } catch (IOException e) { System.out.println("IOException in reading input file!!!"+e); } heapSort(vec); }

Below is the code for the HeapSort class

public class HeapSort <E extends Comparable<? super E>>{ /** sorts the input vector using heap Sort <ul> <li> iterates * through each element of the input vector and inserts each * element to the heap by calling {\tt heapInsert}. <li> deletes * each of the inserted items by calling {\tt heapDelete} the * appropriate number of times, and fills up the vector with the * returned elements. </ul> If you are using the * minheap implementation, this insertion and deletion of all * items will produce a list of items sorted by their key * attribute values. * @param vec input vector */ public void heapSort(Vector<StudentGPA> vec){ // -- TO COMPLETE -- Heap myheap = new Heap<E>(); for(int i = 0; i <vec.size(); i++) { myheap.heapInsert(vec.elementAt(i)); } for(int i = 0; i <vec.size(); i++) { vec.setElementAt((StudentGPA) myheap.heapDelete(), i); } } }

-------------Problems Reply------------

You gave the answer:

you're trying to call heapSort method from Lab14 class, but heapSort method is defined on a different class. The compilation error must be on this line.-


You need to instantiate a HeapSort object, and then call its heapSort method like this.-

HeapSort myHeapSortObject = new HeapSort();

Your heapsort method can be declared static, at least in its current state

public static <E extends Comparable<? super E>> void heapSort(Vector<StudentGPA> vec) {

You can then access it in your main method like this


You need to either:

1) make the heapSort method public static... and invoke it as HeapSort.heapSort(vec)

or 2) Invoke the method as new HeapSort().heapSort(vec)

You have several issues going on here.

  1. You call heapSort(vec); from within the static main of class Lab14. This would mean that the compiler expects a method matching signature static void heapSort(Vector<StudentGPA> vec) within the Lab14 class. Up to you how you solve that.
  2. Change Vector<StudentGPA> vec = new Vector(); into Vector<StudentGPA> vec = new Vector<>();. Note that addition of the angular brackets <>.
  3. There are actually many more problems, possibly because it is still an unfinished work. Probably I should stay focused on your original question, for which 1./ is the answer.
Category:java Views:2 Time:2018-03-11

Related post

Copyright (C), All Rights Reserved.

processed in 0.232 (s). 11 q(s)