This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!

Campbell Ritchie

+ Follow
since Oct 13, 2005
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

You didn't quite get the code tags in the right place: [code=java] goes before the code and [/code] after it. But you haven't indented much of your code in the first place.
This is what the code minus the worst of the long lines would have looked like with code tags in the right place:-I know you have been told to write comments to explain the code, but most of those comments don't say anything that isn't already obvious from reading the code. Methods shouldn't have // comments anyway; they should have proper documentation comments with /** ... */ .
Your indentation is incorrect. Lines 54‑55 are really difficult to read. Besides, I think they are in the wrong place. Look at this thread and my posts yesterday.
Don't use System.exit. Let the code come to the end of the method.
Don't stagger indentation. Lines 85‑90 are another bit hard to read. Actually, they can beneficially be shortened. It is potentially error‑prone to populate an array like that. Use an array initialiser:-That code guarantees that the size of the array and the number of elements are exactly the same and there is no risk of gettin gthe index numbers wrong. The [] belong before the name of the array. That class should be called Item not Items.
Why are you using option panes? I can foresee problems on line 57.

What is the problem? What did you mean about losing items, and why does the sorting do nothing? Can you remember what the algorithm for bubble sort is?
19 minutes ago
Why shouldn't a book know whether its author is getting royalties? It might “know” about that method and never use it. Some books might be paid for by an advance full stop and some might also earn royalties.
Which all goes to show that sometimes there ain't no such thing as a simple yes or no.
36 minutes ago

Carey Brown wrote:In general, spacing doesn't matter in Java, . . .

Maybe you should think that you need the spaces and indentation for your own purposes, but the compiler can usually cope without spacing.

As you have been told, what you have in the quoted text is a word without a preceding space. If you want the space displayed, you have to write a word with a preceding space.

And welcome to The Ranch.


Minor point: you may need spacing if you have two operators adjacent to each other, or a type adjacent to an identifier. You need to writeDon't read what follows
What happens if you combine the sign change operator and predecrement? Can the compiler cope with −−−i? That should expand to − −−i
53 minutes ago
It is usually a bad idea to quote the whole of an old post; that simply makes the thread longer without adding any information. I have removed most of the quoted text.
1 hour ago

Piet Souris wrote:This is an assignment, concerning possibly partly filled arrays. This is not common . . .

That means there are going to be nulls in the array somewhere, and that is impossible to sort using conventional methods as taught atColleges.

putting it in a utility class seems not obvious. But why isn't the variable 'nbrAuthors' a parameter in this case?

You can still use utility methods to sort arrays containing nulls. You can't use Comparable obects, however, because you cannot compare null references like that. You can however create a Comparator<Author> that copes well with nulls. I can't remember whether the tutorials link I gave tells you anything about nulls, nor whether the static methods in Comparable<T> that cope with nulls are permissible, but you can write a Comparator like this:-I am presuming that OP doesn't know how to turn that anonymous class into a λ. If both  arguments are null, that Comparator will cause a redundant swap of the two nulls. You can sort that out by inserting a1 == a2 ? 0 : after return.
1 hour ago
I am not convinced you have found the best solution. I think those methods should all be static because they need no information other than their parameters, and they emit no information other than their returned values.
I think yoiu should have a utility class with a method that swaps two elements in an array.I also think yoiu should have a bubbleSort method in the same utility class:-Use the sort method to do the sorting, and pass on swapping to the swap method.

Read about <T> here, and about Comparable and Comparator here, both in the Java™ Tutorials. There are several ways you can create a Comparator<Author> including alphabetical order of name. I think the tutorials link will explain everything you need to know.
12 hours ago

Emily Rosemond wrote: . . . The Author class represents what you get from reading the cover of a book, nothing more, nothing less. . . . .

I am not convinced; the book cover tends to tells us at least about the author's other books.

What happens if one author writes different types of books, for example, a science-fiction writer might also write autobiographies of celebrities. If Author were abstract and had a ScienceFictionAuthor subclass . . .

Do you think of people as authors of a particular genre? Dorthy L Sayers wrote mostly detective stories, but she also wrote translated Dante's Divine Comedy.
C S Lewis wrote mostly theology, but also wrote 3½ fantasy/SF books. (the Ransom trilogy and Screwtape, which I will put down as half a book because it is hal fantasy and half theology). Or you can include Narnia and make that 10½.
I am not convinced that there is such a person as an SF author, or a biographical author. Maybe the genre belongs to the book rather than the author.

You might think I'm stubborn, . . . trying to justify what I'm doing

Maybe. Or maybe that you have hit an intersting question which will come up in the JavaRanch Journal and earn you a cow.

but I just don't see why or how you would abstract Author when it's a simple class. . .

You don't make an abstraction from a class. A class IS‑AN abstraction of a real‑life object already. Your class encapsulates a name, and it doesn't say what that name is. That makes it an abstraction.

. . . Different Author types don't need to exist as subclasses or implementations.

That sounds the same as I said a few lines back. But without some sort of information about books, or even genres, I am finding it hard to distinguish yoiur Author class from a simple Name class.
12 hours ago
I think I have found all the posts, but I am not sure they are still in their original order.

12 hours ago
I merged your stuff with the following thread. I hope that is okay by you.
12 hours ago
Well, I made a right mess of the merging because I managed to put the wrong number for the old thread. I might even have lost one of your posts by mistake. I shall try to find it.

12 hours ago
Emily Rosemond,
Your post was moved to a new topic.
(This informational message will self destruct in two days)
12 hours ago
Emily Rosemond,
I have merged your topic into this topic. I hope that helps.
12 hours ago
I merged your stuff with the following thread. I hope that is okay by you.
12 hours ago