Solved

Design program to draw diamond depending on diameter size

Posted on 2000-03-23
4
1,757 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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