Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bubblesort confusion

 
Gina Ruffolo-Daniel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 733
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gina Ruffolo-Daniel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gina Ruffolo-Daniel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much! Darn semicolons with get you all the time.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much! Darn semicolons with get you all the time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic