Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************************
- ** Program Name: Scaling Diamond
- ** Author: Terry Weiss
- ** Date Written: August 28, 2015
- ** Course/Section: CSC 111-003W
- ** Program Description:
- ** This program will generate and print a diamond of stars with a
- ** size provided by the user.
- **********************************************************************/
- import java.util.Scanner;
- /*
- * This class will make and display scalable diamond shape.
- *
- * Algorithm:
- * Ask dimensions
- * Make sure the dimensions are within bounds, or try again
- * Generate the diamond
- * Display the diamond shape
- */
- public class Test
- {
- /*
- * This method generates and provides a String with the picture of a
- * diamond whose width is specified by the user.
- *
- * Algorithm:
- * For each line, draw the number of spaces and then the number of *s and
- * add it to the picture:
- * * Generate picture for current line. (currentLine)
- * * Append picture of line to full picture of diamond
- * (diamond = diamond + currentLine)
- * * If it’s an even diamond-like, and this is the longest line,
- * append a duplicate of the picture to make the final diamond’s total
- * height match the width
- * Provide the picture of the diamond (diamond)
- */
- static String makeDiamond( int totalSize )
- {
- /*
- * This is the workspace that holds the picture of the diamond. All
- * diamonds will start and end with an empty line so they can be
- * printed after each other more visibly.
- */
- String diamond = "\n";
- /*
- * This is whether or not the size of the diamond is even or odd, since
- * they need to be handled differently. Note: a real diamond can’t have
- * an even height or width and be symmetrical on an ASCII display, so
- * if an even width is requested, the picture won’t start or end with a
- * point, and it will have a duplicate largest line.
- *
- * The width is even if the total size is divisible by 2 with no
- * remainder.
- */
- boolean isEven = ((totalSize % 2) == 0);
- /*
- * This is the number of the current line we're on so far.
- */
- int lineNumber;
- /*
- * This is the picture of the current line we're on in the diamond.
- */
- String currentLine;
- /*
- * This is the number of the widest line in the diamond. If the diamond
- * is even, this line will be added twice.
- */
- int longestLine = (int) Math.ceil(totalSize / 2);
- /*
- * Algorithm:
- * For each line:
- * * Start with an empty String
- * * Append leading spaces to line up *s
- * * | longestLine - lineNumber |
- * * Calculate the number of *s and append to String to give shape of diamond
- * * -2 * | longestLine - lineNumber | + totalSize
- * * If we're on the longest line and it's an even diamond, append the
- * current line again.
- * * Finish the line with a newline character
- * Provide the finished picture
- */
- for( lineNumber = 1; lineNumber <= totalSize; lineNumber++ )
- {
- currentLine = "";
- /*
- * space and star keep track of how many trailing spaces or
- * stars are being printed on each line.
- */
- int space, star;
- // The number of leading spaces is | longestLine - lineNumber |
- for( space = 0; space < Math.abs(longestLine - lineNumber + 1); space++ )
- {
- currentLine += " ";
- }
- // The number of *s is -2 * | longestLine - lineNumber | + totalSize
- for( star = 0; star < (-2 * Math.abs(longestLine - lineNumber + 1) + totalSize);
- star++ )
- {
- currentLine += "*";
- }
- // Repeat the longest line if that's what we're on
- if( isEven && ((lineNumber - 1) == longestLine) )
- {
- currentLine += "\n" + currentLine;
- }
- currentLine += "\n";
- diamond += currentLine;
- }
- return diamond;
- }
- /*
- * This program will ask a user for a diamond's size and print out the
- * diamond.
- *
- * Algorithm:
- * Ask the total size (totalSize)
- * Make sure the size is a valid number between 1 and 10 or ask again
- * Generate the diamond with the specified size (makeDiamond(totalSize))
- * Display the picture of the diamond
- */
- public static void main( String[] args )
- {
- Scanner user_input = new Scanner(System.in);
- /*
- * dimensions is used to capture the user's requested dimensions
- */
- String dimensions;
- /*
- * size is used to see how big the dimensions are as a number
- */
- Integer size = 0;
- // Continue to ask for the dimensions until it's within bounds
- while( size <= 0 || size > 15 )
- {
- System.out.print("How big would you like your diamond? ");
- dimensions = user_input.next();
- try {
- size = Integer.parseInt(dimensions);
- } catch( NumberFormatException e ) {
- System.out.println("If only we could measure dimensions without"
- + " whole numbers! Try a number between 1 and 10.");
- continue;
- }
- if( size <= 0 )
- {
- System.out.println("Your negativity is killing my mood! Try "
- + "something between 1 and 10.");
- }
- else if( size > 10 )
- {
- System.out.println("Woah! I love people who Think Big, but "
- + "that's just TOO big! Try something between 1 and 10.");
- }
- }
- String str = makeDiamond(size);
- System.out.println(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement