Results 1 to 9 of 9
  1. #1
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default [SOLVED] trouble in sorting...please help

    Add an interface, SizeComparable, that includes only the boolean isBiggerThan( Shape other) method. In your ShapeSet class, provide a method void sort(), that sorts the shapes of the shape set by class Shape implementing the SizeComparable interface.
    This is the problem I wrote codes but it gives run-time error.Could you help me?
    Java Code:
    public abstract class Shape implements SizeComparable {
    Shape a;
    
    public abstract double getArea();
    
    public boolean isBiggerThan(Shape other){
    if(other.getArea()>a.getArea())
    return true;
    else
    return false;	
    }
    }
    
    public interface SizeComparable {
    public boolean isBiggerThan(Shape other);
    }
    
    
    public class ShapeSet {
    //properties
    Shape[] shapeList;
    int number;
    //constructor
    public ShapeSet(){
    shapeList=new Shape[19];
    number=0;
    }
    ............
    .........
    public void sort()
    {
    int in, out;
    
    for(out=1; out<number; out++) // out is dividing line
    {
    Shape temp = shapeList[out]; // remove marked Shape
    in = out; // start shifting at out
    
    while(in>0 && // until smaller one found,
    temp.isBiggerThan(shapeList[in]))
    {
    shapeList[in] = shapeList[in-1]; // shift item to the right
    --in; // go left one position
    }
    shapeList[in]= temp; // insert marked item
    } // end for
    } // end Sort()
    
    }
    Last edited by jacline; 03-07-2009 at 09:58 PM. Reason: missing information

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    what line of code causes the error and what error is specifically thrown? Please post the error code itself. Also, when posting code, you would benefit by using code tags in order to make your code retain its formatting and thus be readable. You can do this by placing the tag [cod&#101;] above your code block and [/cod&#101;] below it. Again this will improve the readability of your code and should mean more folks will actually take the time to read it. Best of luck.

  3. #3
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default

    thanks for your helping.. it gives run-time error so i couldnt understand the exact line.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Is it possible for you to post here the text of the error message?

  5. #5
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default

    it gives this error:

    Exception in thread "main" java.lang.NullPointerException
    at Shape.isBiggerThan(Shape.java:8)
    at ShapeSet.sort(ShapeSet.java:45)
    at Lab03.main(Lab03.java:33)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    so it does give line numbers for you to use. Look at the Shape class, line 8, the ShapeSet class, line 45, ...

    You are somehow using an object that has not been constructed yet, and which object will be one of the ones on the lines above (look at the first line first of all).

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I think I see your problem. Your Shape class has a Shape field, "a" that is probably null and probably shouldn't even be there. Instead compare the "other" shape's area size with "this"'s area's size.

    Java Code:
    public abstract class Shape implements SizeComparable {
    // Shape a; // get rid of
    
      public abstract double getArea();
    
      public boolean isBiggerThan(Shape other){
        if(other.getArea() > getArea()) // use "this's" getArea, not a's
           return true;
        else
           return false;	
      }
    }
    Last edited by Fubarable; 03-07-2009 at 11:56 PM.

  8. #8
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default

    thanks for your helping...:D

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Sorting 10 numbers
    By perito in forum New To Java
    Replies: 56
    Last Post: 03-05-2009, 02:52 AM
  2. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 11:30 AM
  3. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 03:15 PM
  4. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 09:13 AM
  5. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 07:29 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
  •