Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1. /**********************************************************************
  2.  ** Program Name:     Scaling Diamond
  3.  ** Author:           Terry Weiss
  4.  ** Date Written:     August 28, 2015
  5.  ** Course/Section:   CSC 111-003W
  6.  ** Program Description:
  7.  **    This program will generate and print a diamond of stars with a
  8.  **      size provided by the user.
  9.  **********************************************************************/
  10.  
  11. import java.util.Scanner;
  12.  
  13. /*
  14.  *  This class will make and display scalable diamond shape.
  15.  *
  16.  *  Algorithm:
  17.  *  Ask dimensions
  18.  *  Make sure the dimensions are within bounds, or try again
  19.  *  Generate the diamond
  20.  *  Display the diamond shape
  21.  */
  22. public class Test
  23. {
  24.  
  25.     /*
  26.      * This method generates and provides a String with the picture of a
  27.      * diamond whose width is specified by the user.
  28.      *
  29.      * Algorithm:
  30.      * For each line, draw the number of spaces and then the number of *s and
  31.      *   add it to the picture:
  32.      *     * Generate picture for current line. (currentLine)
  33.      *     * Append picture of line to full picture of diamond
  34.      *       (diamond = diamond + currentLine)
  35.      *     * If it’s an even diamond-like, and this is the longest line,
  36.      *       append a duplicate of the picture to make the final diamond’s total
  37.      *       height match the width
  38.      * Provide the picture of the diamond (diamond)
  39.      */
  40.     static String makeDiamond( int totalSize )
  41.     {
  42.         /*
  43.          *  This is the workspace that holds the picture of the diamond. All
  44.          *  diamonds will start and end with an empty line so they can be
  45.          *  printed after each other more visibly.
  46.          */
  47.         String diamond = "\n";
  48.  
  49.         /*
  50.          *  This is whether or not the size of the diamond is even or odd, since
  51.          *  they need to be handled differently. Note: a real diamond can’t have
  52.          *  an even height or width and be symmetrical on an ASCII display, so
  53.          *  if an even width is requested, the picture won’t start or end with a
  54.          *  point, and it will have a duplicate largest line.
  55.          *
  56.          *  The width is even if the total size is divisible by 2 with no
  57.          *  remainder.
  58.          */
  59.         boolean isEven = ((totalSize % 2) == 0);
  60.  
  61.         /*
  62.          *  This is the number of the current line we're on so far.
  63.          */
  64.         int lineNumber;
  65.  
  66.         /*
  67.          *  This is the picture of the current line we're on in the diamond.
  68.          */
  69.         String currentLine;
  70.  
  71.         /*
  72.          *  This is the number of the widest line in the diamond. If the diamond
  73.          *  is even, this line will be added twice.
  74.          */
  75.         int longestLine = (int) Math.ceil(totalSize / 2);
  76.        
  77.  
  78.         /*
  79.          *  Algorithm:
  80.          *  For each line:
  81.          *  * Start with an empty String
  82.          *  * Append leading spaces to line up *s
  83.          *      * | longestLine - lineNumber |
  84.          *  * Calculate the number of *s and append to String to give shape of diamond
  85.          *      * -2 * | longestLine - lineNumber | + totalSize
  86.          *  * If we're on the longest line and it's an even diamond, append the
  87.          *    current line again.
  88.          *  * Finish the line with a newline character
  89.          *  Provide the finished picture
  90.          */
  91.         for( lineNumber = 1; lineNumber <= totalSize; lineNumber++ )
  92.         {
  93.             currentLine = "";
  94.            
  95.             /*
  96.              * space and star keep track of how many trailing spaces or
  97.              * stars are being printed on each line.
  98.              */
  99.             int space, star;
  100.  
  101.  
  102.             // The number of leading spaces is | longestLine - lineNumber |
  103.             for( space = 0; space < Math.abs(longestLine - lineNumber + 1); space++ )
  104.             {
  105.                 currentLine += " ";
  106.             }
  107.  
  108.             // The number of *s is -2 * | longestLine - lineNumber | + totalSize
  109.             for( star = 0; star < (-2 * Math.abs(longestLine - lineNumber + 1) + totalSize);
  110.                     star++ )
  111.             {
  112.                 currentLine += "*";
  113.             }
  114.  
  115.             // Repeat the longest line if that's what we're on
  116.             if( isEven && ((lineNumber - 1) == longestLine) )
  117.             {
  118.                 currentLine += "\n" + currentLine;
  119.             }
  120.  
  121.             currentLine += "\n";
  122.  
  123.             diamond += currentLine;
  124.         }
  125.  
  126.         return diamond;
  127.     }
  128.  
  129.  
  130.     /*
  131.      *  This program will ask a user for a diamond's size and print out the
  132.      *  diamond.
  133.      *
  134.      *  Algorithm:
  135.      *  Ask the total size (totalSize)
  136.      *  Make sure the size is a valid number between 1 and 10 or ask again
  137.      *  Generate the diamond with the specified size (makeDiamond(totalSize))
  138.      *  Display the picture of the diamond
  139.      */
  140.     public static void main( String[] args )
  141.     {
  142.         Scanner user_input = new Scanner(System.in);
  143.  
  144.         /*
  145.          *  dimensions is used to capture the user's requested dimensions
  146.          */
  147.         String dimensions;
  148.        
  149.         /*
  150.          *  size is used to see how big the dimensions are as a number
  151.          */
  152.         Integer size = 0;
  153.  
  154.  
  155.         // Continue to ask for the dimensions until it's within bounds
  156.         while( size <= 0 || size > 15 )
  157.         {
  158.             System.out.print("How big would you like your diamond?  ");
  159.             dimensions = user_input.next();
  160.            
  161.             try {
  162.                 size = Integer.parseInt(dimensions);
  163.             } catch( NumberFormatException e ) {
  164.                 System.out.println("If only we could measure dimensions without"
  165.                         + " whole numbers! Try a number between 1 and 10.");
  166.                 continue;
  167.             }
  168.  
  169.             if( size <= 0 )
  170.             {
  171.                 System.out.println("Your negativity is killing my mood! Try "
  172.                     + "something between 1 and 10.");
  173.             }
  174.             else if( size > 10 )
  175.             {
  176.                 System.out.println("Woah! I love people who Think Big, but "
  177.                     + "that's just TOO big! Try something between 1 and 10.");
  178.             }
  179.         }
  180.  
  181.         String str = makeDiamond(size);
  182.         System.out.println(str);
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement