go to previous page   go to home page   go to next page

Answer:

No.

The compareTo() method involves more operations than the single < used in the integer version of the sort.


Example Run

Here is a run of the testing program:

C:\JavaSource>java BoxSort
Before:
length: 1.0,  height: 2.3,  depth: 2.7,  volume: 6.21
length: 1.0,  height: 4.9,  depth: 3.2,  volume: 15.680000000000001
length: 3.0,  height: 1.3,  depth: 2.7,  volume: 10.530000000000001
length: 3.0,  height: 0.1,  depth: 4.67,  volume: 1.4010000000000002
length: 1.3,  height: 1.3,  depth: 1.3,  volume: 2.1970000000000005
length: 4.0,  height: 2.3,  depth: 1.7,  volume: 15.639999999999999
length: 2.2,  height: 2.1,  depth: 1.67,  volume: 7.715400000000002
length: 2.3,  height: 7.3,  depth: 6.3,  volume: 105.77699999999999
length: 2.0,  height: 3.3,  depth: 5.3,  volume: 34.98

After:
length: 3.0,  height: 0.1,  depth: 4.67,  volume: 1.4010000000000002
length: 1.3,  height: 1.3,  depth: 1.3,  volume: 2.1970000000000005
length: 1.0,  height: 2.3,  depth: 2.7,  volume: 6.21
length: 2.2,  height: 2.1,  depth: 1.67,  volume: 7.715400000000002
length: 3.0,  height: 1.3,  depth: 2.7,  volume: 10.530000000000001
length: 4.0,  height: 2.3,  depth: 1.7,  volume: 15.639999999999999
length: 1.0,  height: 4.9,  depth: 3.2,  volume: 15.680000000000001
length: 2.0,  height: 3.3,  depth: 5.3,  volume: 34.98
length: 2.3,  height: 7.3,  depth: 6.3,  volume: 105.77699999999999

Floating point math has its usual problems. Using DecimalFormat in the toString() method would make the output prettier.


QUESTION 11:

We wrote a sort method for ints a sort method for Strings and a sort method for Boxs. If you were writing a program with many arrays of various types you might need to write a sorting method for each type. Is this convenient?


go to previous page   go to home page   go to next page