logo
g Text Version
Auto
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Society & Culture
Sports
Travel & Leisure
TV & Movies

dailyclick
Bored? Games!
Postcards
Astrology
Take a Quiz
Rate My Photo

new
Journals
Folklore and Mythology
Business Coach
Marriage
Senior Living
Ethnic Beauty
Adolescence


dailyclick
All times in EST

Full Schedule
g
g JavaScript / Java Site
Editor Wanted
BellaOnline's JavaScript / Java Editor

g

Fun With Java - Draw A Diamond
Guest Author - Julie L Baumler

I recently found an interesting request from a Java student on one of the forums I read. His class had been asked to use a for loop to draw a diamond out of asterisks (*). He had a correct and functioning solution, but had been marked down because it was overly complicated and featured several loops. The student asked for suggestions on easier ways to achieve the same result. I thought this was an interesting problem and decided to try my hand at it. My program is below (or you can download a copy here.) This clearly isn't production code – for one thing, while I love the simplicity of the ?: operator, I try not to use it in production code because many programmers find that it makes it harder to read and understand. Not to mention, this code will break if max_width is changed to a number larger than 46. But it is an example of a fairly compact and logical method to achieve the desired result. Can you do better? Report back in our forum.

/*
 * diamonds.java
 *
 * Created on June 4, 2007, 11:39 PM
 */

/**
 *
 * @author Julie Baumler
 */
public class diamonds {

/** Creates a new instance of diamonds */
public diamonds() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int max_width=9; // # of chars wide to make diamond
int width=max_width % 2 == 0? max_width/2: max_width/2 +1;
String stars="**********************************************";
String spaces=" ";
for ( int i=1; i<(width*2); i++ ) {
System.out.print( spaces.substring(0, (i < width ? width -i : i%width)));
System.out.println(stars.substring(0,2*(i<=width? i : width-(i%width))-1 ));
}

}

}

The original request:
Pinimo. "Help me with java (sic)." Online posting. Week of 13 May 2007. Java Discussion (Forum). 4 June 2007 <http://www.mylot.com/w/discussions/1098760.aspx>.

This site needs an editor - click to learn more!

Java For Programmers
JavaScript/Java Newsletter
RSS
Related Articles
Previous Features
Site Map

Add Fun+With+Java+%2D+Draw+A+Diamond to Twitter Add Fun+With+Java+%2D+Draw+A+Diamond to Facebook Add Fun+With+Java+%2D+Draw+A+Diamond to MySpace Add Fun+With+Java+%2D+Draw+A+Diamond to Del.icio.us Digg Fun+With+Java+%2D+Draw+A+Diamond Add Fun+With+Java+%2D+Draw+A+Diamond to Yahoo My Web Add Fun+With+Java+%2D+Draw+A+Diamond to Google Bookmarks Add Fun+With+Java+%2D+Draw+A+Diamond to Stumbleupon Add Fun+With+Java+%2D+Draw+A+Diamond to Reddit


Content copyright © 2009 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


For FREE email updates, subscribe to the JavaScript / Java Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor

g features
Hearing Dog Tool - Adding Randomness

Hearing Dog Training Tool – setTimeout()

GCJ - an Independant Java Implementation

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter

jobs
what
job title, keywords
where
city, state or zip
jobs by job search


vote
Growing a Garden
Veggies and Flowers
Veggies Only
Flowers Only
No Garden

g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2009 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor