Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

diamond program

  Asked By: Arnold    Date: Apr 29    Category: Java    Views: 1537
  

For one of my programming classes, I have to design a program that
takes a word given by the user in a window pop-up and makes the word
into a diamond. ex: The user types in "programs" and the system
response is:

m m
a a
r r
g g
o o
r r
p p
r r
o o
g g
r r
a a
m m


Does anyone have any suggestions on how to go about doing this?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Gustavo Costa     Answered On: Apr 29

1. Use a string buffer and fill it whith empty chars the length of
the diamond.

2. divide the length by two to get the midpoint

3. Use a method (think it's setChar(position, character)) to set the
last character to the midpoint.

4. create 2 variables that increment/decrement by one.

5. Each time they increment/decrement set the previous char to ' ' to
cover its tracks

6. set the other characters to the next letter at the increment/
decrement position (setChar(decrement, character2))

7. Repeat while Stringbuffers character at 0 = ' '

8. Use similar theology for bottom half of diamond

Hope this helps, i just finished a similar proggy a few weeks ago
except instead of typing a word, the user  enters a char and the width
of the diamond and the output is (assuming char is '*' and width is 5)

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

 
Didn't find what you were looking for? Find more on diamond program Or get search suggestion and latest updates.




Tagged: