Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25223 > unrolled thread

Creating a diamond shape wtih java

Started byjohnnyringo888@gmail.com
First post2013-09-04 17:15 -0700
Last post2013-09-08 09:00 -0700
Articles 8 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Creating a diamond shape wtih java johnnyringo888@gmail.com - 2013-09-04 17:15 -0700
    Re: Creating a diamond shape wtih java Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-04 20:33 -0400
      Re: Creating a diamond shape wtih java Roedy Green <see_website@mindprod.com.invalid> - 2013-09-07 14:10 -0700
    Re: Creating a diamond shape wtih java John Stephens <johnnyringo888@gmail.com> - 2013-09-04 17:46 -0700
    Re: Creating a diamond shape wtih java Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-09-05 00:20 -0300
      Re: Creating a diamond shape wtih java Jeff Higgins <jeff@invalid.invalid> - 2013-09-05 03:32 -0400
        Re: Creating a diamond shape wtih java Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-09-05 09:44 -0300
    Re: Creating a diamond shape wtih java John Stephens <johnnyringo888@gmail.com> - 2013-09-08 09:00 -0700

#25223 — Creating a diamond shape wtih java

Fromjohnnyringo888@gmail.com
Date2013-09-04 17:15 -0700
SubjectCreating a diamond shape wtih java
Message-ID<ccc6b9c6-617f-4a17-b14c-9dab366d9025@googlegroups.com>
I have to create a diamond shape for my java class.

     *
    ***
   *****
  *******
 *********
  *******
   *****
    ***
     *

Here is the code I have written so far. Can any provide assistance?



public class Diamond
{
	public static void main (String[] args)
	{
	 for ( int i = 1; i <= 10; i++ )
         {
            for ( int j = 0; j < i; j++ )
            {
	       System.out.print ("*");
            }
            {  
               System.out.println ("\n");
            }   
         }
	
	 for ( int i = 10; i > 0; i-- )
	 {
	    for ( int j = 0; j < i; j++ )
	    {
	       System.out.print ("*");
            }
            {  
               System.out.println ("\n");
            }   
         }

	 for ( int i = 10; i > 0; i-- )
         {
            for ( int j = 0; j < (10 - i); j++ )
				{
	        System.out.print (" ");
            }            }
            for ( int j = 0; j < i; j++ )	
		      {
               System.out.print ("*");
            }
            {  
               System.out.println ("\n");
            }   
 	      }
      
      for ( int i = 1; i <= 10; i++ )
         {
            for ( int j = 0; j < (10 - i); j++ );
            { 
               System.out.print (" ");
            }      
	    for ( int j = 0; j < i; j++ );
	    {
	       System.out.print ("*");
  	    }
            {  
               System.out.println ("\n");
            }
         }   
   }
}

When I compile this is what I get:

*

**

***

****

*****

******

*******

********

*********

**********

**********

*********

********

*******

******

*****

****

***

**

*

**********

 *********

  ********

   *******

    ******

     *****

      ****

       ***

        **

         *

         *

        **

       ***

      ****

     *****

    ******

   *******

  ********

 *********

**********

[toc] | [next] | [standalone]


#25224

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2013-09-04 20:33 -0400
Message-ID<l08jhl$qh$1@dont-email.me>
In reply to#25223
On 9/4/2013 8:15 PM, johnnyringo888@gmail.com wrote:
> I have to create a diamond shape for my java class.
>
>       *
>      ***
>     *****
>    *******
>   *********
>    *******
>     *****
>      ***
>       *
>
> Here is the code I have written so far. Can any provide assistance?

     Since it's homework (you're honest about that; good!) I
think a hint may be of more value than a solution.  Here's the
hint: Imagine that you were assigned to print

	oooo*oooo
	ooo***ooo
	oo*****oo
	o*******o
	*********
	o*******o
	oo*****oo
	ooo***ooo
	oooo*oooo

instead.  How would you go about it?  If you solve this modified
problem, then replacing each "o" with a " " solves the original.
For extra credit (we call it "optimization"), try

	oooo*
	ooo***
	oo*****
	o*******
	*********
	o*******
	oo*****
	ooo***
	oooo*

