// File: Diamond.java // Author: Dr. Blaise W. Liffick // Date: Fall 2007 /* This program prints out a diamond shape, given * an input that indicates the size (number of lines) * of the shape. When the size is odd, it prints a * shape exactly that size. When the size is even, * it prints a shape one larger. */ import java.util.*; public class Diamond { public static void main(String[] args) { int size; // The size of the diamond. Input. Scanner input = new Scanner(System.in); System.out.print("Enter size of diamond: "); size = input.nextInt(); drawTop(size / 2 + 1); drawBottom(size / 2); } //--------------------------------------- // Draw the top "half" of the diamond shape. // Pass the height of the top in as a parameter. public static void drawTop(int height) { // for each line of the top for (int i = 1; i <= height; i++) { // print leading spaces for (int j = 1; j <= height - i; j++) { System.out.print(" "); } // print the line of asterisks for (int j = 1; j <= 2*i-1; j++) { System.out.print("*"); } System.out.println(); } } //-------------------------------------------- // Draw the bottom "half" of the diamond shape. // Pass the height of the bottom in as a parameter. public static void drawBottom(int height) { // for each line of the bottom for (int i = height; i >= 1; i--) { // print leading spaces for (int j = 1; j <= height - i + 1; j++) { System.out.print(" "); } // print the line of asterisks for (int j = 1; j <= 2*i-1; j++) { System.out.print("*"); } System.out.println(); } } }