Translate

Saturday, 26 September 2015

Write a Java code to print following pattern

A
BC
DEF
GHIJ

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

Sunday, 31 May 2015

Write a java code to print following pattern

 
J
JA
JAV
JAVA
JAVAJ
JAVAJ2
JAVAJ2E
JAVAJ2EE

Solution:

class PrintJava
{
public static void main(String[] args)
{
char[] arrChar={'J','A','V','A','J','2','E','E'};
for(int i=0;i<=8;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(arrChar[j]);
}
System.out.println();
}
}
}

Friday, 8 May 2015

Write a java code to print following pattern

1
23
456

Solution:

class printPattern
{
     public static void main(String abc[])
     {
        int counter=1;    
        for(int i=1;i<4;i++)
        {                    
            for(int j=1;j<=i;j++)
              {
                System.out.print(counter);
                counter++;
              }
              System.out.println();
         }
     }
}

Write a java code to print following pattern

A
BB
CCC
DDDD


Solution:

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

Thursday, 9 January 2014

Is it possible to overload main() method of java. If yes, explain with example.

Ans:
Yes, it is possible to overload main() method in java. You can have more than one main method but with different argument list.
A simple example of overloading main method:

//Test.java
class Test
{
public static void main(String abc[])
{
System.out.println("In Main method");
main(10);
}
static void main(int num)
{
System.out.println("Overloaded main method : prints number :" + num);
}
}

Wednesday, 8 January 2014

Create multiple threads in java and assign different sleep() time to each Thread.

//Test.java

class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=0;i<5;i++)
{
try{
String threadName=getName();
if(threadName.equals("A")){
System.out.println(i +": Thread Name: " + getName());
sleep(1000);
}
else if(threadName.equals("B")){
System.out.println("                    "+i +": Thread Name: " + getName());
sleep(3000);
}
else
{
System.out.println("                                        "+i +": Thread Name: "+ getName());
sleep(5000);
}
}
catch(Exception e){System.out.println(e);}
}
}
}

class Test
{
public static void main(String abc[])
{
System.out.println("-----------------------------------------------------------");
new simpleThread("A").start();
new simpleThread("B").start();
new simpleThread("C").start();
}
}

Simple example of Multi Threading in java. Create multiple threads in java.

//Test.java

class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=0;i<5;i++)
System.out.println(i +": Thread Name: " + getName());
{
try{
sleep(1000);
}
catch(Exception e){System.out.println(e);}
}
}
}

class Test
{
public static void main(String abc[])
{
System.out.println("-----------------------------------------------------------");
new simpleThread("A").start();
new simpleThread("B").start();
}
}