and see how you make out.

     One further hint:

	System.out.println ("\n");

prints the "\n" string, which usually ends the current line and
begins a new one.  Next, it ends the current line and begins a
new one.  How many new-and-begin operations is that?

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

[toc] | [prev] | [next] | [standalone]


#25241

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-09-07 14:10 -0700
Message-ID<8d5n299nv4jeqgtfcthb83erduav5bugre@4ax.com>
In reply to#25224
On Wed, 04 Sep 2013 20:33:52 -0400, Eric Sosman
<esosman@comcast-dot-net.invalid> wrote, quoted or indirectly quoted
someone who said :

>Here's the
>hint: Imagine that you were assigned to print

This is a very good answer. Non-computer people tend to think of space
as "nothing".  To computer people, space is just another letter of the
alphabet.  Imagine it were drawn with a few pixels at the bottom.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Truth is much too complicated to allow anything but approximations. 
 ~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)

[toc] | [prev] | [next] | [standalone]


#25225

FromJohn Stephens <johnnyringo888@gmail.com>
Date2013-09-04 17:46 -0700
Message-ID<1b5006d3-094a-4b38-990d-cfa1ebaf7456@googlegroups.com>
In reply to#25223
On Wednesday, September 4, 2013 8:15:53 PM UTC-4, John Stephens wrote:
> I have to create a diamond shape for my java class.
> 
> 
> 
>      *
> 
>     ***
> 
>    *****
> 
>   *******
> 
>  *********
> 
>   *******
> 
>    *****
> 
>     ***
> 
>      *
> 
> 
> 
> Here is the code I have written so far. Can any provide assistance?
> 
> 
> 
> 
> 
> 
> 
> public class Diamond
> 
> {
> 
> 	public static void main (String[] args)
> 
> 	{
> 
> 	 for ( int i = 1; i <= 10; i++ )
> 
>          {
> 
>             for ( int j = 0; j < i; j++ )
> 
>             {
> 
> 	       System.out.print ("*");
> 
>             }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }   
> 
>          }
> 
> 	
> 
> 	 for ( int i = 10; i > 0; i-- )
> 
> 	 {
> 
> 	    for ( int j = 0; j < i; j++ )
> 
> 	    {
> 
> 	       System.out.print ("*");
> 
>             }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }   
> 
>          }
> 
> 
> 
> 	 for ( int i = 10; i > 0; i-- )
> 
>          {
> 
>             for ( int j = 0; j < (10 - i); j++ )
> 
> 				{
> 
> 	        System.out.print (" ");
> 
>             }            }
> 
>             for ( int j = 0; j < i; j++ )	
> 
> 		      {
> 
>                System.out.print ("*");
> 
>             }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }   
> 
>  	      }
> 
>       
> 
>       for ( int i = 1; i <= 10; i++ )
> 
>          {
> 
>             for ( int j = 0; j < (10 - i); j++ );
> 
>             { 
> 
>                System.out.print (" ");
> 
>             }      
> 
> 	    for ( int j = 0; j < i; j++ );
> 
> 	    {
> 
> 	       System.out.print ("*");
> 
>   	    }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }
> 
>          }   
> 
>    }
> 
> }
> 
> 
> 
> When I compile this is what I get:
> 
> 
> 
> *
> 
> 
> 
> **
> 
> 
> 
> ***
> 
> 
> 
> ****
> 
> 
> 
> *****
> 
> 
> 
> ******
> 
> 
> 
> *******
> 
> 
> 
> ********
> 
> 
> 
> *********
> 
> 
> 
> **********
> 
> 
> 
> **********
> 
> 
> 
> *********
> 
> 
> 
> ********
> 
> 
> 
> *******
> 
> 
> 
> ******
> 
> 
> 
> *****
> 
> 
> 
> ****
> 
> 
> 
> ***
> 
> 
> 
> **
> 
> 
> 
> *
> 
> 
> 
> **********
> 
> 
> 
>  *********
> 
> 
> 
>   ********
> 
> 
> 
>    *******
> 
> 
> 
>     ******
> 
> 
> 
>      *****
> 
> 
> 
>       ****
> 
> 
> 
>        ***
> 
> 
> 
>         **
> 
> 
> 
>          *
> 
> 
> 
>          *
> 
> 
> 
>         **
> 
> 
> 
>        ***
> 
> 
> 
>       ****
> 
> 
> 
>      *****
> 
> 
> 
>     ******
> 
> 
> 
>    *******
> 
> 
> 
>   ********
> 
> 
> 
>  *********
> 
> 
> 
> **********

Thank you Eric! I will try this method and see what happens.

[toc] | [prev] | [next] | [standalone]


#25227

FromArved Sandstrom <asandstrom2@eastlink.ca>
Date2013-09-05 00:20 -0300
Message-ID<BLSVt.125549$ob7.52360@fx03.iad>
In reply to#25223
On 09/04/2013 09:15 PM, johnnyringo888@gmail.com wrote:
> I have to create a diamond shape for my java class.
>
>       *
>      ***
>     *****
>    *******
>   *********
>    *******
>     *****
>      ***
>       *
>
> Here is the code I have written so far. Can any provide assistance?
>
[SNIP]

I agree with Eric, kudos for acknowledging that this is for a class.

Here's another hint: the solution is always in your head, not in code. 
Code is just an implementation of a solution.

Pseudocode is often a good first approach. Pen (or pencil) and paper - 
describe how you'd solve that problem. You can even execute and debug 
pseudocode. :-)

9 times out of 10, we make mistakes because we are hasty. We are not 
Donald Knuthian. We leap into coding.

AHS
-- 
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift

[toc] | [prev] | [next] | [standalone]


#25228

FromJeff Higgins <jeff@invalid.invalid>
Date2013-09-05 03:32 -0400
Message-ID<l09buc$1sl$1@dont-email.me>
In reply to#25227
On 09/04/2013 11:20 PM, Arved Sandstrom wrote:
> On 09/04/2013 09:15 PM, johnnyringo888@gmail.com wrote:
>> I have to create a diamond shape for my java class.
>>
>>       *
>>      ***
>>     *****
>>    *******
>>   *********
>>    *******
>>     *****
>>      ***
>>       *
>>
>> Here is the code I have written so far. Can any provide assistance?
>>
> [SNIP]
>
> I agree with Eric, kudos for acknowledging that this is for a class.
>
> Here's another hint: the solution is always in your head, not in code.
> Code is just an implementation of a solution.
>
> Pseudocode is often a good first approach. Pen (or pencil) and paper -
> describe how you'd solve that problem. You can even execute and debug
> pseudocode. :-)

For my own future memento.
Attempto Controlled English (ACE)
<http://attempto.ifi.uzh.ch/site/>

>
> 9 times out of 10, we make mistakes because we are hasty. We are not
> Donald Knuthian. We leap into coding.
>
> AHS

[toc] | [prev] | [next] | [standalone]


#25229

FromArved Sandstrom <asandstrom2@eastlink.ca>
Date2013-09-05 09:44 -0300
Message-ID<01%Vt.267521$Z84.78576@fx29.iad>
In reply to#25228
On 09/05/2013 04:32 AM, Jeff Higgins wrote:
> On 09/04/2013 11:20 PM, Arved Sandstrom wrote:
>> On 09/04/2013 09:15 PM, johnnyringo888@gmail.com wrote:
>>> I have to create a diamond shape for my java class.
>>>
>>>       *
>>>      ***
>>>     *****
>>>    *******
>>>   *********
>>>    *******
>>>     *****
>>>      ***
>>>       *
>>>
>>> Here is the code I have written so far. Can any provide assistance?
>>>
>> [SNIP]
>>
>> I agree with Eric, kudos for acknowledging that this is for a class.
>>
>> Here's another hint: the solution is always in your head, not in code.
>> Code is just an implementation of a solution.
>>
>> Pseudocode is often a good first approach. Pen (or pencil) and paper -
>> describe how you'd solve that problem. You can even execute and debug
>> pseudocode. :-)
>
> For my own future memento.
> Attempto Controlled English (ACE)
> <http://attempto.ifi.uzh.ch/site/>
>
>>
>> 9 times out of 10, we make mistakes because we are hasty. We are not
>> Donald Knuthian. We leap into coding.
>>
>> AHS
>
Scan http://www.codinghorror.com/blog/2009/05/pseudocode-or-code.html

