Results 1 to 9 of 9
- 03-07-2009, 08:49 PM #1
[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 08:58 PM. Reason: missing information
-
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 [code] above your code block and [/code] 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.
- 03-07-2009, 08:59 PM #3
thanks for your helping.. it gives run-time error so i couldnt understand the exact line.
-
Is it possible for you to post here the text of the error message?
- 03-07-2009, 09:21 PM #5
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)
-
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).
-
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 10:56 PM.
- 03-08-2009, 04:25 PM #8
thanks for your helping...:D
-
Similar Threads
-
Sorting 10 numbers
By perito in forum New To JavaReplies: 56Last Post: 03-05-2009, 01:52 AM -
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10-02-2008, 10:30 AM -
sorting problem...
By mark-mlt in forum New To JavaReplies: 4Last Post: 04-17-2008, 02:15 PM -
sorting problem
By mcal in forum New To JavaReplies: 1Last Post: 02-14-2008, 08:13 AM -
sorting JTable
By mansi_3001 in forum Advanced JavaReplies: 3Last Post: 08-10-2007, 06:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks