• Post Reply Bookmark Topic Watch Topic
  • New Topic

CompareTo and ArrayLists  RSS feed

 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, everyone! I have a new practice problem I'm working on- I get the general sense of it, but can't seem to tweak it to meet the requirements! Here's the problem:

Define a class RadioStation that can be used to store information about radio stations. For each station, keep track of its name (a string), its broadcast band (a string) and its station number (a real number). For example, there is a local station called KUOW that is an FM station broadcast on 94.9. The class must have the following public methods:

RadioStation(name, band, number) constructs a radio station with given name, band and station number

getName() returns the name

getBand() returns the band

getNumber() returns the number

toString() returns a String representation of the radio station

simulcast(other) records the fact that this station and the other are simulcasts

The broadcast band can be any arbitrary string. For example, it might be "AM" versus "FM" or might include subdivisions like "FM 1" and "FM 2" or might include other text like "XM" for satellite radio. The toString method should return a String composed of the station name followed by the band followed by the station number. For example, if you were to construct the following station:

RadioStation station = new RadioStation("KKNW", "AM", 1150.0);

then a call on station.toString() should return "KKNW AM 1150.0".

The simulcast method is used to record a relationship between two stations that are broadcasting the same material, as in:

RadioStation station1 = new RadioStation("KUOW", "FM", 94.9);
RadioStation station2 = new RadioStation("KUOW", "AM", 1340);
station1.simulcast(station2);

The call on simulcast indicates a link between the two stations. The link goes in both directions, so it shouldn't matter whether you make the call above or if you instead make the following call:
station2.simulcast(station1);

A given station will have at most one simulcast relationship. When such a relationship exists, it should be included in the result of toString. Given the calls above, the call station1.toString() should return:

KUOW FM 94.9 (simulcast on AM 1340.0)

and the call station2.toString() should return:

KUOW AM 1340.0 (simulcast on FM 94.9)

Notice that the simulcast notation appears in parentheses and that we include just the band and station number. You are to exactly reproduce this format. Your class should implement the Comparable<E> interface. Radio stations should be grouped together by band (e.g., AM stations grouped together and FM stations grouped together). Within a given band, the stations should be sorted by station number (e.g., FM 94.9 less than FM 96.5).

I'm struggling with the simulcast method (mine basically returns a string) and grouping the stations first by band, then by number (rather than just number, as I have). Any help, hints, or comments would be appreciated!

Here is my class:


And here is the test class:
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should write down on paper very carefully what the simulcast method is supposed to do. Write down examples of its behaviour.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:I'm struggling with the simulcast method (mine basically returns a string) and grouping the stations first by band, then by number (rather than just number, as I have). Any help, hints, or comments would be appreciated!

Well, firstly: Congratulations. A very clearly stated and detailed question that shows you have plainly put some effort into this; I wish more posters asked questions like you.

For your first issue - simulcast - I'd simply add a 'simulcast' attribute that is a RadioSatation. If it's null, there's no simulcast in place.

