• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question on collections and specifically using the comparable interface

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently going through an exercise on collections. I need some help answering because I've studied how the Comparable and Comparator interfaces work, but am struggling to see what I need to do. The question is as following:

Given below is a class for street addresses in a town that is meant to allow sorting.




The constructor should create a map and add all the given street addresses along
with the corresponding names of residents. The print method should print the
street addresses and resident names of all the people in the mailing list in the
ascending order of street address.
 
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Were you given that code exactly as you wrote it? That is pretty dreadful style; the lack of indentation will mean you can easily get lost, and the lack of {} in lines 11‑13 can cause more confusion. The old Sun Style Guide will tell you there is a better way to write that sort of code anyway. And public fields (lines 2‑3) is poor design.
Anyway, you have code which will work, however unreliably, and you know all about Comparable<T> and Comparator<T>? Remind yourself about object ordering in the Java™ Tutorials. If you click the “previous” link, you will find it tells you about Maps. That shou‍ld tell you all you need to know about adding things to Maps. Remind yourself about how a SortedMap works, and you are going to find what I sometimes say, that these things are actually easier to implement than they look at first sight.
I presume the details of the Map mean that the “K”s represent addresses and the “V”s the names of the occupants.
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaiser Nahom wrote:. . . . . .

Are you really given parallel Lists? That is another bit of poor design.
 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob. I've attached an image of the problem just so it's easier on the eye, but yes lines 11 to 13 really do miss out any swirly brackets.
I have looked at most of the stuff in the collections interface, sortedmap included as well as Comparator and Comparable. I think I understand them, that comparable is used to define a way of naturally ordering a set, whilst the comparator allows us to implement natural ordering for the basic classes (string and int and that) but I learned about it in a slightly different context and using a different print method.

I went through a video tutorial on comparable and natural ordering here https://www.youtube.com/watch?v=OlMw3ecL1ow

this is the code from the video, with my own commenting (feel free to point out any misunderstandings)

comparable-question.PNG
[Thumbnail for comparable-question.PNG]
the question in its original form
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaiser Nahom wrote:Thanks Rob. . . .

Who's Rob?

I went through a video tutorial on comparable and natural ordering here . . .

Quite a good tutorial, but there is one bad bit of style and two potential errors in the code.
How far have you got with your assignment, which is probably very easy to complete?
 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahem, sorry, my bad Campbell.
I understand from the description of the exercise what needs to be done, however I'm still unsure of what actually needs to be done.
It states that:

The constructor should create a map and add all the given street addresses along
with the corresponding names of residents. The print method should print the
street addresses and resident names of all the people in the mailing list in the
ascending order of street address.


However I don't know what this would look like.
Then in the print method, I'm confused as to why there is nothing being passed into the method. So much confusion.
 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*unsure of how to exactly implement a constructor in this setting, and use this sort of method to print
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing at a time. Start by creating a constructor at all. Then we can add more to it later.
 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects of the MailingList class are gonna be instantiated with addresses of the class Address(that has been already made) and residents of the class String.
If I had to say then I would think it would be
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That syntax looks as if you had been guessing. Don't guess; the compiler won't like it, for a start.
How would you usually create a new object and assign it to a field? Hint: I would call the left half of that statement correct, providing the map field has already been declared.
 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We've already specified what type of object we want to pass into the map, <Address>, and a map consists of a key and value pair, which is why I put the addresses as the key, and the residents as the value. But we've only specified that they have they are of the type Object and would need to clarify that they come in an ArrayList. So I would think it would be




but if not then it would be this (which I don't think it is, since Address is the type of object being passed in)

 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wait, we have to apply the same logic to the left hand side then too?

 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

All that new code (line 5 now) is wrong. It won't compile. You are still guessing. At the moment it is what is to the right of the = sign that is wrong.
Start by formatting every line of your code correctly so you can actually read it. If I have difficulty with it, you having less experience, will have even more difficulty reading what you have written. Go back to the original code from your first post. Change it bit by bit. Go back to your original lecture notes about how to create an object. Remind yourself about Maps.
 
Kaiser Nahom
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, I'm not sure if I mentioned, but this was from a written question in a previous paper, so I cant't change any code..



could you please send me what the code should look like? I can then go through the code myself? It's how I learn best, because I prefer going through established examples, if I have any difficulties then I will ask.
 
Campbell Ritchie
Marshal
Posts: 80623
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that from reading the screenshot you posted. You must use the code provided, even though some of it is not very good, but you are not filling in the gaps correctly.
You appear to have forgotten what you were taught about how to create an object. You have already declared the map. Please go to the SortedMap documentation and remind yourself what things there might be in an implementing class that allow you to create an object.

We don't hand out complete solutions.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic