The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Making the Grade

Posted by Matt Gerrans on January 26, 2002 at 2:34 AM


> Matt, so what would my solution get as a grade. I've included a polished version below:
>


> public static void displayDiamond(char c, int numRow)
> {
> for (int i=1; i> {
> for (int j=1; j<(i+1); j++)
> {
> System.out.print(c);
> }
> System.out.print('\n');
> }
> for (int i=1; i<(numRow+1); i++)
> {
> System.out.print(c);
> }
> System.out.print('\n');
> for (int i=(numRow - 2); i>0; i=i-2)
> {
> for (int j=1; j<(i+1); j++)
> {
> System.out.print(c);
> }
> System.out.print('\n');
> }
> }
>

> Of course, you could also use a StringBuffer or a String.
> Hiran

Oh no, now I'm on the spot. Well, I'm afraid I have some bad news for you, Hiran: Your code doesn't produce the correct results (except in the case of one row). It is always left-justified. So, we'll have to give you an 'incomplete' for now...

On another front, while looking at your code, I noticed a problem in the display of the less-than symbol: it gets treated as HTML and the for-loops get all messed up. The problem can be solved by using &lt; to get the "<" symbol to show (even inside the pre blocks). However, now when you look in the message text area below, all is fine. So there is a dichotomy: using the HTML symbols in the code posting will give a nice display, but the code cannot be cut-and-pasted (from the text area) into your favorite editor and easily compiled (you'll have to change the HTML symbols first). Oh well.



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us