File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is there any better way of solving this problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there any better way of solving this problem?" Watch "Is there any better way of solving this problem?" New topic
Author

Is there any better way of solving this problem?

Akira Reddy
Greenhorn

Joined: Dec 14, 2012
Posts: 6
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.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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

Joined: Dec 14, 2012
Posts: 6
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.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2161
    
  47
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
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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

Joined: Dec 14, 2012
Posts: 6
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

Joined: Aug 07, 2007
Posts: 2161
    
  47
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
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

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
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

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.
 
Don't get me started about those stupid light bulbs.
 
subject: Is there any better way of solving this problem?
 
Similar Threads
loops
For loop Containing another For loop
Java Code
charAt used on arraylist
null pointer exception in Game Of Life