Translate

Saturday, 29 May 2021

Coming soon .Net CORE

We are soon starting posts on .NET CORE. 


You will learn what is .NET Core, why should we use it, pros and cons of .NET Core and various assignments.


We will also have exciting hands on assignments and QA sections. 


You can follow this blog to receive new updates.

Wednesday, 31 August 2016

Write a java code to print following pattern

I
CC
SSS
EEEE

Solution:

class printABCD
{
     public static void main(String abc[])
     {
    char arr[]={'I','C','S','E'};   // Store alphabets in an array
        for(int i=0;i<4;i++)
        {                                
            for(int j=0;j<=i;j++)
              {
                System.out.print(arr[i]);              
              }
          System.out.println();
         }
     }
}

Friday, 5 February 2016

Write a java code to print following pattern

(a)
(a+b)
(a+b+c)
(a+b+c+d)


Solution:

//PrintABCD.java
class PrintABCD
{
public static void main(String abc[])
{
for(int i=1;i<10;i++) // increase count 10 to 27 to print till "z"
        {          
            String strChars="";  
            int alphabet=97;                          //ASCII value of A = 65 and a=97
            for(int j=1;j<=i;j++)
              {
                strChars=strChars+(char)alphabet + " + ";
                alphabet++;
              } 
          System.out.println("(" + strChars.substring(0,strChars.length()-3) + ")");
         }
     }
}

Friday, 23 October 2015

Write a java code to print following pattern

AAAA
   AAA
      AA
         A
         A
         AB
         ABC
         ABCD

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

    static void printAAAA()
    {
        int alphabet=65;                          //ASCII value of A
        for(int i=4;i>0;i--)
        {      
            for(int k=4;k>i;k--)
            {
                    System.out.print(" ");
            }   
                for(int j=1;j<=i;j++)
                {
                    System.out.print((char)alphabet);
                }
                System.out.println();
         }
    }
}

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();
}
}

Monday, 30 December 2013

Static Initialization Blocks with a simple example

A static initialization block is a normal block of code enclosed 
in braces, { }, and preceded by the static keyword. Here is an example: 

static { 
 // whatever code is needed for initialization goes here
} 

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. 
The run-time system guarantees that static initialization blocks are called in the order that they appear in the source code.
For example,
Consider following code.

class StaticBlockDemo 
{ 
static 
{ 
System.out.println("I am in static block 1"); 
} 
static 
{ 
System.out.println("I am in static block 2"); 
} 
public static void main(String args[]) 
{ 
System.out.println("I am in main() method"); 
} 
}

Output:
 
I am in static block 1
I am in static block 2
I am in main() method

So you can see that static block initialization takes place before main() method. 
Static blocks are executed sequentially. Here we have two static blocks. 
Block 1 executed prior to Block 2. 
If we change order of blocks to something like 

static 
{ 
System.out.println("I am in static block 2"); 
} 

static 
{ 
System.out.println("I am in static block 1"); 
} 

 
then output will be

I am in static block 2
I am in static block 1

 
Use of static block:

Generally, static block is used to perform initialization.

Tuesday, 24 December 2013

Write a java code to print goat

class goat
{
public static void main(String abc[])
{
System.out.println();
System.out.println();
System.out.println("               <___>");
System.out.println("               (o-o) ");
System.out.println("   +============\\_/  ");
System.out.println("  / || %%%%%  ||      ");
System.out.println(" /  || %%%%%  ||      ");
System.out.println("*   ||--------||      ");
System.out.println("    \"\"        \"\"      ");
}
}

Note: Just copy paste the above code in notepad file and run it. I hope you will enjoy this. Also, from this code you will learn about the importance of escape characters in a program.

Sunday, 29 September 2013

Write a java code to get an ascii value of a given character

Solution:

import java.io.*;
class CharToAscii
{
public static void main(String abc[])
{
System.out.println("Enter a character");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
int c=br.read();
System.out.println("The ascii value of above character is " + c);
}
catch(Exception e)
{
System.out.println("Error...");
}
}}

Output:
C:\> java CharToAscii
Enter a character
A
The ascii value of above character is 65

Write a java code to sort an array

Solution:

class Sort
{
public static void main(String abc[])
{
int temp;
int arr[]={3,7,8,5,2,1,9,6,4};
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{

//Use arr[i]<arr[j] if you want to sort an array in descending order.
//Note: The below condition will sort an array in ascending order.

if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}

}

Write a java code to print square

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

Solution:-

class square
{
public static void main(String abc[])
{
int size=9;                 //You can change the value of size variable to minimize or maximize square
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i>=1&&i<=size-2&&j>=1&&j<=size-2)
{
if(i==size/2&&j==size/2)
{
System.out.print("*");
}
else{
System.out.print(" ");
}
}
else
{
System.out.print("*");
}

}
System.out.println();
}
}

}

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();
         }
     }
}