• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Sharpen Your pencil Page 329 - Working with Comparable

 
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book shows this code:



The book refers to the methos documentation for CompareTo()


Returns:  
a negative integer if th object is less than the specified object;
a zero if the're equal;
a positive integer if the object is greater than the specified object.

I"m not writing down the entire class that holds the song tiles here.  
It is the SongV2 Class.  
It returns the method like this:



I'll type in more of the code if I need to post it in the question.  


The exercise is to implement the compareTo() method in a way that will sort() the Song objects by title.
Hint: if you're on the right track.  It should take less than three lines of code!

My thought is that I need to implement the Comparable interface.   Here goes:

public mySort implements Comparable<T> {
       return songs <  // I don't know what to compare it to.

Let me know if I need to show more of the code in the previous pages that lead up to this exercise.

After typing this post... I turned the page and I see the authors have a page explaining the answer. From a quick look it is nothing that I would have thought of.    

It is past my bed time.  I'll look at this again later.

Thanks,

Kevin

 
Saloon Keeper
Posts: 10834
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a number of ways to sort songs. On what field(s) do you want to sort them by?

Look up Comparable and see what method(s) you'll need to implement.

 
Marshal
Posts: 79520
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By implementing Comparable, they are saying that a Song is sorted by its title, and that is its natural ordering. Have a look at the datatype that title is and go through its documentation and see what you can find.
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

title is variable type String.  

The book took the reader though how to use the sort method belonging to the String.  

There was too much going on to understand the book.

I'd probably have to enter the code in the book,  draw pictures, and keep going back to Java API.    

The book then shows how to use lambdas on top of this.

I asked about this sorting here before.  I'll go back on the next pass of the book or when I need to do sorting.

After lambdas, the book is moving on to the Collection API.  I understood this topic a lot more on the first pass of the book.  

I'm going to add code behind the GUI objects with my tutor tomorrow.  I'll find out how to react to a button being pressed and how to get text out of a text field.  

My current QA contract is very different than coding and I have to learn a lot about the clients business.  I'm going to take one or two classes a week with my Java/Mandarin language tutor.  

I'll read a few pages in the book, code a little bit,  do some exercises in the book, ask questions here.  I need to go slowly and skip parts when I get frustrated and go back to them later.

Best,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 10834
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are jumping all over the place.

You don't have to do the sorting. You have to implement the Comparable interface. Have you looked to see what method(s) you'll need to implement?
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

I agree.  I'm jumping around too much with Java.  

I need to go back 30 pages in the book to the start of the Data Structures chapter.

The book threw too many concepts and topic at me.  Here are some of them:

sorting a list
diamond operator
sorting objects by different instance variables
static <T extend Comparable<? super T>> voide sort list <T> list)
generics
comparable
comparator
custom comparator
lambdas
getting rid of duplicates in a sort

I read the pages a couple of times and get some idea of each topic.  
I don't understand each topic because I have not tried using them.

I'll go to the start of the chapter and type the code into Intellij so I can see how it works.

It will take a while because I'm getting more busy on my QA project at work.

Thanks,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 10834
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You are jumping all over the place.

You don't have to do the sorting. You have to implement the Comparable interface. Have you looked to see what method(s) you'll need to implement?


Exactly my point.

We come here to this thread focused only on solving the original question you posted. You are focused on all the reasons you can't do it.
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We all agree.

I looked at my original post on this thread.  It has too many parts that I don't know.

I won't ask for assistance on this post... at least for now.

I'm starting the chapter from the beginning.

I got to know people on two other forums in the past 30 years.  They were Clipper and Automation testing groups.  

I met a few people on the Clipper board in a class.  I never met anyone in person from the Automation group.  After a few years on each forum I knew enough to assist others.

A lot of my success on projects was by getting advice on the boards.  I must have helped many other people also.

I felt as if I was friends with the strangers even though I never met them.  It is like that here as well.

Another forum that I like Is MrExcel.com

Thanks,

Kevin



 
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:My thought is that I need to implement the Comparable interface.   Here goes:



Those thoughts were quite close. However, you may mislead yourself with some naming. Where you wrote mySort, rename it to Song, so you get:

Research, what method you need to implement. You can figure that out by reading Java API documentation here <-- click it. Let us know what progress you have made.
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should become I believe, because you need the method which compares two Song objects.
 
