Rusty at Java

Reply

Join Date: Sep 2007
Posts: 9
Reputation: Icetigris is an unknown quantity at this point 
Solved Threads: 0
Icetigris's Avatar
Icetigris Icetigris is offline Offline
Newbie Poster

Rusty at Java

 
0
  #1
Sep 16th, 2007
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:

  1. public interface SortingAlgorithm extends Algorithm
  2. {
  3. public int[] createSortIndex(java.lang.Comparable[] data);
  4.  
  5. public int[] createSortIndex(int[] data);
  6.  
  7. public void sortInPlace(java.lang.Comparable[] data);
  8.  
  9. public void sortInPlace(int[] data);
  10. }

and here's the main class:

  1. abstract public class InsertionSort implements SortingAlgorithm{
  2.  
  3. static int rangeLow = 100;
  4. static int rangeHigh = 1000;
  5.  
  6. static int[] makeRandomData (int numValues)
  7. {
  8. int[] data = new int [numValues];
  9. for (int i=0; i<data.length; i++)
  10. data[i] = (int) UniformRandom.uniform ( (int) rangeLow, (int) rangeHigh );
  11. return data;
  12. }
  13.  
  14. static void printData (int[] data)
  15. {
  16. System.out.print ("Data: ");
  17. for (int i=0; i<data.length; i++)
  18. System.out.print (" " + data[i]);
  19. System.out.println ("");
  20. }
  21.  
  22.  
  23. static void sortInPlace (int[] data)
  24. {
  25. for(int i = 1; i < data.length; i++)
  26. {
  27. int index = data[i];
  28. int j = i;
  29. while((data[j - 1] > index) && (j > 0))
  30. {
  31. data[j] = data[j - 1];
  32. j = j - 1;
  33. }
  34. data[j] = index;
  35. }
  36. }
  37.  
  38. public static void main (String[] argv)
  39. {
  40. if ( (argv == null) || (argv.length == 0) ) {
  41. System.out.println ("Usage: java InsertionSort <number-of-values>");
  42. System.exit(0);
  43. }
  44.  
  45. try {
  46. int numValues = Integer.parseInt (argv[0]);
  47. int[] data = makeRandomData (numValues);
  48. System.out.println ("BEFORE SORTING: ");
  49. printData (data);
  50. sortInPlace (data);
  51. System.out.println ("AFTER SORTING: ");
  52. printData (data);
  53. }
  54. catch (Exception e) {
  55. System.out.println (e);
  56. }
  57. }
  58.  
  59. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: aniseed is an unknown quantity at this point 
Solved Threads: 6
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: Rusty at Java

 
0
  #2
Sep 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: jtonic is an unknown quantity at this point 
Solved Threads: 0
jtonic jtonic is offline Offline
Newbie Poster

Re: Rusty at Java

 
0
  #3
Sep 17th, 2007
  1. abstract public class InsertionSort implements
  2. SortingAlgorithm {
  3. //....
  4. public void sortInPlace(int[] data) {
  5. for (int i = 1; i < data.length; i++) {
  6. int index = data[i];
  7. int j = i;
  8. while ((data[j - 1] > index) && (j > 0)) {
  9. data[j] = data[j - 1];
  10. j = j - 1;
  11. }
  12. data[j] = index;
  13. }
  14. }
  15. //...
  16. }
  17. public static void main(String[] argv) {
  18. if ((argv == null) || (argv.length == 0)) {
  19. System.out.println("Usage: java InsertionSort <number-of-values>");
  20. System.exit(0);
  21. }
  22.  
  23. try {
  24. int numValues = Integer.parseInt(argv[0]);
  25. int[] data = makeRandomData(numValues);
  26. System.out.println("BEFORE SORTING: ");
  27. printData(data);
  28. InsertionSort insertionSort = new InsertionSort() {
  29.  
  30. public int[] createSortIndex(Comparable[] data) {
  31. return null;
  32. }
  33.  
  34. public int[] createSortIndex(int[] data) {
  35. return null;
  36. }
  37.  
  38. public void sortInPlace(Comparable[] data) {
  39. }
  40.  
  41. };
  42. insertionSort.sortInPlace(data);
  43. System.out.println("AFTER SORTING: ");
  44. printData(data);
  45. } catch (Exception e) {
  46. System.out.println(e);
  47. }
  48. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC