•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,468 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,780 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 542 | Replies: 2
![]() |
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:
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:
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.
•
•
Join Date: Apr 2006
Location: Mumbai, India
Posts: 351
Reputation:
Rep Power: 0
Solved Threads: 5
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:
Rep Power: 0
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); } }
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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


Linear Mode