Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Design program to draw diamond depending on diameter size

Posted on 2000-03-23
4
1,756 Views
Last Modified: 2013-12-03
Design, write in Java, test and document a program (application) that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single newline character. Maximise your use of repetition (with nested loops) and minimise the number of output statements.

The program should ask the user to enter an odd number in the range 1 to 21 to specify the number of rows in the diamond. The program should then display a diamond of the appropriate size. For example, if the number entered by the user is 9, the following diamond will be displayed:

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

Note that the diamond is not to be printed by outputting strings as
System.out.println("      *     ");
System.out.println("    ***    ");
System.out.println("   *****   ");
System.out.println(" ******* ");
      System.out.println("*********");
      System.out.println(" ******* ");
System.out.println("  *****  ");
System.out.println("    ***    ");
System.out.println("      *     ");
Any solution that produces a diamond by outputting strings (as above) will fetch a zero mark.
0
Comment
Question by:warc_renegadex
  • 2
4 Comments
 
LVL 3

Expert Comment

by:terajiv
ID: 2652312
This Seems tobe Home work...isnt it?
0
 
LVL 1

Accepted Solution

by:
mrachow earned 50 total points
ID: 2652628
Hope I'm not doing your course lesson ?! ;-)
Looks good when printed to an environment with monospaced font of course.
/**
 * This program
 */
import java.util.*;

public class actual {                   // Define a class
  public static void main(String[] args) { // The program starts here

    int numMiddle = 33;
    int maxBlanks = numMiddle/2;
    int numBlanks;
    int idx;
    String blanks;
    String stars;
    Stack keep = new Stack();

    for(numBlanks=maxBlanks;numBlanks>=0;numBlanks--)
    {
      blanks = "";
      stars = "";
      for(idx=1;idx<=numBlanks;idx++)
      {
        blanks = blanks + " ";
      }
      for(idx=1;idx<=2*(maxBlanks-numBlanks)+1;idx++)
      {
        stars = stars + "*";
      }
      System.out.println(blanks + stars);
      keep.push(blanks + stars);
    }
    keep.pop();     // Smething to think about for you :-))
    while(!keep.empty())
    {
      System.out.println(keep.pop());
    }
  }                                        // The main() method ends here
}                                          // The class ends here
0
 

Author Comment

by:warc_renegadex
ID: 2653687
so thanx to mrachow for your answer, this is a good idea. i had some other loops and while stuff in there but i couild not get to write a class as good as yours. now i'll write some main method and try it out.
0
 

Author Comment

by:warc_renegadex
ID: 2653744
hey mrachow
do you have a email address my is at w.ho@student.murdoch.edu.au or wmh97@hotmail.com
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question