Translate

Thursday, 18 July 2013

Write java code to print following pattern

  *
 ***
*****

 ***
  *

Solution:

class printDiamond
{
public static void main(String abc[])
{
int Diamond_Size=19;      

if(Diamond_Size%2==1)
{
printUpperTriangle(Diamond_Size);
System.out.println();
printLowerTriangle(Diamond_Size);
}
else
{
System.out.println("Please set limit to some odd number");
}
}
public static void printUpperTriangle(int limit)
{
int sp=limit/2;
for(int i=1,space=sp;i<=limit;i+=2,space--)
{
if(space>=0)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printLowerTriangle(int limit)
{
int sp=limit/2;
for(int i=limit-2,space=1;i>0;i-=2,space++)
{
if(space<=sp)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printSpace(int space)
{
for(int i=space;i>0;i--)
{
System.out.print(" ");
}
}
}
//[Note: Grow Diamond size by increasing value of Diamond_Size variable.
//for example Diamond_Size=29; 
//please set Diamond_Size variable to odd numbers only.]

Wrtite java code to print triangle

*****
  ***
    *

Solution:

class printTriangle
{
public static void main(String abc[])
{
int Triangle_Size=19;    

if(Triangle_Size%2==1)
{
printTriangle(Triangle_Size);
}
else
{
System.out.println("Please set limit to some odd number");
}
}
public static void printTriangle(int limit)
{
int sp=limit/2;
for(int i=limit-2,space=1;i>0;i-=2,space++)
{
if(space<=sp)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printSpace(int space)
{
for(int i=space;i>0;i--)
{
System.out.print(" ");
}
}
}
//[Note: Grow Triangle_Size by increasing value of Triangle_Size variable.
//for example Triangle_Size=29;

//please set Triangle_Size variable to odd numbers only.]

Write java code to print diamond

  *
 ***
*****
 ***
  *

Solution:- [Read last comment in program to enjoy the code]

class printDiamond
{
public static void main(String abc[])
{
int Diamond_Size=19;      

if(Diamond_Size%2==1)
{
printUpperTriangle(Diamond_Size);
printLowerTriangle(Diamond_Size);
}
else
{
System.out.println("Please set limit to some odd number");
}
}
public static void printUpperTriangle(int limit)
{
int sp=limit/2;
for(int i=1,space=sp;i<=limit;i+=2,space--)
{
if(space>=0)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printLowerTriangle(int limit)
{
int sp=limit/2;
for(int i=limit-2,space=1;i>0;i-=2,space++)
{
if(space<=sp)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printSpace(int space)
{
for(int i=space;i>0;i--)
{
System.out.print(" ");
}
}
}
//[Note: Grow Diamond size by increasing value of Diamond_Size variable.
//For example Diamond_Size=29; 
//Please set Diamond_Size variable to odd numbers only.]

Tuesday, 16 July 2013

Write a java code to print following pattern

A
AB
ABC
ABCD

Solution:-

class printABCD
{
     public static void main(String abc[])
     {
        for(int i=1;i<5;i++)
        {
            int alphabet=65;                          //ASCII value of A
            for(int j=1;j<=i;j++)
              {
                System.out.print((char)alphabet);
                alphabet++;
              }
          System.out.println();
         }
     }
}

Monday, 24 June 2013

Parameterless Constructors

It can be tedious to initialize all of the variables in a class each time an instance is created. Because the requirement for initialization is so common, Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.

A  constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Constructors look a little strange because they have no return type, not even void. This is because the implicit return type of a class constructor is the class type itself. It is the constructor's job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.

Example:

class ConstructorDemo
{
int count;
String strName;

// This is constructor for class
ConstructorDemo()             // Constructor Name=Class name
{
count=10;                            // initializing int type variable
strName="First String";       // initializing string type variable
System.out.println("I am inside the Constructor");    
}
}

class CreateConstructorDemoObject
{
public static void main(String args[])
{
ConstructorDemo obj=new ConstructorDemo();    // This statement calls Constructor
System.out.println("I am outside the Constructor");
}
}

When this program is run, it generates the following results:

I am inside the Constructor
I am outside the Constructor

Monday, 9 July 2012

Write java code to print following pattern

..........
.........
........
.......
......
.....
....
...
..
.
Solution:

class Pattern4
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
for(int j=i;j<10;j++)
{
System.out.print(".");
}
System.out.println();
}
}
}



Write java code to reverse a given string using "StringBuffer" class


import java.io.*;

class ReverseString
{
public static void main(String args[]) throws IOException
{
String str;
System.out.println("Enter a string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine();
StringBuffer sb=new StringBuffer(str);
String revstr=new String(sb.reverse());
System.out.println("Reverse of "+str+" is "+revstr);
}

}