I am trying to print letters in asterik format. Below is my code for printing letter 'A'. Is there any better of way of doing it? Suggest me if there is any alternative way of doing. Because I would like to do it for all the letters. Thanks in advance for any replies.
Java Learn wrote: Is there any better of way of doing it?
Ask yourself this...
If you came back to this code in a week, could you tell just by looking at it for 10 seconds what it does? If the answer is "No", then there is a better way. If the answer is "yes" (and if you said it was yes I'd say you are lying), the answer STILL may be there is a better way.
What is wrong with nine print statements:
Note: I would put this in a method, called something like "printA"
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred has offered you a hard-coded solution, which one could say was a variation of a data-driven solution. It's very easy to understand and to modify. Yours is a procedural solution. If, for some reason, you wanted to adapt your characters at run-time, that might be a better approach ("better" being one of those words that get harder to be sure of the more you try to define them). I'd offer an intermediate scheme, where you could still use data to drive your output, and retain the easy-to-modify aspect, but have the data be read by something that uses it when you need it:
When run, this is the output:
There are a lot of things in this code I wouldn't do in a production setting, but this ought to give some idea how you can use a data-driven approach without hard coding.
Now, if you really want to make your head spin, think about what you'd have to do with the above to get the output to look like this:
In the other disciplines, we rise by standing on each others' shoulders. In computer science, we do it by standing on each others' toes.
Joined: Dec 14, 2012
Thank you Fred and Stevens for responding. I liked both your approaches. My next question would have been what Stevens asked me. To print the letters right next to each other. Taking the Fred's approach, I would write the print statements such that it prints ABC. I think I would write my code in below style. But I do not know any other way of doing it.
fred rosenberger wrote:I say that my was is the best for the problem given ("print letters in an asterisk format") and the way I set those competing priorities in my own mind.
My comment about Stevens' approach being more flexible wasn't intended as a criticism of your approach and yes, I would agree that your solution fulfilled the initial requirements perfectly.
Agreed, on all points made by both of you. Indeed, this discussion illustrates just how important it is to distinguish one measure of quality from another. fred's solution clearly scores very high by some perfectly valid metrics. So does mine. For that matter, so does Akira's original technique. When asking for a "better" way to do anything, one might want to define that term or, alternatively (and, somewhat as we've done here) ask those proposing different solutions to explain in what way(s) they are better.
I always enjoy this kind of give-and-take. It helps me break out of what might otherwise become rather doctrinaire thinking. As I try to learn more about OOP, I am learning that doctrine is not as useful as wisdom; they're rarely the same.
I agree with both of you. All are valid approaches. All solve the problem in different ways.
Terms like "Best" or "Most efficient" really don't have much place in a programming discussion - if they are the only qualifiers. "Best in terms of smallest memory footprint" or "Most efficient in terms of Big-O notation"...sure.
This is something that people at all levels, from beginners to seasoned veterans, need reminded of.