Solved

Design program to draw diamond depending on diameter size

Posted on 2000-03-23
4
1,752 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 77
javap not working 8 44
type mismatch (Object[] to double[] 4 24
by zero exception 10 39
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 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