LAB - 3 Revision LAB

JAVA LAB Program

1) Revise Concept of Static and Non-Static Properties of Class

2) Revise Concept of Initializer and Class Initializer Block

3) Revise Concept of "this".

4) Revise Concept of Constructor Overloading

Program:
Create Class Book with attributes like bookid, bookname, authorname, price etc.

Implement some methods like printBookInfo, printPrice…etc using appropriate parameter and return value.

Write 3 Constructors and implement fundamentals of Overloading.

Write one methods which counts no of objects which are created in program and print it.

LAB - 2 Initializer Block in JAVA

class Test
{
static
{
System.out.println("From Class Initializer Block");
}

{
System.out.println("From Initializer Block");
}

Test()
{
System.out.println("From Constructor");
}

public static void main(String a[])
{
System.out.println("START of MAIN");
Test t = new Test();
System.out.println("END of MAIN");
}
}

//OUTPUT
//E:\java\2015\LAB-2>java Test
//From Class Intializer Block
//START of MAIN
//From Intializer Block
//From Constructor
//END of MAIN

LAB - 1 JAVA Basic Fundamentals

JAVA LAB-1 Content


1) Basic Java Program "Hello World"

2) How to access normal Variable and Static Variable?

3) How to access Normal Function and Static Function?

4) How to add multiple Class in same program?

Common Program


class C
{
//Class without main()
static int k=20;
}

class A
{
//static variable can be accessed with Class Name
static int i=10;

//Normal Variable
int j=15;

public static void main(String args[])
{
//Print First Argument
System.out.println("String - 1 "+args[0]);
// How to Concatenate multiple string using +
                System.out.println(" i = " + i + "  String - 2" +args[1]);
}
}

class B
{
public static void main(String args[])
{
//Accessing variable of Class A using anonymous Object
System.out.println(" J = "+new A().j);

//Declaring Array of String
String s[]={"abc","def"};

//Calling main function of Class A with Parameter
A.main(s);

//Accessing static variable of Class C 
System.out.println(" C = "+C.k);
}
}

class D
{
public static void main(String args[])
{
//Calling main()
main();
}

//Overloading main()
public static int main()
{
System.out.println("Class D");
return 0;
}
}

class E
{
public static void main(String args[])
{
//Calling All main()
A.main(new String[2]);
B.main(new String[2]);
D.main(new String[2]);
}
}

How to Run & Compile JAVA Program ?


Compile :

Filepath > javac filename.java

Run :

Filepath > java classfilename

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