And read the comments too.

Everyone is different as to what works. But I've often found that laying 
out a program structure in comments first works pretty good. I mentioned 
Knuth on purpose too: literate programming.

AHS

-- 
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift

[toc] | [prev] | [next] | [standalone]


#25258

FromJohn Stephens <johnnyringo888@gmail.com>
Date2013-09-08 09:00 -0700
Message-ID<602cd444-c24d-4144-b4ea-2a8d6a9558bf@googlegroups.com>
In reply to#25223
On Wednesday, September 4, 2013 8:15:53 PM UTC-4, John Stephens wrote:
> I have to create a diamond shape for my java class.
> 
> 
> 
>      *
> 
>     ***
> 
>    *****
> 
>   *******
> 
>  *********
> 
>   *******
> 
>    *****
> 
>     ***
> 
>      *
> 
> 
> 
> Here is the code I have written so far. Can any provide assistance?
> 
> 
> 
> 
> 
> 
> 
> public class Diamond
> 
> {
> 
> 	public static void main (String[] args)
> 
> 	{
> 
> 	 for ( int i = 1; i <= 10; i++ )
> 
>          {
> 
>             for ( int j = 0; j < i; j++ )
> 
>             {
> 
> 	       System.out.print ("*");
> 
>             }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }   
> 
>          }
> 
> 	
> 
> 	 for ( int i = 10; i > 0; i-- )
> 
> 	 {
> 
> 	    for ( int j = 0; j < i; j++ )
> 
> 	    {
> 
> 	       System.out.print ("*");
> 
>             }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }   
> 
>          }
> 
> 
> 
> 	 for ( int i = 10; i > 0; i-- )
> 
>          {
> 
>             for ( int j = 0; j < (10 - i); j++ )
> 
> 				{
> 
> 	        System.out.print (" ");
> 
>             }            }
> 
>             for ( int j = 0; j < i; j++ )	
> 
> 		      {
> 
>                System.out.print ("*");
> 
>             }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }   
> 
>  	      }
> 
>       
> 
>       for ( int i = 1; i <= 10; i++ )
> 
>          {
> 
>             for ( int j = 0; j < (10 - i); j++ );
> 
>             { 
> 
>                System.out.print (" ");
> 
>             }      
> 
> 	    for ( int j = 0; j < i; j++ );
> 
> 	    {
> 
> 	       System.out.print ("*");
> 
>   	    }
> 
>             {  
> 
>                System.out.println ("\n");
> 
>             }
> 
>          }   
> 
>    }
> 
> }
> 
> 
> 
> When I compile this is what I get:
> 
> 
> 
> *
> 
> 
> 
> **
> 
> 
> 
> ***
> 
> 
> 
> ****
> 
> 
> 
> *****
> 
> 
> 
> ******
> 
> 
> 
> *******
> 
> 
> 
> ********
> 
> 
> 
> *********
> 
> 
> 
> **********
> 
> 
> 
> **********
> 
> 
> 
> *********
> 
> 
> 
> ********
> 
> 
> 
> *******
> 
> 
> 
> ******
> 
> 
> 
> *****
> 
> 
> 
> ****
> 
> 
> 
> ***
> 
> 
> 
> **
> 
> 
> 
> *
> 
> 
> 
> **********
> 
> 
> 
>  *********
> 
> 
> 
>   ********
> 
> 
> 
>    *******
> 
> 
> 
>     ******
> 
> 
> 
>      *****
> 
> 
> 
>       ****
> 
> 
> 
>        ***
> 
> 
> 
>         **
> 
> 
> 
>          *
> 
> 
> 
>          *
> 
> 
> 
>         **
> 
> 
> 
>        ***
> 
> 
> 
>       ****
> 
> 
> 
>      *****
> 
> 
> 
>     ******
> 
> 
> 
>    *******
> 
> 
> 
>   ********
> 
> 
> 
>  *********
> 
> 
> 
> **********

Thank you all for the hints. I figured this out!!!!!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web