Java Puzzles

andypsion
andypsion used Ask the Experts™
on
I have the code for this diamond        
         *
        ***
       *****
      *******
     *********
      *******
       *****
        ***
         *
I am struggling to modify it to produce a hollow diamond
         *
        * *
       *   *
      *     *
     *       *
      *     *
       *   *
        * *
         *
and then use the above to produce one like this:-

         *
        * *
       *****
      *     *
     *********
      *     *
       *****
        * *
         *
  Here the diamond size is 5 but it could be any size.      
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
show us the code as to how you produced the solid diamond and we shall tell you what modifications need to be made

vemul
if u have the code to create the first figure, i'm sure that by struggling harder u can produce the next two.
good luck
-gkern
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
Sounds like a homework assignment I gave in a C++ class several years ago.

mlmcc
care to post your code here ?
Commented:
Obviously homework so no code will be given.

Look at the program to display the first diamond.  My guess is that is is done using a nested loop - the outer for the rows and the inner for the asterisks in each row.

It should be easier to modify the inner loop so it will print '*' only for the first and last positions and ' ' for the internal positions.  Thus you get the 2nd diamond.

Now you can combine the two solutions - use the 1st one for the even numbered rows (starting from row #0) and the 2nd one for the odd numbered rows.  Thus you get the 3rd diamond.

Now go code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial