Solved

Design program to draw diamond depending on diameter size

Posted on 2000-03-23
4
1,748 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
Comment Utility
This Seems tobe Home work...isnt it?
0
 
LVL 1

Accepted Solution

by:
mrachow earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
hey mrachow
do you have a email address my is at w.ho@student.murdoch.edu.au or wmh97@hotmail.com
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
True Type Font Files? 4 62
what font behind pdf 32 100
JAVA part two 5 39
ArrayIndexOutOfBoundException 9 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now