• 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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
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: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:
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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:

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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

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: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:
This should become I believe, because you need the method which compares two Song objects.
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:This should become


Absolutely. My copy/paste mistake.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
This is a member method so no need to use a getter.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:
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: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 10797
86
  • Number of slices to send:
    Optional 'thank-you' note:
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: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:
You're correct, I just looked it up:Which does just what you want it to do. No overflowing subtractions.
 
Campbell Ritchie
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

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: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:
Please search my posts for, “BigDecimal,” and you should find lots of useful information there.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
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
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |