File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Bubblesort confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bubblesort confusion" Watch "Bubblesort confusion" New topic
Author

Bubblesort confusion

Gina Ruffolo-Daniel
Greenhorn

Joined: Oct 01, 2005
Posts: 19
I am having trouble getting my bubbleSorts to work. I want to create a sort by String and integer. Here is my code. Constructor Class:


SalonReport Class:


if anyone can straighten me out I would appreciate it.

Thank you,
Gina
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Can you describe your problem?

Does the program compile?
If not copy and paste the full text of the error messages here.

Does the program execute to end?
Same as above.

Is the output correct?
If not, copy output here and explain why it's wrong and what you'd like to change.
Gina Ruffolo-Daniel
Greenhorn

Joined: Oct 01, 2005
Posts: 19
The program does not compile if I try to bubbleSort the SortDescription.

The error states: "The method getServiceType() is undefined.

If I comment out this code the compile and completes execution.

I can't figure out how to print the results to the console. When I debug, it appears to be successful.
Kelly Loyd
Greenhorn

Joined: Oct 10, 2005
Posts: 28
Hi,

Which object do you want to refer to in the following statement?

if (array[b].compareTo(getServiceType()));

getServiceType is only valid in the context of an object.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think you need to back up a moment. From the code here, it is not clear what kind of comparison you are using. How do you plan on comparing two Service objects? What makes one Service object less than (or greater than or equal to) another Service object?

Also, the common way of implementing this in Java is by implementing the Comparable interface. From your code, I think you are heading in this direction, but you aren't quite there yet. Service needs to implement Comparable and have a method called compareTo() that takes an Object as a parameter. (Here I am assuming that you are not using the new generics features from Java 1.5. If you want to use these, your code will get a slight bit more complicated.) If you implement this method, I think you should be able to figure out how to change the line that is giving you the error message.

For future reference, it will help a lot if you indicate which line gives a compiler or runtime error so we don't have to try and guess where the error is coming from.

Layne


Java API Documentation
The Java Tutorial
Gina Ruffolo-Daniel
Greenhorn

Joined: Oct 01, 2005
Posts: 19
Good Morning, I have the compare between to String objects. My outout is not sorting correctly. Instead of (Cut, Manicure,etc) I am getting (Cut,Shampoo,etc..


I hope that I have provided enough information.

Thank you in advance,
Gina
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Take a close look at the end of this line:



Note the semi-colon at the end of the line. This semicolon actually counts as the entire if block! It is the same as writing:

Thus, your swaps always happen, resulting in no change to the order whatsoever. Remove the semicolon, and all will be fine.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Gina Ruffolo-Daniel
Greenhorn

Joined: Oct 01, 2005
Posts: 19
Thank you very much! Darn semicolons with get you all the time.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
It looks like you are getting closer. Hopefully Joel's suggestion will fix your problems. I just have one comment about your design: it seems like the Service class should implement the Comparable interface and have a compareTo() method. One reson for this is that you will then be able to change

to

Notice that it makes this line much shorter and easier to understand. Internally, the compareTo() method can use the service type as its key for comparing the two Service objects by calling the comparTo() method on the two String objects.

Alternatively, you might want to create a separate class that implements the Comparator interface. This allows multiple sorting orders on the same class. For example, you can have one Comparator that sorts by the service type and another by the price. This makes it so you don't need two separate sorting methods. You just need one that takes a Comparator object to define the sorting order.

Both methods also allow you to easily use multiple keys for sorting. For example, let's say two Service objects have the same service type String. What if you want to break the tie by using the price or minutes? With your current design, you will need a lot more code than the first if statement above. If Service has a compareTo() method, though, you will be able to add this logic there and the if statement in your bubble sort will stay the same.

Another advantage comes up if you decide you create another class that you want to be able to sort. As it is, you will have to rewrite your sorting algorithm to deal with this other class. However, if you Service and this new class both implement Comprable and you change your sorting method as described above, you won't need to change it for the new class.

I hope you will consider these options as it will greatly reduce the amount of code you have to write.

Cheers!

Layne
Gina Ruffolo-Daniel
Greenhorn

Joined: Oct 01, 2005
Posts: 19
Thank you very much! Darn semicolons with get you all the time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bubblesort confusion
 
Similar Threads
variable to hold the size of the array
Help with Arrays!
Cannot Find Symbol Variable Errors
sorting an array
CompareTo method