Pages

Monday, August 4, 2014

ISC COMPUTER SCIENCE PRACTICAL– 2000 DIAMOND SHAPE PATTERN

ISC COMPUTER SCIENCE PRACTICAL– 2000 DIAMOND SHAPE PATTERN, ISC JAVA PROGRAMS
Write a program to print a rectangle with diamond shape gap exactly at the centre of
the rectangle, using an input string with odd number of characters, not exceeding 20.

For example:

INPUT:

HAPPY-HAPPY
OUTPUT:

HAPPY HAPPY
HAPP      APPY
HAP           PPY
HA               PY
H                   Y
HA               PY
HAP           PPY
HAPP      APPY
HAPPY HAPPY

INPUT:

ISCPRAC*ISCPRAC

OUTPUT:

ISCPRAC ISCPRAC
ISCPRA     SCPRAC
ISCPR         CPRAC
ISCP              PRAC
ISC                  RAC
IS                       AC
I                            C
IS                       AC
ISC                  RAC
ISCP              PRAC
ISCPR         CPRAC
ISCPRA    SCPRAC
ISCPRAC ISCPRAC



import java.util.*;
import java.io.*;
public class DiamondShape {
    public static void main(String[] args) throws IOException {
        String msg;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
        System.out.println(" Enter the sentence:");
        msg = br.readLine();
        while ( msg.length() > 20 || (msg.length())%2 == 0) {
            System.out.println("INCORRECT FORMAT, INPUT AGAIN:");
            msg = br.readLine();
            }
        Pattern pt = new Pattern(msg);
        System.out.println("OUTPUT:");
        pt.patternPrint();
        }
    }   
class Pattern {
    Pattern(String msg) {
        inputMsg = msg;
        middle = (inputMsg.length()-1)/2;
        reverse = new String[middle+2];
        }
    public void patternPrint() {
       
        int index,temp = middle+1;
        index = temp;
        reverse[index] += inputMsg;
        for ( int i = middle; i > 0; i--) {  // loop to print the pattern till we have one character at left and one at right **
            reverse[index-1] = "";
            for ( int j = 0; j <= middle; j++) { // loop to print left half from the centre
                if ( j > i-1) {
                    System.out.print(" ");
                    reverse[index-1] += " ";
                    }
                else {   
                    System.out.print(inputMsg.charAt(j));
                    reverse[index-1] += inputMsg.charAt(j);
                    }
                }   
            for ( int k = middle+1; k < inputMsg.length(); k++) { // loop to print right half from the centre
                if( k < temp ) {
                    System.out.print(" ");
                    reverse[index-1] += " ";
                    }
                else {   
                    System.out.print(inputMsg.charAt(k));   
                    reverse[index-1] += inputMsg.charAt(k);
                    }
                }
            temp++;   
            index--;
            System.out.println();            
            }
        for ( int i = 2; i < middle+1; i++) // once the loop ** is completed we just print the string in reverse order to get the pattern
            System.out.println(reverse[i]);
       
        }   
    private String inputMsg;
    private String[] reverse;
    private int middle;
    }   

No comments:

Post a Comment