Creating a diamond shape wtih java

J

johnnyringo888

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:

*

**

***

****

*****

******

*******

********

*********

**********

**********

*********

********

*******

******

*****

****

***

**

*

**********

*********

********

*******

******

*****

****

***

**

*

*

**

***

****

*****

******

*******

********

*********

**********
 
E

Eric Sosman

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?
 
J

John Stephens

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.
 
S

Stefan Ram

I have to create a diamond shape for my java class.
*
***
*****
*******
*********
*******
*****
***
*

Starting with the dreadly diamond is too difficult!

Therefore, I have on my German-language page

http://www.purl.org/stefan_ram/pub/programmieren_aufgabe_iteration_de

some other simpler exercise in front of that diamond:

Write a method »line« to print a line of the argument
length:

line( 20 )
********************

. Write a method »rectangle«:

rectangle( 17, 4 )
*****************
*****************
*****************
*****************

. Write a methode for a shifted rectangle:

rectangle( 17, 4, 2 )
*****************
*****************
*****************
*****************

. Write a method for a simple triangle, given
height and increment of length per line.

triangle( 6, 1 )
*
**
***
****
*****
******

. Write a methode for a symmetric triangle:

triangle( 4, 1 )

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

. Only having completed the preceding exercises in this
order, students may try to attack the dreadly diamond.
Without the proper preparation, it can be very dangerous.
 
A

Arved Sandstrom

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
 
J

Jeff Higgins

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)
 
A

Arved Sandstrom

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)
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
 
R

Roedy Green

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.
 
J

John Stephens

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!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top