Results 1 to 7 of 7
  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default BubbleSorting Problem: invalid method declaration

    So im so close to finishing this bubblesorting prog. all i have is one error.

    Take a look see if you can help

    All help is appreciated:)

    Java Code:
    public class BubbleSort
    {
     class ArrayBub
    {
      private long a[];
      private int nElems;
    }
    
    public ArrayBub(int max)
    {
       a=new long[max];
       nElems=0;
    }
    public void insert(long value)
    {
    	a[nElems]=value;
    	nElems++;
    }
    public void display()
    {
    	for(int j=0;j<nElems;j++)
    	System.out.print(a[j]+" ");
    	Util.skip(1);
    }
    public void bubbleSort()
    {
       int out,in;
       for(out=nElems-1; out>1; out--)
       for(in=0; in<out; in++)
       if( a[in] > a[in+1] )
       swap(in, in+1);
    }
    public void swap(int one,int two)
    {
    	long temp=a[one];
    	a[one]=a[two];
    	a[two]=temp;
    }
    class BubbleSortApp
    {
    	public static void main(String[] args)
    	{
    		int maxSize=100;
    		ArrayBub arr;
    		arr=new ArrayBub(maxSize);
    		arr.insert(77);
    		arr.insert(99);
            arr.insert(44);
            arr.insert(55);
            arr.insert(22);
            arr.insert(88);
            arr.insert(11);
            arr.insert(00);
            arr.insert(66);
            arr.insert(33);
            arr.display(); 
            arr.bubbleSort(); 
            arr.display();
    
    		
    	}
    }
    }
    And my error is:

    Java Code:
    C:\BubbleSort.java:10: invalid method declaration; return type required
    public ArrayBub(int max)
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by SwEeTAcTioN View Post
    Java Code:
    public class BubbleSort
    {
     class ArrayBub
    {
      private long a[];
      private int nElems;
    }
    
    public ArrayBub(int max)
    {
       a=new long[max];
       nElems=0;
    }
    ...
    And my error is:

    Java Code:
    C:\BubbleSort.java:10: invalid method declaration; return type required
    public ArrayBub(int max)
    Right, your ArrayBub method is not a constructor. It's located in the BubbleSort class; it's not in ArrayBub, should it be??
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    im not sure really???:(:(:confused::confused:

    how would you do that
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Move the ArrayBub constructor inside of the AraryBub class - literally, physically move the code, and it will work fine.

    Edit:
    OK, after reviewing your code, it looks like your Class name should be named ArrayBub, instead of BubbleSort, since ALL the methods are part of the ArrayBub class, and not in BubbleSort. Is there a reason you used a nested class?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    so i tryed that and no success:(
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    You should be using only one class for this. It seems you called it ArrayBub. All the methods you have should be in this one class.

    Your other classes BubbleSort and BubbleSortApp do nothing but introduce bugs into your program. Put ALL the methods in the same class, ArrayBub, and it works fine.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by CodesAway View Post
    Is there a reason you used a nested class?
    Good question.

    @OP Saying you had "no success" conveys very little information about what your code now is, or what the effect of compiling it was.

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  2. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  3. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 05:38 AM
  4. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 12:10 PM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •