Diamond drawer script_Java

Diamond Drawer

The programme allows the user to create a diamond shape on canvas based on the user’s input. The user will enter 3 parameters: x and y coordinate of the diamond tip, and diamond size.

The canvas size is 40×20. User are only allowed to input an x value between 1 and 40 and y value between 1 and 20. Diamond size can only be even number. If the diamond size is too big for the canvas, the program will ask the user to input the diamond size again.

Attention: string input will crash the program.

Pseudo Code

Define Terms:
0.1 let ROWS be the canvas row unber, and COLS be the canvas column number.
0.2 let input_num be the diamond size which holds the users’ input
0.3 let cor_x, cor_y be the coordinates of the diamond tip.
0.4 let diamond_chart be the 2-dimensional canvas chart.

1. Initiate the canvas by filling diamond_chart with space characters.
2. Draw the x and y axis using characters such as ‘+’, ‘|’, ‘-’, ‘^’, ‘>’
3. cor_x ← user input, if the user’s input an invalid number, it asks the user to input again.
4. cor_y ← user input, if the user’s input an invalid number, it asks the user to input again.
5. input_num ← user input, if the user input is too big for the canvas, it asks the user to input again.
6. Let diamond_row ← CORW + 1- cor_y (calculate the diamond tip position on canvas)
7. Let diamond_col ← cor_x +2 (calculate the diamond tip position on canvas)
8. Use for loop to draw the upper diamond
9. Use for loop to draw the lower diamond
10. Print the diamond and axis

//Program Name: DiamondDraw
//Author: Di Wang
//Date: 2016 9 29
//Compiler: JDK1.8
//IDE: Eclipse

import java.util.Scanner;
public class DiamondDraw {

public static void main(String[] args) {
// define the chart dimension
final int ROWS = 20;
final int COLS = 40;
int input_num;
int cor_x;
int cor_y;
// define a 2d array of empty characters.
char [][] diamond_chart= new char [ROWS+3][COLS+3];

// create an empty canvas by filling in space characters
for(int i=0; i< (ROWS+3); i++){
for(int j=0; j< (COLS+3); j++){
diamond_chart [i][j] = ' ';

//initiate a scanner object
Scanner input = new Scanner(System.in);

// draw the y axis
for (int i = 1; i=0 ; i -=10){
diamond_chart [i][1] = ‘0’;
diamond_chart [i][0] = Character.forDigit((ROWS+1-i)/10, 10);

diamond_chart [i-5][1] = ‘5’;
diamond_chart [i-5][0] = Character.forDigit((ROWS+1-i)/10, 10);

// draw the x axis
for (int i = 2; i’;

// draw numbers on the x axis
for(int i= 2; i<(COLS+3); i+=10){
diamond_chart [ROWS+2][i] = '0';
diamond_chart [ROWS+2][i-1] = Character.forDigit(i/10, 10);

if(i+5 COLS||cor_xROWS||cor_y < 1);

// user will only allowed create a limited size diamond
System.out.println("Enter the diamond size (only odd number accepted) :");
System.out.println("Make sure the dimond size is small enough to fit into the canvas.");
input_num = input.nextInt();
}while(cor_x < (input_num/2) || cor_y < input_num || input_num%2==0);

// draw the diamond
int diamond_row = ROWS+1-cor_y;
int diamond_col = cor_x + 2 ;

for (int i = 0; i<= input_num; i +=1){
if (i<=input_num/2)
//upper diamond
for (int j = 0; j input_num/2)
//lower diamond
for (int j = input_num-i-1; j>=0; j–){
diamond_chart [diamond_row + i][diamond_col-j]=’*’;
diamond_chart [diamond_row + i][diamond_col+j]=’*’;


//print out the shape
for(int i = 0; i < (ROWS+3); i++){
for (int j= 0 ; j < (COLS+3); j++){




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s