Liutauras Vilda
Marshal
Posts: 8884
638
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:This should become


Absolutely. My copy/paste mistake.
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey, Paul, Liutauras,

I am starting the chapter from the beginning and making a new post with my first question.   I'm naming it "Static comment not clear".  

I'll  look at everyone's posts before re-asking questions.  I'll also make use of the API documentation.

Thanks,

Kevin

 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I kept trying to get the sorting on object code working.   My tutor kept looking at my attempts and correcting me for three classes.   Finally I got this code working and understand it.  I could not write it from scratch.  I'd need to use my notes and the book.   I kept reading the explanations and understood pieces but became overwhelmed.  I want to go back and read everyone's posts and expect they will make sense to me now.    Here is the code I finally got working.



Thank you everybody,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 10834
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a member method so no need to use a getter.
 
Campbell Ritchie
Marshal
Posts: 79520
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure I like the concept of songs implementing Comparable. If you look at Comparable's documentation, it says it imposes a total ordering on its implementing class. I think that means there is one way to order the objects. For example, Integer has one obvious ordering: small to large. [Large to small doesn't count as a different ordering; it is simply the first ordering backwards.] But songs may have several orderings, by title, by composer, by performer, etc. That makes me think it would be more appropriate to create Comparator<Song>s for the different features you would order them by. There is more about odering in the Java™ Totorials.
 
Campbell Ritchie
Marshal
Posts: 79520
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:...

Your comment in line 45 hides a potential error. It won't occur because the metronome markings are always small positive numbers. But if the two values differ a lot, as for two BankAccounts one containing $20,000,000 and the other overdrawn by $2,000,000 with the amounts denominated in cents as ints, they will sort in the wrong order. I shall leave finding the explanation as, “an exercise for the reader.”
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

I'd have to be careful about not using Int if the numbers get too large.

I'd have to look for a different type.   Float, long, Big number or something that holds a larger range.

Thanks,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 10834
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not the issue, it's the subtraction that's the problem. I think the Integer class has a method to compare two ints without using subtraction. (haven't bothered to look it up)
 
Paul Clapham
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're correct, I just looked it up:Which does just what you want it to do. No overflowing subtractions.
 
Campbell Ritchie
Marshal
Posts: 79520
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote: . . . I'd have to look for a different type.   Float, long, Big number . . .

I have my own opinion about when you should use the float datatype. It's very simple: never.
Unfortunately some methods and constructors force you to use floats.
If subtracting two doubles returns a number with an absolute value < 1.0, then it has to be cast to an int and will degenerate to zero, so that won't work.
The BigXXX classes are very useful, but this link on a BigInteger says it may return the wrong sign. A BigDecimal can suffer the same error as a double for a small difference.
I did say deonominated as an int because I knew that was a circumstance likely to produce such an error. ()

Thanks,

Kevin

That's a pleasure

Didn't I give you a Java™ Tutorials link a few days ago? I think you will find that link discusses some of the things going wrong with subtraction (or negation) and tells you how to correct those errors. Here's the link again.
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell and friends,

I have been away from Code Ranch for a couple of weeks.  I'm looking at the last posts on this thread.   I needed large decimal numbers a few years ago and used bigdecimal and it worked.  I looked at the links in your posts.  It is too much to understand right now in detail.  It is good to know that there are these methods for working with numbers that won't work with ordinary int or Integer.  Also other variable types.  I hope I can find this post when I need it later on.  I'm looking for the place to make this into a Code Ranch Book mark.  I have my book marks and watches, but iI forgot where the place is that I tell it to save as a book mark.

I'm working on testing moving mainframe code from 45 years ago to a new home.  It is amazing how we got to the moon in 1969 with what we had back then.  

I'll  check to see posts on occasion.  I might be done with my project in a couple of months, and I can get back to Java learning again.

Kevin

 
Campbell Ritchie
Marshal
Posts: 79520
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please search my posts for, “BigDecimal,” and you should find lots of useful information there.
 
kevin Abel
Ranch Foreman
Posts: 928
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

I searched in Code Ranch on BigDecimal and read about 30 posts.  I saw some from you.

If I need to work with currency, I'll use it.

I'm sure it has other uses.

I can't remember the purpose, but BigDecimal solved the situation about 10 years ago.

Best,

Kevin
 
reply
    Bookmark Topic Watch Topic
  • New Topic