Your instructions say: "simulcast(other) records the fact that this station and the other are simulcasts", but your method:
public String simulcast(RadioStation other) { ...
looks much more like a helper method for your toString() method to me.
So:
(a) I'd make it private.
(b) I'd name it something different (simulcastString()?) to avoid confusion.

Also: Don't forget your instructions say that simulcasts need to be recorded in both directions.
How do you think you might do that assuming your method is declared as
public void simulcast(RadioStation other) { ...
?
Hint: it involves using 'other', which is completely visible inside the method, because this and other are the same class. If you get really stuck on it, I'll help you out.

Another thing to think about: What if you try to simulcast with a station that is already being simulcast? Your instructions aren't explicit about it, but I'd say that it's probably an error.


As for your compareTo() issue, let me give you an example:
Suppose I have a Version class that contains two integers - major and minor - and I want to produce Versions in major.minor order. My compareTo() method would probably look something like this:Note: did you notice that I put the '>' and '<' checks first? It's only a minor efficiency thing, but probably worth remembering - eliminate the most likely possibilities first (and, in your case, '==' is the least likely).

Hope that helps.

Winston
 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for all the help. I've figured out the simulcast method- here's what I have, and it outputs correctly and in the manner the directions specify (I'm storing away the information about private, etc, for future use- right now I'm trying to do it as simply as possible and as close to the directions as I can)-



The compareTo method is still giving me trouble, though. I seem to get the gist of it, but for some reason the AM and FM aren't grouping correctly- the first station always stays in the same place. What can I do to correct this? Here's my compareTo code:



Many thanks again!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:Thanks so much for all the help. I've figured out the simulcast method- here's what I have...I'm trying to do it as simply as possible and as close to the directions as I can...

OK, well maybe you and I read those directions differently, because they specifically say:

"The simulcast method is used to record a relationship between two stations that are broadcasting the same material, as in:
RadioStation station1 = new RadioStation("KUOW", "FM", 94.9);
RadioStation station2 = new RadioStation("KUOW", "AM", 1340);
station1.simulcast(station2);

The call on simulcast indicates a link between the two stations. The link goes in both directions, so it shouldn't matter whether you make the call above or if you instead make the following call:
station2.simulcast(station1);"

They also go on to say:

"When such a relationship exists, it should be included in the result of toString. Given the calls above, the call station1.toString() should return:
KUOW FM 94.9 (simulcast on AM 1340.0)
and the call station2.toString() should return:
KUOW AM 1340.0 (simulcast on FM 94.9)"

What your simulcast() method does is simply print out what toString() is supposed to; it doesn't establish any link between the stations - and if you're going to do that, you may as well define it as you did the first time (ie, have it return a String). However, that's not how I read the directions.

Have another look at my suggestions above and see if they make any more sense now.

Also, your instructions say:
"A given station will have at most one simulcast relationship."
so, as I said before, I think you need to work out what you want to do if the method is called on a station that already has a relationship.

The compareTo method is still giving me trouble, though. I seem to get the gist of it, but for some reason the AM and FM aren't grouping correctly- the first station always stays in the same place. What can I do to correct this?

Simply put: AvoidTheEqualityOperator (←click). What type is a 'band'?

You wouldn't expect to be able to say:
if (band > other.band)
would you?
So what makes you think that:
if (band != other.band)
is going to work (or at least work the way you want it to)?

Also: I don't think your comparison logic is right. How will that outer '} else {' ever get executed? Comparisons are always concerned with greater, less and equal; not just equal and not equal.

My suggestion: write out in English what you want it to do, and then translate it to Java.

Winston
 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... thanks for your patience with me. I'm VERY much a beginner at all of this stuff! I think I finally understand what you're saying- and here is my revised code. It seems to be working the way it should (though "seems" is a flexible word).

The class:


The test class:


Thanks for any additional pointers and tidbits for the future!

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a comment: I was scrolling through your code and noticed "* 100" in your compareTo() method. What's that about, I thought. Multiplying by 100 shouldn't affect the sign of the result, and the sign is all you need. But then I scrolled up to the top of the class and found that "number" was a double and not an int. Well, okay. That might explain something, but why is it a double? So then I went to see why it was a double, and I found it had values like 93.7. Okay, that does explain it then. And it also explains why 100 and not some other number. So that's fine.

But why is that variable called "number"? If had been called something else, like maybe "frequency", I think I would have clued in a bit faster. And the easier it is for somebody else to understand your code, the better it is. Is "frequency" the right term for the number you're using there, by the way?

This is obviously a minor quibble, but I thought it was worth mentioning. Of course the choice of a variable name has no effect on how the class works. You could call the variable "xyzzy" instead of "number" and it would work just the same. It would just be harder for somebody else to understand. So choose your variable and method names with care.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:...here is my revised code. It seems to be working the way it should (though "seems" is a flexible word).

And that's where good testing comes in.
As Paul said, good naming is also very important. A well-written program can almost leap off the page at you, saying "yes, I'm going to work", because everything makes sense - although that's not a reason to avoid proper testing.

However, congratulations again: a very nicely written program. You've obviously learnt how to format well, and keep to Java spelling conventions; and - most important - it does what you were asked to do.....Almost.

Remember what I said about the "one relationship" business? Have a think what will happen if you run:
station1.simulcast(station2);
followed by:
station1.simulcast(station3);
What will happen when you then run:
System.out.println(station1);
System.out.println(station2);
and how do you think you might solve it? (Hint: there's more than one way).

Also: what if someone calls:
station1.simulcast(station1);
? (Again: more than one way to solve this one)

A few other minor points:
1. You might want to have a look at Javadoc comments; they're very useful - and they're also what creates all that nice API documentation you see on the Oracle website.

2. I reckon you can improve on 'other'. As a parameter name, it's fine, but as an attribute name, it's a bit vague.

3. You can also write private methods, and they can often help to improve the readability and logic of your classes.
For example, suppose you added a:method. Then your toString() method would be:which reads a lot better.
And if you'd kept the method you originally wrote and simply renamed it, viz:then your toString() method becomes simply:Do you see how that works? Using methods doesn't just help to break down logic "externally", but also inside your classes as well.

HIH. And again, well done; I predict good things if you decide to stay at this programming lark..

Winston

PS: Are you from Seattle? I remember all those stations from when I lived in Vancouver.
 
Laura Peterson
Greenhorn
Posts: 23
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help, encouragement, and replies. I'll work on the one relationship part in my spare time. I'm currently taking a C# class, but my instructor recommended a thorough knowledge of Java, so I'm learning as much as I can myself. Javadoc seems very intimidating, I'm not sure I'm ready for that yet- but I'll try!

Just a note- I used "number" because it was the term used in the instructions. As I stated before, I'm trying to stay as close to them as possible- but I will remember the importance of good names for the future.

And last... yes, I am from Seattle.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Peterson wrote:I'm currently taking a C# class, but my instructor recommended a thorough knowledge of Java, so I'm learning as much as I can myself.

I'd say s/he's absolutely right. Quite apart from the basic premise that all knowledge is good, both languages cover much the same ground and have a similar syntax, so (to some extent) you can "get inside the designer's head" and work out why they chose different ways of doing things.
C# is newer, and so has learnt from Java in some areas, such as being able to explicitly declare properties; although in others (such as its implementation of final methods), I think it's inferior - although I'm quite sure I'll be flamed for that opinion.

It's also worth noting that Java holds a significant (and fairly steady) advantage in market share over C#, so when it comes to job-hunting...

Javadoc seems very intimidating, I'm not sure I'm ready for that yet- but I'll try!

Yeah, I understand; it seemed that way to me too. My advice: get familiar with the API docs, because you'll soon work out that they all have a similar structure, which basically boils down to about half a dozen frequently used tags. Believe me, as someone who started programming back in 1976, it's a dream that I had to wait 25 years for.

Thank you very much for your help, encouragement, and replies.

You're welcome. Like I say, it's a pleasure to see a beginner ShowSomeEffort and write nice, clear code.

Winston
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware [tangent]
Winston Gutkowski wrote: . . . market share . . .
It is interesting to see some very old languages eg LISP, COBOL, Fortran, still up there. So is FORTH, which I am using.

I ought to look at that index more often.[/tangent]
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is interesting to see some very old languages eg LISP, COBOL, Fortran, still up there. So is FORTH, which I am using.

Agreed, and as a frustrated Fortraner myself, it's interesting to see that it's still supported by LLVM.

At the same time, I think I've got over that; and I'm more interested in the new generation of OO+ languages. Still haven't quite got my head around Objective-C yet, mainly due to its "unfriendly" syntax; but it would appear that many others disagree. I was also very interested to see the jump in bash, which (along with its close relative, the Korn shell) is one of the few languages (?) I think I can reasonably claim to be an "expert" on.

Winston
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . Objective-C yet, mainly due to its "unfriendly" syntax; but it would appear that many others disagree. . . .
TIOBE record how much discussion there is about the language on the Net, rather than how much it is used. Maybe the rush in Objective‑C is caused by millions of people cursing its syntax online.

I have never tried it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!