User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2007
Posts: 9
Reputation: Icetigris is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Icetigris's Avatar
Icetigris Icetigris is offline Offline
Newbie Poster

Rusty at Java

  #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:

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Mumbai, India
Posts: 351
Reputation: aniseed is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 5
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: Rusty at Java

  #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  
Join Date: Sep 2007
Posts: 2
Reputation: jtonic is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jtonic jtonic is offline Offline
Newbie Poster

Re: Rusty at Java

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:21 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC