| | |
Rusty at Java
![]() |
Hey everyone. This is pretty lame of me having to do this, but I haven't done Java for about a year and this algorithms class I'm taking assumes we remember all the details. Anyway, I'm trying to implement a method from an interface in another class's main method.
Here's the interface:
and here's the main class:
The issue is that I get a compilation error about the fact that I'm trying to override an interface's method with a static method. The problem is it also won't let me use a non-static method in the main() method. So basically, what do I have to do to make this method from an interface work in the main method?
I feel really silly for asking this question, but it's been a really long time. Thanks for your patience.
Here's the interface:
Java Syntax (Toggle Plain Text)
public interface SortingAlgorithm extends Algorithm { public int[] createSortIndex(java.lang.Comparable[] data); public int[] createSortIndex(int[] data); public void sortInPlace(java.lang.Comparable[] data); public void sortInPlace(int[] data); }
and here's the main class:
Java Syntax (Toggle Plain Text)
abstract public class InsertionSort implements SortingAlgorithm{ static int rangeLow = 100; static int rangeHigh = 1000; static int[] makeRandomData (int numValues) { int[] data = new int [numValues]; for (int i=0; i<data.length; i++) data[i] = (int) UniformRandom.uniform ( (int) rangeLow, (int) rangeHigh ); return data; } static void printData (int[] data) { System.out.print ("Data: "); for (int i=0; i<data.length; i++) System.out.print (" " + data[i]); System.out.println (""); } static void sortInPlace (int[] data) { for(int i = 1; i < data.length; i++) { int index = data[i]; int j = i; while((data[j - 1] > index) && (j > 0)) { data[j] = data[j - 1]; j = j - 1; } data[j] = index; } } public static void main (String[] argv) { if ( (argv == null) || (argv.length == 0) ) { System.out.println ("Usage: java InsertionSort <number-of-values>"); System.exit(0); } try { int numValues = Integer.parseInt (argv[0]); int[] data = makeRandomData (numValues); System.out.println ("BEFORE SORTING: "); printData (data); sortInPlace (data); System.out.println ("AFTER SORTING: "); printData (data); } catch (Exception e) { System.out.println (e); } } }
The issue is that I get a compilation error about the fact that I'm trying to override an interface's method with a static method. The problem is it also won't let me use a non-static method in the main() method. So basically, what do I have to do to make this method from an interface work in the main method?
I feel really silly for asking this question, but it's been a really long time. Thanks for your patience.
You will have to declare an instance method if you are overriding.
To use a non-static method in main(), you will need to create an instance/object of the class. Since this class is abstract, you would not be able to achieve it unless you implement a concrete subclass for this abstract class.
To use a non-static method in main(), you will need to create an instance/object of the class. Since this class is abstract, you would not be able to achieve it unless you implement a concrete subclass for this abstract class.
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
java Syntax (Toggle Plain Text)
abstract public class InsertionSort implements SortingAlgorithm { //.... public void sortInPlace(int[] data) { for (int i = 1; i < data.length; i++) { int index = data[i]; int j = i; while ((data[j - 1] > index) && (j > 0)) { data[j] = data[j - 1]; j = j - 1; } data[j] = index; } } //... } public static void main(String[] argv) { if ((argv == null) || (argv.length == 0)) { System.out.println("Usage: java InsertionSort <number-of-values>"); System.exit(0); } try { int numValues = Integer.parseInt(argv[0]); int[] data = makeRandomData(numValues); System.out.println("BEFORE SORTING: "); printData(data); InsertionSort insertionSort = new InsertionSort() { public int[] createSortIndex(Comparable[] data) { return null; } public int[] createSortIndex(int[] data) { return null; } public void sortInPlace(Comparable[] data) { } }; insertionSort.sortInPlace(data); System.out.println("AFTER SORTING: "); printData(data); } catch (Exception e) { System.out.println(e); } }
![]() |
Similar Threads
- calculator program (Java)
- Java 3D Environment (Java)
- Java 3D (Java)
- Java Expert (Needed) (Java)
- Java Chat Reboots (Java)
Other Threads in the Java Forum
- Previous Thread: connection mysql
- Next Thread: I need help in data file please
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows





