Varargs Example in JAVA

import java.util.*;
import java.io.*;
class T
{
static void display(Object ... val)
{
for(Object o:val)
System.out.println(o);
}

public static void main(String args[])throws Exception
{
display();
display(45);
display(10,20.2f,30,"Hello");
display(45,43,67,23);
}
}

Pattern and Matcher Class Example

import java.util.regex.*;
class T
{
public static void main(String args[])
{
//Example of Regular Expression
Pattern p=Pattern.compile("[w|W]{3}[.][a-zA-Z0-9]+[.][a-zA-Z0-9]+");
Matcher m=p.matcher(args[0]);
System.out.println(m.matches());

m=m.usePattern(Pattern.compile("[w]{3}[.][a-zA-Z0-9]+[.][a-zA-Z0-9]+"));
System.out.println(m.matches());

//Example of Split Function
Pattern p1=Pattern.compile("[ @.]");
String str[]=p1.split(args[1]);

for(String s:str)
System.out.println(s);

System.out.println(Pattern.matches("ab", "aaaaab"));
}
}

TimeZone and its Methods in JAVA

import java.util.*;
class Tm 
{
public static void main(String[] args) 
{

TimeZone t1=TimeZone.getDefault();

System.out.println(t1);

System.out.println(t1.getID());

System.out.println(t1.getDisplayName());

System.out.println(t1.getOffset(new Date().getTime())/1000/60 + " " + "Minutes");

}
}

fill Method of Arrays Class in JAVA

import java.util.Arrays;
class Arr1
{
public static void main(String []args)
{
int a[]={1,2,3,4,5};

Arrays.fill(a,6);

System.out.println("After Arrays.fill with 2 Parameters");

for(int p:a)
System.out.println(p);

Arrays.fill(a,2,a.length,7);

System.out.println("After Arrays.fill with 4 Parameters");

for(int p:a)
System.out.println(p);
}
}

Multiple Class Initializer and Initializer Block in JAVA

Multiple Class Initializer and Initializer Block in JAVA

Today My one of the Student ask me questions as per following

1) Can we have Multiple Class Initializer and Initializer Block in One Class?
2) If Yes then What will be the Execution Sequence ?

Following Code will help you find answers.

class A 
{

//Class Initializer Block - 1 
static 
{
System.out.println("1");
}

//Class Initializer Block - 2
static
{
System.out.println("2");
}

//Initializer Block - 1 
{
System.out.println("3");
}


//Initializer Block - 2
{
System.out.println("4");
}

A()
{
System.out.println("5");
}

public static void main(String s[])
{
A a = new A();
}
}

Output :

E:\>java A
1
2
3
4
5

Object Class Functions

Object Class Functions

1) equals
2) toString
3) hashCode

class Cube
{

int h;

Cube()
{
h=3;
}

Cube(int w)
{
this.h=w;
}

}

class Rect extends Cube
{

int l,b;

Rect()
{
l=0;
b=0;
}

Rect(int i)
{
super(i);
this.l=i;
this.b=i;
}

Rect(int i, int j)
{
super(i);
this.l=i;
this.b=j;
}

Rect(int i, int j,int k)
{
super(k);
this.l=i;
this.b=j;
}

//toString() Overloading
public String toString()
{
 return " Length : "+l+" Breath : "+b;
}

//equals() overloading
public boolean equals (Object o)
{
return ((this.l * this.b)==(((Rect)o).l * ((Rect)o).b));
}

public static void main(String s[])
{
Rect r1 = new Rect(6);
Rect r2 = new Rect(12,3,6);
Rect r3 = new Rect(9,4);
Rect r4 = new Rect(6);

System.out.println(r1.equals(r2));
System.out.println(r2.equals(r3));
System.out.println(r3.equals(r1));
System.out.println(r1);
System.out.println(r2+ " " +r3);

//How to Use hashCode() Function
System.out.println(r1.hashCode());
System.out.println(r4.hashCode());
        
}
}

Anonymous Object in JAVA

Anonymous Object in JAVA

class A  {

int k;

A(int m)  {
k=m;
}

int getK() {
  return k;
}

}


class TestDemo {

public static void main(String s[])  {

System.out.println(new A(10).getK());

}

}

Watch Following Video to Learn in Detail :



Static Variable

Demonstrating Use of Static Variable.

class Student
{

int rollno=0;
static int boy=0;
static int girl=0;


        // Value 1-Boy 2-Girl Constructor
Student(int no)
{
if(no==1)
{
boy++;
rollno=boy + girl;
}
else
{
girl++;
rollno=boy + girl;
}

}

static void TotalObj()
{
System.out.println(boy + girl);
}

}

class C
{

public static void main(String s[])
{
Student sanjay=new Student(1);
Student amit=new Student(1);
Student sonali=new Student(2);
Student dipti=new Student(2);
System.out.println("Sanjay Roll No : "+sanjay.rollno);
System.out.println("Dipti Roll No : "+dipti.rollno);
System.out.println("Total Boys: " + sanjay.boy);
System.out.println("Total Girls: " + Student.girl );
System.out.print("Total Students : ");
Student.TotalObj();
}
}

Initializer and Class Initializer Block

class A
{

// Class Initializer Block - Will called First
static
{
System.out.println("Class Initializer Block Called");
}

// Initializer Block - Will called at time of Object Creation
{
System.out.println("Initializer Block Called");
}

public static void main(String s[])
{

System.out.println("Program Stated - Main Block Called");
A a=new A();

}
}

OUTPUT :

Class Initializer Block Called
Program Stated - Main Block Called
Initializer Block Called

Reverse the Words of String using JAVA

Reverse the Words of String using JAVA

class A1
{
public static void main(String args[])
{

//Input String
String text="gardi mca gtu sooadm java";

//Output String
String s1="";

int k2,k1,i=0,j=0;
//Set both pointer to End of String
k2=k1=text.length()-1;

//Loop for moving in String from END to START
for(i=k2;i>=0;i--,k1--)
{

//Checking for SPACE and First Character
 if(text.charAt(i)==' ' || i==0)
{

//Creating New String
for(j=(i==0)?0:i+1;j<=k2;j++)
{
s1=s1+text.charAt(j);
} // For Loop of j

s1=s1+' ';
k2=k1-1;

} // IF condition

} //For Loop of i

System.out.println(s1);
} // main

} //Class

Primitive Data Types Relations

Primitive Data Types Relations in JAVA


JAVA is following Big Endian

How can we Say that JAVA (JVM ) is following Big Endian Byte Ordering?

[* For Intel x86/x86_64 ]


class C1
{

public static void main(String args[])
{
int no=Integer.parseInt(args[0]);

Byte barray[] = new Byte[4];

barray[0] = (byte) ((no >> 24) & 0xFF);
barray[1] = (byte) ((no >> 16) & 0xFF);
barray[2] = (byte) ((no >> 8) & 0xFF);
barray[3] = (byte) (no & 0xFF);


for(int i=0;i<4;i++)
{
System.out.println(barray[i]);
}

}
}

How to Simulate System.out.println

How to Simulate System.out.println() ?

// Class A Will Work like PrintStream Class
class A
{
// printp Function will replace println()
public void printp()
{
System.out.println("Testing");
}
}

// Class B Will Work like System Class
class B
{
//Here a is like out
public static A a=new A();
}

class C
{
public static void main(String args[])
{
// System.out.println();
B.a.printp();
}
}

Watch Following Video to Learn in Detail :

What is System.out.println

What is System.out.println() ?

//Code is start from here
import java.io.*;

//Kapil will Replace the System
class Kapil 
{
//shukla will Replace the out
public static PrintStream shukla = new PrintStream(System.out);
}

class Txt
{
          public static void main(String args[])
{
           // Kapil.shukla can be used in place of System.out
           Kapil.shukla.println("Hello World");
}
}

Watch Following Video to Learn in Detail :


C Language is Following Little Endian Method

How can We Prove that C Language (Microprocessor) is Following Little-Endian Method to Store Values?
[* For Intel x86/x86_64 Not for BM Power PC Thanks Josh for Comment]

void main()

{

int i=9;


char *p;

clrscr();


p = (char) &i ;

printf("\n First Byte : %d",*p);


p++;


printf("\n Second Byte : %d",*p);

getch();


}


You will Get Answer 

First Byte : 9

Second Byte : 0

It proves that C is using Little-Endian.

Applet and Servlet

Applet:


  • An Applet is a specific type of ByteCode, which can be embedded inside a HTML page.
  • It can be downloaded and Run by a Web Browser.



Servlet:


  • The Servlet is a resource on the Web Server.
  • It is made up of Java ByteCode and would remain on Server.
  • It is not delivered to Client like the Applet.





Perfect Number

class perfect
{
    public static void main(String args[])
    {
    int no=Integer.parseInt(args[0]);
    int cn=0,i=0;
    for(i=1;i<no;i++)
       {
       if(no%i==0)
        {
        cn+=i;
        }
       }
  if(cn==Integer.parseInt(args[0]))
    {
    System.out.println("Given Number is Perfect");
    }
 else
   {
   System.out.println("
Given Number is Not Perfect");
   }
  }

}

How JAVA Program Executes

How JAVA Program Executes ?

How Java Program Executes ?
JVM: Java Virtual Machine

Bytecode :
  • Java Source file is first Compiled and the output of this process is known as Bytecode.
  • It is collection of Instruction Set.


History of JAVA

History of JAVA

Java Programming Language has syntax similar to C, but it is not an extension of C.

It was developed under “Project Green”.

“Project Green” was started at by SUN Developers Group under the leadership of “JAMES GOSLING”

JAMES GOSLING is known as a Father of JAVA.

Initially the New Language was called as “OAK” then due to Trademark issue with OAK Technologies it was renamed as “JAVA”

It was invented and written for Embedded System but somehow it was not become popular for Consumer Electronics in early days.

On 23rd May, 1995 when Sun Microsystem demonstrates the “Applet” in web browser, Java becomes Language for Web Development.

That Date (23rd May, 1995) is known as Birth date of JAVA.

JAVA is a General purpose Programming Language though it got success because of use in Web Development.

Count Occurrences of Characters in String - JAVA

Count Occurrences of Characters in String - JAVA


Make a Simple program which count the occurrences of Each Character in String . It will take one STRING as an INPUT.

Example:
INPUT    :   kapilshukla
OUTPUT : 
k – 2
a – 2
p – 1 ….

class prog2
{
public static void main(String args[])
{

char c[]=new char[10];

for(int i=0;i<args[0].length();i++)
{
        //This code will work if character is not @

if(args[0].charAt(i)!='@')
{

          //Count variable will count occurrences of Character 
int count=1;

for(int j=i+1;j<args[0].length();j++)
{
//This code will compare two character
if(args[0].charAt(i)==args[0].charAt(j))
{
count++;
}
}
       System.out.println(args[0].charAt(i)+ " - " + count);
}
      //This Code will replace all counted character with @
args[0] = args[0].replace(args[0].charAt(i), '@');
}
}

}

Calculator Application


Make a Simple Calculator using Command Line Argument in Java.

It will take input from Command Line in series of 
OPERAND – OPERATOR – OPERAND.

Example:

    INPUT : 15 + 29 – 30


    OUTPUT : 14
class first
{
public static void main(String args[])
{
int i=0;
int no;
no =Integer.parseInt(args[i++]);

                while(i<args.length)
{
if(args[i].equals("+"))
{
no = no + Integer.parseInt(args[i+1]);
}
else if(args[i].equals("-"))
{
no = no - Integer.parseInt(args[i+1]);
}
else if(args[i].equals("x"))
{
no = no * Integer.parseInt(args[i+1]);
}
else
{
no = no / Integer.parseInt(args[i+1]);
}
i = i + 2; 
}
System.out.println("\n Calculation : " + no);
}
}

OUTPUT :

E:\>javac first.java

E:\>java first 10 + 20 - 25


 Calculation : 5

Logic Building with JAVA

Date : 27 / 8 /2014


Logic Building with JAVA

Activity – I


Prepared by

Prof. Kapil Shukla




AIM of Activity
  • Team Building
  • Concreting Logic
  • Clear Presentation of Idea
  • Converting Logic into CODE
  • Better Understanding of Problem
  • Thinking about Alternative Solution

[1]
Calculator Application

Make a Simple Calculator using Command Line Argument in Java.

It will take input from Command Line in series of OPERANDOPERATOROPERAND.

Example:
   INPUT : 15 + 29 – 30
   OUTPUT : 14

[2]
Occurrences of Characters

Make a Simple program which count the occurrences of Each Character in String .

It will take one STRING as an INPUT.

Example:
   INPUT : kapilshukla
   OUTPUT :        k – 2
     a – 2
     p – 1 ….
[3]

Concatenate Strings and SORT 

Make a Simple program which concatenate two String and then Perform SORT operation on Final String.

It will take TWO STRINGS as an INPUT.

Example:
   INPUT : gardi mca
   OUTPUT : aacdgimr (IF possible – acdgimr)