Solved

Design program to draw diamond depending on diameter size

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

Industry Leaders: 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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.
Suggested Courses

617 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