• 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

Is there any better way of solving this problem?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Akira Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My next question would have been what Stevens asked me. To print the letters right next to each other.


This is where Fred's approach is less flexible than Stevens' because with Steven's approach you can write method to output a line of letters without having to redefine the letter arrays. ie


 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See...this is the problem with asking for "a better way" or an "alternative" way.

"better" is a very subjective term. It all depends on what is the most important thing among readability, flexibility, memory use, execution speed, and about 20 other variables.

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.
 
Akira Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you posting the replies. Next time I will frame my question more accurately :-). But I really appreciate you all for replying very quickly. Thank you so much.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

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.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic