Aug 17, 2011

Print a Diamond using JAVA


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Diamond {

public static void main(String[] args) {
Diamond diam = new Diamond();
diam.checkInput();
}

private void draw(int N, int counter, String sym) {
int max=0;

for (int i = 0; i < N;) {

for (int j = 0; j <= (N - i) / 2; j++) {
System.out.print(" ");
}
for (max = 0; max <= i; max++) {
System.out.print(sym);


}

System.out.println();

i = i + counter;


}


for (int i = (max - counter); i > 0;) {

for (int j = 0; j <= (N - i) / 2; j++) {
System.out.print(" ");
}
for (int k = i; k > 0; k--) {
System.out.print(sym);
}

System.out.println();
i = i - counter;

}
}

private String getInput() {
String value = null;

BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
try {
value = buf.readLine();

} catch (IOException e) {
e.printStackTrace();
}
return value;
}

private void checkInput() {
System.out.print("Enter the Value of N:");
int N = Integer.parseInt(getInput());
System.out.print("\n Enter the Value for counter: ");
int counter = Integer.parseInt(getInput());
System.out.println("\n Enter the Diamond Symbol:");
String symbol = getInput();

System.out.println("Vaildating Check wait for 2 sec");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {

e.printStackTrace();
}
if( N>0  && N%2 !=0  && counter >0 && counter%2 == 0 && counter
draw(N, counter, symbol);
}
else{
System.out.println("invalid data");
}



}

}

No comments:

Post a Comment

Post a Comment

Note: Only a member of this blog may post a comment.