Monday, September 7, 2009

Hollow Rhombus example in java............


This is my 1st post to this blog.Here I've tried to solve,one of the most common programming
construct through java.The problem name is 'Hollow Rhombus Example'.This type of tusks has
generally given to us as an assignment to clear our programing concept,mainly looping concept.
I have done similar type of example in C,
@1st year in my B.Tech course.Here I've done it through java.The output of the problem would be
a hollow rhombus.The building concept will be much more easier to us,if we divide the entitr tusk into
4 modules.
i)Printing the 1st line as it is.
ii)Generate the upper half of the triangle.
ii)Generate the lower half of the triangle.
iv)Finally printing the last line.
Here is the source code for the tusk and corresponding outputs.I think it will help you more to understand the
problem more suitably.

/* Code For The Tusk*/

import java.io.*;
class Triangle{
public static void main(String args[]) throws IOException{
int n,i,j,z,y,chlen,sp=-2,str=0; //initializing the variables.....
String ch="";
DataInputStream dis=new DataInputStream(System.in);
System.out.print("Entern Any String:");
ch=dis.readLine(); //talking input..............
chlen=ch.length();
ch=ch+"*"+ch;
System.out.print("\n"+ch); //printing the 1st line............
for(i=0;i{
System.out.println();
sp+=2;
for(j=0;j<(chlen-i);j++)
System.out.print(ch.charAt(j));
for(z=0;z<=sp;z++)
System.out.print(" ");
for(y=str;ySystem.out.print(ch.charAt(y));
str++;
}
str -= 2 ;
for(i=chlen-1-1;i>=0;i--) //code to generate the lower half.............
{
System.out.println();
sp-=2;
for(j=0;j<(chlen-i);j++)
System.out.print(ch.charAt(j));
for(z=sp;z>=0;z--)
System.out.print(" ");
for(y=str;ySystem.out.print(ch.charAt(y));
str--;
}
System.out.print("\n"+ch+"\n"); //printing the last line............
}
}

No comments:

Post a Comment