944,008 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 969
  • Java RSS
Sep 16th, 2007
0

Rusty at Java

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Icetigris is offline Offline
9 posts
since Sep 2007
Sep 16th, 2007
0

Re: Rusty at Java

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.
Reputation Points: 48
Solved Threads: 7
Posting Whiz
aniseed is offline Offline
353 posts
since Apr 2006
Sep 17th, 2007
0

Re: Rusty at Java

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtonic is offline Offline
2 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: connection mysql
Next Thread in Java Forum Timeline: I need help in data file please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC