1
Hello World Example
compile by : javac Hello.java
and run by command : java Hello
class Hello {
public static void main(String[] args) {
System.out.println("I am N");
}
}
2:
public class Lab02 {
public static void main(String[] args) {
String st1="jlc",st2="jlc1",st3="jlc2";
System.out.println(st1);
System.out.println(st2);
System.out.println(st3);
int a=120,b=123,c=345;
System.out.println(a);
System.out.println(b);
System.out.println(c);
int d,e,f;
d=e=f=100;
System.out.println(d);
System.out.println(e);
System.out.println(f);
//int g=h=i=100;
//int g,int h,int i;
int g;int h;int i;
g=h=i=12;
System.out.println(g);
System.out.println(h);
System.out.println(i);
}
}
3:
class Hello{
boolean b1;
byte b2;
short s;
int i;
long l;
float f;
double d;
String str;
Hello h;
void show(){
System.out.println("boolean ="+ b1);
System.out.println("byte ="+b2);
System.out.println("short ="+s);
System.out.println("int ="+i);
System.out.println("long ="+l);
System.out.println("float ="+f);
System.out.println("double ="+d);
System.out.println("String ="+str);
System.out.println("Hello ="+h);
}
}
public class Lab03 {
public static void main(String[] args) {
Hello h=new Hello();
h.show();
}
}
4:
class Fello{
char ch;
void show(){
System.out.println(ch==0);
//System.out.println(ch=='');
System.out.println(ch=='\u0000');
}
}
public class Lab04 {
public static void main(String[] args) {
Fello h=new Fello();
h.show();
}
}
5:
public class Lab06 {
public static void main(String[] args) {
//boolean b=TRUE;
//boolean b1=True;
boolean b2=true;
//System.out.println(b);
//System.out.println(b1);
System.out.println(b2);
char ch=' ';
System.out.println(ch);
char ch1='1';
System.out.println(ch1);
char ch2='j';
System.out.println(ch2);
char ch3='+';
char ch4='%';
char ch5='"';
System.out.println(ch3+"\t"+ch4+"\t"+ch5);
int a=\u0037\u0030;
int b=\u0031;
int c='\u0032';
char ch6='\u0041';
char ch7='\u0042';
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(ch6);
System.out.println(ch7);
char ch8='\u0045'+'\u0042';
System.out.println(ch8);
int d=0xA;
System.out.println(d);
int e=0x61;
int f=0X61;
int g=0Xa;
int h=0XAFD;
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
int i=101;
int j=0101;
//int k=0b1010;//from java 7
boolean l=true;
//int m=0b0101;//from java 7
System.out.println(i);
System.out.println(j);
System.out.println(l);
double n=077;
double o=077.0;
double p=0xa9;
//double r=0xa9.0;
System.out.println(n);
System.out.println(o);
System.out.println(p);
double q=0XaP0;
double r=0xa9.0p0;
double s=0xa12.p0;
double t=0XA9p0;
double u=0xa9p1;
double v=0xa9p2;
double w=0xa9p3;
double x=0xa9p4;
double y=0xfp2d;
System.out.println(q);
System.out.println(r);
System.out.println(s);
System.out.println(t);
System.out.println(u);
System.out.println(v);
System.out.println(w);
System.out.println(x);
System.out.println(y);
float f1=0xf.ap01F;
float f2=0xaf.a5p1f;
float f3=0xa1F.01P2f;
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
String str=null;
Hello h1=null;
Hello h2=new Hello();
System.out.println(str);
System.out.println(h1);
System.out.println(h2);
int a1=2345678;
//int a2=1,34,56,781;
//int a2=1_23_56_781;//from java 7
System.out.println(a1);
char c1='A';
char c2='B';
//char c3=c1+c2;
int i1=c1+c2;
System.out.println(i1);
long l1=10+10L;
System.out.println(l1);
/* float f10=0/0;
System.out.println(f10);
float f11=0/0.0;
double d1=0/0.0;
System.out.println(d1);
float f11=0/0.0f;
System.out.println(f11);*/
int a10=90;
int a11=9;
System.out.println(a10+a11+"is result");
System.out.println("result is"+a10+a11);
System.out.println("sum is "+a10+(-a11));
// String str10=a10;
final int a12=90;
//String str11=a12;
String str11="a12";
System.out.println(a12);
System.out.println(str11);
System.out.println("------------");
String str12=a10+"";
System.out.println(str12);
String str13=""+a10;
System.out.println(str13);
int s1=a10+a12;
System.out.printf("\nSum of %d and %d is %d.\n",a10,a12,s1);
String dr1="G:\\Niraj\\Corejava";
System.out.println(dr1);
String dr2="G://Niraj//Corejava";
System.out.println(dr2);
String str14=str11+str12;
System.out.println(str14);
System.out.println(a10>>>60);
System.out.println(a10>>>50L);
System.out.println(60>>>a10);
System.out.println(a10>>>10);
System.out.println(100>>10);
System.out.println(23>>>12);
}
}
6:
public class Lab08 {
public static void main(String[] args) {
char ch='A';
for(int a=0;a<5;a++){
for(int b=0;b<=a;b++){
System.out.print(ch+" ");
//ch++;
}
System.out.println();
//ch++;
}
}}
7:
public class Lab09 {
public static void main(String[] args) {
char ch='A';
for(int a=0;a<5;a++){
for(int b=0;b<=a;b++){
System.out.print(ch+" ");
System.out.print(" ");
ch++;
}
System.out.println();
//ch++;
}
}}
8:
public class Lab10 {
public static void main(String[] args) {
int n=5;
char ch='A';
for(int r=5;r>=0;r--){
//for space
for(int c=0;c<r;c++){
System.out.print(" ");//one space
}
//for character
for(int c=0;c<n-r;c++){
System.out.print(ch+" ");
}
System.out.println();
ch++;
}
System.out.println("------------------------");
int n1=5;
char ch1='A';
for(int r1=5;r1>=0;r1--){
//for space
for(int c1=0;c1<n1-r1;c1++){
System.out.print(ch1+" ");//one space
}
//for character
for(int c1=0;c1<=r1;c1++){
System.out.print(" ");
}
System.out.println();
ch1++;
}}
}
9:
public class Lab11 {
public static void main(String[] args) {
int n=5,i=1;
while(i<=10){
System.out.println(n*i++);
}
System.out.println("===============================");
int a=5;
for(int b=1;b<=10;b++){
System.out.println(a*b);
}
System.out.println("================================");
int e=1;
for(int c=1;c<=10;c++){
System.out.println(e*c);
}
{e=2;
for(int c=1;c<=10;c++)
System.out.println(e*c);
System.out.println("--------------------------");
}
int f=4,g=0;
while(g++<10){
System.out.println(f*g);
}
System.out.println("***************************");
char ch='A';
int rows=0;
while(rows<5){
int cols=0;
while(cols<=rows){
System.out.print(ch+" ");
//System.out.println(" ");
cols++;
ch++;
}
System.out.println();
rows++;
//System.out.println(" ");
ch++;
}
System.out.println("+++++++++++++++++++++++");
int m=5;
char ch1='A';
for(int r=0;r<m;r++){
//for space
for(int c=0;c<m-r;c++){
System.out.print(" ");//two space
}
//for character
for(int c=0;c<=r;c++){
System.out.print(ch1+" ");
}
System.out.println();
ch1++;
}
System.out.println("...........................");
for(int r=0;r<m;r++){
//for space
for(int c=0;c<n-r;c++){
System.out.print(" ");//one space
}
//for character
for(int c=0;c<=r;c++){
System.out.print(ch+" ");
}
System.out.println();
ch++;
}
System.out.println("-------------------------------");
char ch2='A';
for(int p=5;p>=0;p--){
//for space
for(int q=0;q<m-p;q++){
System.out.print(" ");//one space
}
//for character
for(int q=0;q<=p;q++){
System.out.print(ch2+" ");
}
System.out.println();
ch2++;
}
System.out.println("...........................");
int m1=5;
char ch3='A';
for(int r1=5;r1>=1;r1--){
//for space
for(int c1=0;c1<m1-r1;c1++){
System.out.print(" ");//one space
}
//for character
for(int c1=0;c1<=r1;c1++){
System.out.print(ch3+" ");
}
System.out.println();
ch3++;
}
int m2=5;
char ch4='A';
for(int r2=0;r2<m2;r2++){
//for space
for(int c2=5;c2<1;c2--){
System.out.print(" ");//two space
}
//for character
for(int c2=0;c2<=r2;c2++){
System.out.print(ch4+" ");
}
System.out.println();
ch4++;
}
}
}
10:
public class Lab12 {
public static void main(String[] args) {
int a=3;
if(a==0)
System.out.println("Sunday");
else if(a==1)
System.out.println("Monday");
else if(a==2)
System.out.println("Tuesday");
else if(a==3)
System.out.println("Wednesday");
else if(a==4)
System.out.println("Thursday");
else if(a==5)
System.out.println("Friday");
else if(a==6)
System.out.println("Saturday");
else
System.out.println("Worng Input");
}
}
11:
public class Lab13 {
public static void main(String[] args) {
int a=4;
switch(a){
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
default:
System.out.println("Wrong Input");
}
}
}
12:
public class Lab14 {
public static void main(String[] args) {
/*long a=10;
//switch(a)
//switch(10)
switch(30){
case 10:
System.out.println("TEN");
case 30:
System.out.println("THIRTY");
}*/
/*final int arr[]=new int[2];
System.out.println("Len:"+arr.length);
//arr=new int[4];
//arr=null;
//arr.length=20;*/
/*int arr[]=new int[3];
System.out.println("Len 0:"+arr.length);
/*for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
arr=new int[5];
System.out.println("Len:"+arr.length);
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);*/
/*arr[0]=23;arr[1]=54;arr[2]=32;
int arr1[]=arr;
System.out.println("Len 1:"+arr.length);
System.out.println("Len 2:"+arr1.length);
arr=new int[6];
System.out.println("Len 3:"+arr.length);
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
System.out.println("Len 4:"+arr.length);
System.out.println("Len 5:"+arr1.length);
for(int i=0;i<arr1.length;i++)
System.out.println(arr1[i]);*/
/*final int arr[]=new int[2];
System.out.println("Len:"+arr.length);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(" ----------- ");
arr[0]=12;
arr[1]=23;
System.out.println(arr[0]);
System.out.println(arr[1]);*/
int arr[]=new int[3];
Object obj=arr;
int arr2[]=(int[])obj;
for(int i=0;i<arr2.length;i++)
System.out.println(arr2[i]);
}
}
13:
public class Lab15 {
public static void main(String[] args) {
int a=10;
//float f=30.0f;
//switch(f)
//switch(10.0f)
//switch(10.0)
//switch(30)
//>-switch(10)
switch(a)
{
case 10:
System.out.println("Ten");
case 30:
System.out.println("Thirty");
}
}
}
14:
public class Lab16 {
public static void main(String[] args) {
System.out.println("index\tvalue");
int arr[]={2,45,78,78,12,45,8,2,18};
for(int counter=0;counter<=arr.length;counter++){
System.out.println(counter+"\t"+arr[counter]);
}
}
}
15:
public class Lab17 {
public static void main(String[] args) {
Customer1 c1=new Customer1();
c1.show();
c1.cid=998;
c1.name="Raja";
c1.add="asfs";
}
}
class Customer1{
int cid;
String name;
String add;
void show(){
System.out.println(cid);
System.out.println(name);
System.out.println(add);
}}
16:
public class Lab18 {
public static void main(String[] args) {
Radha r1=new Radha();
r1.show();
r1.rid=87;
Customer c1=new Customer();
c1.Syso();
c1.cid=23;;
}
}
class Radha{
int rid;
String add;
void show(){
System.out.println(rid);
System.out.println(add);
}
}
class Customer extends Radha{
int cid;
String name="Raju";
String add1="Bal";
void Syso() {
System.out.println(name);
System.out.println(add1);
}}
17:
public class Lab19 {
static {
System.out.println("sb1");
}
public static void main(String... args)
//static public final synchronized strictfp void main(String... args)
{
System.out.println("Main :"+Hello1.a);
}
}
class Hello1{
static int a=10;
static Hello1 h=new Hello1();
{
System.out.println("IB");
}
static{
System.out.println("SB");
//static
int b;
b=0;
//static
String str="12";
System.out.println(b);
System.out.println(str);
}
class Hai{
//public static void main(String... args)
{
//System.out.println("Hai Main");
}
}
}
18:
import java.util.*;
class TestCollection1{
public static void main(String args[]){
/* ArrayList<String> al=new ArrayList<String>();//creating arraylist
al.add("Ravi");//adding object in arraylist
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements
while(itr.hasNext()){
System.out.println(itr.next());
} */
/*//class TestCollection2{
// public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
for(String obj:al)
System.out.println(obj);
}
}
*/
// import java.util.*;
// public class TestCollection3{
// public static void main(String args[]){
class Student{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
//Creating user-defined class objects
Student s1=new Student(101,"Sonoo",23);
Student s2=new Student(102,"Ravi",21);
Student s3=new Student(103,"Hanumat",25);
ArrayList<Student> al=new ArrayList<Student>();//creating arraylist
al.add(s1);//adding Student class object
al.add(s2);
al.add(s3);
Iterator itr=al.iterator();
//traversing elements of ArrayList object
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
19:
public class Lab01 {
public static void main(String[] args) {
Hai h1=new Hai();
System.out.println(h1.a);
System.out.println(h1.b);
System.out.println(h1.c);
//class Hello1{
//static
Hello1 h3=new Hello1();
{
System.out.println(h3.a);
System.out.println(h3.b);
System.out.println(h3.c);
}
Radha1 r1=new Radha1();
System.out.println(r1.a);
System.out.println(r1.b);
System.out.println(r1.c);
}
}
class Hai extends Hello1 {
//static
int a=12;
//int b=01;
//int c=02;
}
class Hello1 extends Radha1{
//static
int b=13;
}
class Radha1 {
//static
int c=14;
static int a=10;
static int b=15;
}
20:
public class Lab1{
public static void main(String[] args) {
/*BankA r1=new BankA();
r1.show();
r1.rid=87;
Customer c1=new Customer();
c1.Syso();
c1.name="Ram";
c1.add1="bal";
}
}
class Radha{
int rid;
String add;
void show(){
System.out.println("r1"+rid);
System.out.println("r1"+add);
}
class Customer extends BankA{
String name;
String add1;
private void show1() {
System.out.println("s1"+name);
System.out.println("s1"+add1);
}}}
class Customer{
String name;
String add1;
void Syso(){
System.out.println("S"+name);
System.out.println("S"+add1);
*/
}
}
public class BankA {
public static void main(String[] args) {
BankC bc=new BankC();
BankD bd=new BankD();
bc.Cid=01;
bd.Cid=02;
bc.Name="Radha";
bd.Name="Radhika";
bc.phone=8328382;
bd.phone=4453424;
bc.Add="Baloroe";
bd.Add="Kabjsnjas";
System.out.println(bc.Cid);
System.out.println(bc.Name);
System.out.println(bc.phone);
System.out.println(bc.Add);
System.out.println(bd.Cid);
System.out.println(bd.Name);
System.out.println(bd.phone);
System.out.println(bd.Add);
}
}
class BankB{
String Name;
int phone;
String Add;
int Cid;
void RecordCustomerB(int Cid,String Name,int phone,String Add){
}
}
class BankC extends BankB{
void RecordCustomerC(int Cid,String Name,int phone,String Add){
}
}
class BankD extends BankB{
void RecordCustomerD(int Cid,String Name,int phone,String Add){
}
}
21:
public class BankInterest {
public static void main(String[] args) {
//Bank b=new Bank();
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
//System.out.println("Bank rate of interest:"+b.RateOfInterest());
System.out.println("SBI rate of interest:"+s.RateOFInterest());
System.out.println("ICIC rate of interest:"+i.RateOfInterest());
System.out.println("Axis rate of interest:"+a.RateOfInterest());
}
}
class Bank{
int RateOfInterest(){
return 0;
}
}
class SBI extends Bank{
int RateOFInterest(){
return 8;
}
}
class ICICI extends Bank{
int RateOfInterest(){
return 7;
}
}
class AXIS extends Bank{
int RateOfInterest(){
return 9;
}
}
22:
abstract class HigherPerson {
public static void main(String[] args) {
Person per=null;
//per.sleeping();
per=new Student();
per.sleeping();
per.eating();
per=new Employee();
per.eating();
per.walking();
per.sleeping();
/*Person per=new Person();
per.walking();*/
}
}
abstract class Person{
void eating(){
System.out.println("Person eating");
}
void sleeping(){
System.out.println("Person Sleeping");
}
static void walking(){
System.out.println("Person Walking");
}
}
class Employee extends Person{
void working(){
System.out.println("Employee Working");
}
static void walking(){
System.out.println("Employee Walking");
}
void sleeping(){
System.out.println("Employee Sleeping");
}
}
class Student extends Person{
void sleeping(){
System.out.println("Student Sleeping");
}
void reading(){
System.out.println("Student Reading");
}
static void walking(){
System.out.println("Student Walking");
}
}
23:
public class Lab02 {
public static void main(String[] args) {
//System.out.println(a);
}}
class A{
//B b=new B();
//A a=new A();
//void show(){
int a=12;
//System.out.println(a);
}
//}
class B extends A{
//A a=new A();
static int a=13;
//System.out.println(a);
}
24:
public class Lab03 {
public static void main(String[] args) {
System.out.println("main");
E e=new E();
e.show();
F f=new F();
f.show();
}
}
class F{
void show(){
System.out.println("F");
}
}
class E extends F{
F f=new F();
void show1(){
System.out.println("E");
}
}
25:
public class Lab19 {
public static void main(String[] args) {
System.out.println("Main :"+Hello.a);
}
}
class Hello{
static int a=10;
//static Hello h;
//=new Hello();
{
System.out.println("IB");
}
static{
System.out.println("SB");
}
Hello h1;
static Hello h2;
{
System.out.println("h2");
System.out.println("h1");
}
static{
System.out.println("last");}
static Hello h;//=new Hello();
}
26:
class A1{
int x=10;
}
class B1 extends A1{
String x="jlc";
void show(){
System.out.println(x);
System.out.println(super.x);
}
}
public class Lab20 {
public static void main(String[] args) {
B1 b=new B1();
//A1 a=b;
b.show();
}
}
27:
class A2{
void show(int ab){
System.out.println("A2 Show()");
}
}
class B2 extends A2{
void show(int ab){
System.out.println("B2 show()");
}
}
public class Lab21 {
public static void main(String[] args) {
A2 ref=new A2();
ref.show(67);
}
}
28:
public class Lab22 {
public static void main(String[] args) {
String st1="jlc";
String st2=new String("jlc");
String st3=st2.intern();
System.out.println(st1);
System.out.println(st2);
System.out.println(st3);
System.out.println(st1==st2);
System.out.println(st1==st3);
System.out.println();
String st4="OK".intern();
System.out.println(st4);
String st5="OK";
System.out.println(st4==st5);
System.out.println(st5);
}
}
29:
public class X {
public static void main(String[] args) {
new Y().m1();
new Z().m1();
Z z=new Z();
z.m2();
Y y=new Y();
y.m1();
y.m2();
z.m1();
//Z.m1();
//Y.m1();
Y.m2();
Z.m2();
}
}
class Y{
void m1(){}
static void m2(){
}
}
class Z extends Y{
void m1(){}
static void m2(){}
}
30:
public class Lab01 {
public static void main(String[] args) {
/* C cobj=new C();
cobj.show();
cobj.x=true;
B bref=cobj;
bref.x="SRI";
A aref=cobj;
aref.x=88;
cobj.show();
}
}
class A{
int x=99;
}
class B extends A{
String x="jlc";
}
class C extends B{
boolean x=false;
void show(){
char x='j';
System.out.println(x);
C cref=this;
B bref=this;
A aref=this;
System.out.println(cref.x);
System.out.println(bref.x);
System.out.println(aref.x);*/
}
}
31:
abstract interface Inter{
void m1();
void m2();
int A=10;
int B=20;
}
/*interface Inter{
}*/
interface Inter1{
void m2();
void m3();
int A=12;
int C=30;
}
class Hello implements Inter,Inter1{
public void m1(){
System.out.println("Hello->m1()");
}
public void m2(){
System.out.println("Hello->m2()");
}
public void m3(){
System.out.println("Hello->m3()");
System.out.println(Inter.A);
System.out.println(Inter1.A);
System.out.println(B);
System.out.println(C);
}
}
/*class Hai extends Hello{
public void m3(){
System.out.println("Hai->m3()");
}
void m4(){
System.out.println("Hai->m4()");
}
}*/
public class Lab02 {
public static void main(String[] args) {
System.out.println("Hello Guys");
Hello h=new Hello();
Inter ref=h;
ref.m1();
ref.m2();
Inter1 ref1=h;
ref1.m2();
ref1.m3();
/*h.m1();
h.m2();
h.m3();
/*System.out.println(h.A);
System.out.println(h.B);
h.m2();
h.m1();
/*
Inter ref=null;
//ref=new Inter();
ref=new Hello();
ref.m1();
ref.m2();
/*ref=new Hai();
ref.m3();
ref.m4();
Inter ref1=new Hai();
ref1.m4;*/
}
}
33:
/*interface Inter{
int show();
}
/*interface Inter2{
int show();
}*/
class X {
public static void show(){
System.out.println("show method");
}
}
/*class Y extends X implements Inter1{
public void show(){}
}*/
/*public void show(){}
}
class Y implements Inter2{
public int show(){
return 0;
}}
class Z implements Inter1,Inter2{
public void show(){}
public int show(){}
}*/
public class Lab03 {
public static void main(String[] args) {
//Inter in=new X();
X x=new X();
x.show();
}
}
34:
class A{}
class B extends A{}
class C extends A{}
class D extends B{}
class E{}
public class Lab04 {
public static void main(String[] args) {
/*D dobj=new D();
A aobj=dobj;
B bobj=dobj;
//D dobj1=aobj;
B bobj1=(B)aobj;
D dobj1=(D)aobj;*/
A aobj=new D();
//C cobj=(C)aobj;
/*System.out.println(aobj instanceof A);
System.out.println(aobj instanceof B);
System.out.println(aobj instanceof C);
System.out.println(aobj instanceof D);
//System.out.println(aobj instanceof E);
*/
D dob=new D();
B bob=new B();
C cob=new C();
E eob=new E();
System.out.println(aobj==dob);
System.out.println(aobj==bob);
System.out.println(aobj==cob);
//System.out.println(aobj==eob);
//System.out.println(bob==cob);
}
}
35:
interface Inter3{
class Aa implements Inter3{}
class Bb extends Aa{}
class Cc extends Bb{}
}
public class Lab06 {
public static void main(String[] args) {
/*Inter3 arr[]=new Bb[3];
arr[0]=new Bb();
arr[1]=new Bb();
arr[2]=new Cc();*/
int arr[]=new int[7];
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
}
36:
class HelloA{
{System.out.println("IB HelloA");
class Inner{}
}
static{
System.out.println("SB-HelloA");
class Inner{}
}
HelloA(){
System.out.println("DC HelloA");
class Inner{}
}
void m1(){
System.out.println("m1() HelloA");
class Inner{}
}
}
public class Lab07 {
public static void main(String[] args) {
System.out.println("Hello Guys");
}
}
37:
class Outer{
void m1(){
//Inner1 ob1=new Inner1();
class Inner1{
void show(){
System.out.println("Inner1");
}
}
new Inner1().show();
//Inner1 ob1=new Inner1();
}
void m2(){
class Inner2{}
}
void m3(){
/*Inner1 ob1=new Inner1();
Inner2 ob2=new inner2();*/
}
}
public class Lab08 {
public static void main(String[] args) {
new Outer().m1();
}
}
38:
public class Lab09 {
public static void main(String[] args) {
/*showClassInfo("JLC");
Student stu=new Student();
showClassInfo(stu);
Object obj=new Object();
showClassInfo(obj);
}
static void showClassInfo(Object obj){
Class cls=obj.getClass();
System.out.println("Class Name :"+cls.getName());
Class scls=cls.getSuperclass();
if(scls!=null)
System.out.println("Super Class :"+scls.getName());
else
System.out.println("No Super Class");
}}
class Person{}
class Student extends Person{*/
}}
39:
public class Lab10 {
public static void main(String[] args) {
System.out.println("\n***With Student1");
Student1 st1=new Student1(99,767578687);
Student1 st2=new Student1(99,767578687);
Student1 st3=new Student1(88,289299986);
Student1 st4=st1;
System.out.println(st1.hashCode());
System.out.println(st2.hashCode());
System.out.println(st3.hashCode());
System.out.println(st4.hashCode());
System.out.println(st1==st2);
System.out.println(st1==st3);
System.out.println(st1==st4);
System.out.println(st3==st4);
}
}
class Student1{
int sid;
long phone;
Student1(int sid,long phone){
this.sid=sid;
this.phone=phone;
}
}
40:
class Student2{
int sid;
String name;
Student2(int sid,String name){
this.sid=sid;
this.name=name;
}
}
public class Lab1031 {
public static void main(String[] args) {
//Student2 st1=null;
//System.out.println(st1);
//st1=
Student2 st1=new Student2(99,"Sri");
System.out.println(st1);
System.out.println("\n**Defalut implementation");
String cname=st1.getClass().getName();
int hc=st1.hashCode();
System.out.println(hc);
System.out.println("hascode :"+hc);
System.out.println(st1.hashCode());
String hx=Integer.toHexString(hc);
System.out.println(/* "hexString :"+*/hx);
String msg=cname+"@"+hx;
System.out.println(msg);
/*String str=new String("Java Learning Center");
System.out.println(str);
Integer ref=new Integer(123);
System.out.println(ref);*/
}
}
41:
//import java.io.*;
public class Lab01 {
public static void main(String[] args)
{
/*//Object obj=new Object();
//Object obj1=obj.clone();
//System.out.println(obj==obj1);*/
Student1 st1=new Student1(11);
Student1 st2=st1.clone();
System.out.println(st1==st2);
System.out.println(st1.sid);
System.out.println(st2.sid);
}
}
class Student1 //implements Cloneable
{
int sid;
Student1(int sid){
this.sid=sid;
}
public Student1 clone(){
return new Student1(this.sid);
}
}
/*Hello h1=new Hello(99);
Hello h2=h1.clone();
System.out.println(h1==h2);
System.out.println(h1.a);
System.out.println(h2.a);
}}
class Hello{
int a;
Hello(int a){
this.a=a;
}
public Hello clone(){
return new Hello(this.a);
}
}*/
42:
class Hai{
public void finalize(){
System.out.println("Hai-Finalize");
}
}
class Hello{
void show(){
System.out.println("Hello-show()");
}
void m1(){
System.out.println("M1-statrt");
Hai h1=new Hai();
Hai h2=new Hai();
Hai h3=new Hai();
System.out.println(h1);
}
public void finalize(){
System.out.println("Hello-Finalize");
}
}
public class Lab02 {
public static void main(String[] args) {
new Hello().show();
Hello h1=new Hello();
h1=null;
Hello h2=new Hello();
Hello h3=new Hello();
h2=h3;
new Hello().m1();
//System.runFinalization();
System.gc();
}
}
43:
public class Lab03 {
public static void main(String[] args) {
Employee emps[]=new Employee[500];
for(int i=0;i<emps.length;i++){
//new Employee("jp-"+(i+1));
emps[i]=new Employee("jp-"+(i+1));
}
}
}
class Employee{
String eid;
double add[]=new double[123456];
Employee(String eid){
this.eid=eid;
System.out.println("\nObject Created with id :"+eid);
}
protected void finalize(){
System.out.println("Finalize :"+eid);
}
}
44:
public class Lab04 {
public static void main(String[] args) {
Student4 st=new Student4(88);
st.finalize();
System.out.println("Sid :"+st.sid);
}
}
class Student4{
int sid;
Student4(int sid){
this.sid=sid;
}
public void finalize(){
System.out.println("Finalize :"+sid);
}
}
45:
public class Lab05 {
public static void main(String[] args) {
Student5 st=new Student5("jp-001");
new Student5("jp-002");
System.runFinalizersOnExit(true);
//System.runFinalizersOnExit(false);
System.out.println("Main Completed");
}
}
class Student5{
String sid;
Student5(String sid){
this.sid=sid;
System.out.println("Object Created with id :"+sid);
}
protected void finalize(){
System.out.println("Finalize :"+sid);
}}
46:
public class Lab07 {
public static void main(String[] args) {
Student7 st=new Student7(22);
//new Student(10);
st.finalize();
//System.runFinalizersOnExit(true);
System.out.println("Sid :"+st.sid);
}
}
class Student7{
int sid;
Student7(int sid){
this.sid=sid;
}
public void finalize(){
System.out.println("Finalize :"+sid);
}
}
47:
public class Lab08 {
public static void main(String[] args) {
String st1="jlc";
String st2="jlc";
String st3="jlc";
String st4=st1;
String st5=new String();
new String("jlc");
System.out.println(st1==st2);
System.out.println(st1!=st3);
System.out.println(st4==st1);
System.out.println(st2==st5);
System.out.println(st3==st5);
}
}
48:
public class Lab09 {
public static void main(String[] args) {
showClassInfo("JLC");
Student stu=new Student();
showClassInfo(stu);
Object obj=new Object();
showClassInfo(obj);
}
static void showClassInfo(Object obj){
Class cls=obj.getClass();
System.out.println("Class Name :"+cls.getName());
Class scls=cls.getSuperclass();
if(scls!=null)
System.out.println("Super Class :"+scls.getName());
else
System.out.println("No Super Class");
}}
class Person{}
class Student extends Person{
}
49:
class A1{
public void show(){
System.out.println("super");
}
}
class B1 extends A1{
public void show(){
System.out.println("In child");
}
}
public class Lab10 {
public static void main(String[] args) {
A1 obj=new B1();
obj.show();
}
}
50:
public class Lab11 {
public static void main(String[] args) {
Stud st=new Stud("sri");
Emp em=new Emp("sri");
System.out.println(st.snm==em.enm);
st.show(em);
}
}
class Stud{
String snm;
Stud(String snm){
this.snm=snm;
}
void show(Emp em){
String msg="sri";
System.out.println(em.enm==msg);
System.out.println(em.enm==snm);
}
}
class Emp{
String enm;
Emp(String enm){
this.enm=enm;
}
void display(Stud st){
String var="sri";
System.out.println(st.snm==var);
System.out.println(st.snm==enm);
}
}
51:
class A2{
public void show(){
System.out.println("super");
}
}
class B2 extends A2{
public void show(){
System.out.println("child");
}
public void display(){
System.out.println("display");
}
}
public class Lab12 {
public static void main(String[] args) {
A2 obj=new B2();
obj.show();
//obj.display();
}
}
52:
public class Lab13 {
public static void main(String[] args) {
Stud1 st=new Stud1("sri");
Emp1 em=new Emp1("sri");
System.out.println("main");
System.out.println("main :"+(st.snm==em.enm));
st.show(em);
}
}
class Stud1{
String snm;
Stud1(String snm){
this.snm=snm;
}
void show(Emp1 em){
String msg="sri";
System.out.println("Stud1.show() :"+(em.enm==msg));
System.out.println("Stud1.show() :"+(em.enm==snm));
em.display(this);
}
}
class Emp1{
String enm;
Emp1(String enm){
this.enm=enm;
}
void display(Stud1 st){
String var="sri";
System.out.println("Emp1.dispaly()");
System.out.println("Emp1.dis :"+(st.snm==var));
System.out.println("Emp1.dis :"+(st.snm==enm));
}
}
53:
public class Lab14 {
public static void main(String[] args) {
String str="jlc,java learning center." +
"No 1 java training and placement." +
"java is papular langauge.";
String res[]=str.split("java",1);
System.out.println("Length :"+res.length);
for(int i=0;i<res.length;i++){
String st=res[i];
System.out.println(i+"\t"+st);
}
System.out.println("******************************");
String res1[]=str.split("java",2);
System.out.println("Length :"+res1.length);
for(int i=0;i<res1.length;i++){
String st=res1[i];
System.out.println(i+"\t"+st);
}
System.out.println("******************************");
String res2[]=str.split("java",3);
System.out.println("Length :"+res2.length);
for(int i=0;i<res2.length;i++){
String st=res2[i];
System.out.println(i+"\t"+st);
}
System.out.println("******************************");
String res3[]=str.split("java",4);
System.out.println("Length :"+res3.length);
for(int i=0;i<res3.length;i++){
String st=res3[i];
System.out.println(i+"\t"+st);
}
System.out.println("******************************");
String res4[]=str.split("java",5);
System.out.println("Length :"+res4.length);
for(int i=0;i<res4.length;i++){
String st=res4[i];
System.out.println(i+"\t"+st);
}
}
}
54:
public class Lab15 {
public static void main(String[] args) {
String str="Hello world I am Niraj";
System.out.println(str);
String dir="G:\\set.txt";
String res[]=dir.split("\\\\");
for(int i=0;i<res.length;i++){
String st=res[i];
System.out.println(i+"\t"+st);
}
}
}
55:
public class Lab16 {
public static void main(String[] args) {
String st1="AB";
String st2=new String("AB");
System.out.println(st1.hashCode());
System.out.println(st2.hashCode());
System.out.println(st1==st2);
}
}
56:
public class Lab17 {
public static void main(String[] args) {
String st1="Website is www.jlcindia.com";
String st2="info@jlcindia.com is id to send email";
boolean b1=st1.regionMatches(2,st2, 5,10);
System.out.println(b1);
boolean b2=st1.regionMatches(15, st2, 5,9);
System.out.println(b2);
}
}
57:
public class Lab18 {
public static void main(String[] args) {
short s1=128;
short s2=130;
short s3=128;
int a1=120;
int a2=130;
Integer a1i=new Integer(a1);
Integer a2i=new Integer(a2);
Short sref1=new Short(s1);
Short sref2=new Short(s2);
Short sref3=new Short(s3);
System.out.println("compare");
System.out.println(sref1.compareTo(sref1));
System.out.println(sref1.compareTo(sref2));
System.out.println(sref1.compareTo(sref3));
System.out.println("--Compare(short,short)--");
System.out.println(Short.compare(sref1,sref1));
System.out.println(Short.compare(sref1,sref2));
System.out.println(Short.compare(sref1,sref3));
System.out.println("hashCode()");
System.out.println(sref1.hashCode());
System.out.println(sref2.hashCode());
System.out.println(sref3.hashCode());
System.out.println(a1i.hashCode());
System.out.println(a2i.hashCode());
System.out.println("--equals()--");
System.out.println(sref1.equals(sref2));
System.out.println(sref1.equals(sref3));
}
}
58 :
public class Lab19 {
public static void main(String[] args) {
int i=Integer.parseInt("101",2);
System.out.println(i);
int ref=Integer.valueOf("101",2);
System.out.println(ref);
int i1=Integer.parseInt("101",8);
System.out.println(i1);
int ref1=Integer.valueOf("101",8);
System.out.println(ref1);
System.out.println(Character.isMirrored('a'));
System.out.println(Character.isMirrored('A'));
System.out.println(Character.isMirrored(']'));
System.out.println(Character.isMirrored('('));
Character c1=new Character('A');
System.out.println(c1.hashCode());
int in1=123;
int in2=124;
System.out.println(in1==in2);
in1++;
System.out.println(in1==in2);
in1++;
System.out.println(in1==in2);
in2++;
System.out.println(in1==in2);
++in1;
System.out.println(in1==in2);
System.out.println(++in1==in1++);
System.out.println(++in1==in1--);
System.out.println(++in1==--in1);
System.out.println(in1++==++in1);
System.out.println(in1++==in1);
System.out.println(in1++==in1++);
System.out.println(in1);
}
}
public class Lab19 {
public static void main(String[] args) {
int i=Integer.parseInt("101",2);
System.out.println(i);
int ref=Integer.valueOf("101",2);
System.out.println(ref);
int i1=Integer.parseInt("101",8);
System.out.println(i1);
int ref1=Integer.valueOf("101",8);
System.out.println(ref1);
System.out.println(Character.isMirrored('a'));
System.out.println(Character.isMirrored('A'));
System.out.println(Character.isMirrored(']'));
System.out.println(Character.isMirrored('('));
Character c1=new Character('A');
System.out.println(c1.hashCode());
int in1=123;
int in2=124;
System.out.println(in1==in2);
in1++;
System.out.println(in1==in2);
in1++;
System.out.println(in1==in2);
in2++;
System.out.println(in1==in2);
++in1;
System.out.println(in1==in2);
System.out.println(++in1==in1++);
System.out.println(++in1==in1--);
System.out.println(++in1==--in1);
System.out.println(in1++==++in1);
System.out.println(in1++==in1);
System.out.println(in1++==in1++);
System.out.println(in1);
}
}
59:
import java.util.*;
public class Lab21 {
public static void main(String[] args) {
System.out.println("\n***Accessing All Properties");
Properties p=System.getProperties();
Enumeration enm=p.propertyNames();
while(enm.hasMoreElements()){
String pName=enm.nextElement().toString();
String val=System.getProperty(pName);
System.out.println(pName+"\t:\t"+val);
}
}
}
60:
import java.util.*;
import java.io.*;
public class Lab22 {
public static void main(String[] args)throws Exception {
Object ref1=new int[]{10,21,13,14,15,12};
Object ref2=new int[]{3,12,61,41,51,21};
int obj1[]=new int[6];
System.arraycopy(ref1, 0, obj1, 0, 6);
System.out.println("First Array Data");
for(int i=0;i<obj1.length;i++){
System.out.println(i+"\t"+obj1[i]);
}
System.out.println("Second Array Data");
System.arraycopy(ref2, 0, obj1, 0, 6);
for(int i=0;i<obj1.length;i++){
System.out.println(i+"\t"+obj1[i]);
}
int arr1[]={23,12,15,14};
String st[]=new String[5];
System.arraycopy(arr1, 0, st, 0, 4);
}
}
61:
public class Lab23 {
public static void main(String[] args) {
Runtime rt=null;
//rt=new Runtime();
System.out.println(Runtime.getRuntime());
System.out.println(Runtime.getRuntime());
rt=Runtime.getRuntime();
System.out.println("AP :"+rt.availableProcessors());
System.out.println("MM :"+rt.maxMemory());
System.out.println("TM :"+rt.totalMemory());
System.out.println("FM :"+rt.freeMemory());
}
}
62:
public class Lab24 {
public static void main(String[] args) {
System.out.println(Math.E);
System.out.println(Math.PI);
System.out.println(Math.sqrt(16));
System.out.println(Math.pow(3, 4));
System.out.println(Math.abs(-44));
System.out.println(Math.ceil(10.2));
System.out.println(Math.ceil(10.5));
System.out.println(Math.ceil(10.9));
System.out.println(Math.cbrt(11.1));
System.out.println(Math.floor(10.2));
System.out.println(Math.floor(10.5));
System.out.println(Math.floor(10.9));
System.out.println(Math.floor(11.1));
System.out.println(Math.round(11.0));
System.out.println(Math.round(11.2));
System.out.println(Math.round(11.5));
System.out.println(Math.round(11.9));
System.out.println(Math.round(12.1));
}
}
63:
public class Lab25 {
public static void main(String[] args) {
for(int i=0;i<10;i++){
System.out.println(Math.random());
}
System.out.println("\n*******************");
for(int i=0;i<10;i++){
System.out.println(10*Math.random());
}
}
}
64:
public class Lab26 {
public static void main(String[] args) {
Hello1 h=Hello1.getHello1();
System.out.println(h);
System.out.println(Hello1.getHello1());
System.out.println(Hello1.getHello1());
Hello1 h2=(Hello1)h.clone();
System.out.println(h2);
}
}
class Hello1{
private static Hello1 INS=null;
/*static{
INS=new Hello1();
}*/
private Hello1(){}
public static Hello1 getHello1(){
if(INS==null)
INS=new Hello1();
return INS;
}
protected Object clone(){
return this;
}
}
65:
class Hello2{
void m1()throws ArithmeticException,ArrayIndexOutOfBoundsException{
try{
m2();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("m1-ok :"+e);
if(1==2){
throw new ArithmeticException("Don't do this");
} }}
void m2()throws ArrayIndexOutOfBoundsException{
m3();
}
void m3()throws ArrayIndexOutOfBoundsException{
try{
m4();
}catch(ArithmeticException e){
System.out.println("m3()-ok :"+e);
throw new ArrayIndexOutOfBoundsException();
}
}
void m4()throws ArithmeticException{
m5();
}
void m5()throws ArithmeticException{
int x=10/0;
}
}
public class Lab27 {
public static void main(String[] args) {
try{
Hello2 h=new Hello2();
h.m1();
}catch(Exception e){
e.printStackTrace();
}
}
}
66:
public class Lab28 {
public static void main(String[] args) {
System.out.println("Main Started");
JLCResource res1=null;
JLCResource res2=null;
try{
res1=new JLCResource(1);
res2=new JLCResource(2);
}catch(Exception e){
System.out.println(e);
}finally{
if(res2!=null)
res2.close();
if(res1!=null)
res1.close();
}
System.out.println("Main Ends");
}}
class JLCResource implements AutoCloseable{
int id;
JLCResource(int id){
this.id=id;
}
public void close(){
System.out.println("JLCResource closed :"+id);
}
}
67:
import java.util.*;
public class Lab29 {
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
System.out.println(al);
System.out.println("******************");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
68:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Lab30 extends Panel{
public static void main(String[] args) {
//Graphics p=new Graphics(awt);
}
public void paintComponant(Graphics g){
super.paintComponents(g);
this.setBackground(Color.MAGENTA);
g.setColor(Color.BLACK);
g.fillRect(10, 20, 50, 25);
g.setColor(new Color(120,51,78));
g.fillRect(10,25,50,25);
g.setColor(Color.BLUE);
g.drawString("Some text is there", 20,30);
}
}
69:
public class Lab31 {
public static void main(String[] args) throws CloneNotSupportedException{
Student6 st=new Student6(22);
System.out.println(st.sid);
Student6 st2=st.myclone();
System.out.println(st2.sid);
System.out.println(st==st2);
}
}
interface JLCCloneable{}
class Student6 implements JLCCloneable{
int sid;
Student6(int sid){
this.sid=sid;
}
public Student6 myclone()throws CloneNotSupportedException{
if(this instanceof JLCCloneable)
return new Student6(sid);
else
throw new CloneNotSupportedException("implement JLCCloneable");
}
}
70:
import java.util.*;
public class Lab32 {
public static void main(String[] args) {
Vector v=new Vector();
List list=new ArrayList();
list.add("Sri");
list.add("radha");
list.add("raju");
v.add("rani");
v.addElement("ra");
v.addElement("rah");
v.addElement("rajuni");
Enumeration enu=v.elements();
while(enu.hasMoreElements())
System.out.println(enu.nextElement());
System.out.println("**************************");
ListIterator lit=list.listIterator();
while(lit.hasNext()){
Object obj=lit.next();
System.out.println(obj);
}
}
}
71:
import java.util.*;
public class lab33 {
public static void main(String[] args) {
Map map=new LinkedHashMap();
map.put("eid", new Integer(99));
map.put("name", "Srinivas");
map.put("phone", new Long(412313563L));
map.put("vaild", new Boolean(true));
/*System.out.println(map);
System.out.println("remove(eid)"+map.remove("eid"));
System.out.println(map);
System.out.println("put-phone "+map.put("phone",new Long(4534544546L)));
System.out.println(map);
System.out.println("get()");
System.out.println(map.get("name"));
System.out.println(map.get("email"));*/
/*System.out.println("\n***Keys");
Set keys=map.keySet();
Iterator it=keys.iterator();
while(it.hasNext()){
Object key=it.next();
System.out.println(key);
}
System.out.println("\n***Values");
Collection col=map.values();
Iterator it1=col.iterator();
while(it1.hasNext()){
Object val=it1.next();
System.out.println(val);*/
System.out.println("\n**Keys and Values");
Set data=map.entrySet();
Iterator it=data.iterator();
while(it.hasNext()){
Object obj=it.next();
Map.Entry entry=(Map.Entry)obj;
Object key=entry.getKey();
Object val=entry.getValue();
System.out.println(key+"\t\t" +val);
}
}
}
72:
import java.util.*;
public class Lab34 {
public static void main(String[] args) {
Date dt=new Date();
System.out.println(dt);
System.out.println("Date\t :"+dt.getDate());
System.out.println("Mon\t :"+dt.getMonth());
System.out.println("Mon+1\t :"+(dt.getMonth()+1));
System.out.println("Year\t :"+dt.getYear());
System.out.println("Year+1900\t :"+(dt.getYear()+1900));
System.out.println("Hours\t :"+dt.getHours());
System.out.println("Minute\t :"+dt.getMinutes());
System.out.println("Sec\t :"+dt.getSeconds());
System.out.println("\nStoring date");
dt.setDate(3);
dt.setMonth(1);
dt.setYear(105);
System.out.println(dt);
Date dt1=new Date(112,12,25);
Date dt2=new Date(112,6,25);
System.out.println("\ndt1 :"+dt1);
System.out.println("\ndt2 :"+dt2);
System.out.println("\ndt1.compareTo(dt2) :"+dt1.compareTo(dt2));
System.out.println("\ndt2.compareTo(dt1) :"+dt2.compareTo(dt1));
System.out.println("\ndt1.after(dt2) :"+dt1.after(dt2));
System.out.println("\ndt1.before(dt2) :"+dt1.before(dt2));
}
}
73:
import java.util.*;
public class Lab35 {
public static void main(String[] args) {
System.out.println("About to Schedule task");
new Reminder(4);
}
}
class Reminder{
Timer timer;
public Reminder(int seconds){
timer=new Timer();
timer.schedule(new RemindTask(),seconds*1000);
}
class RemindTask extends TimerTask{
public void run(){
System.out.println("Time's Up!");
timer.cancel();
}
}
}
74:
public class Student3 implements Cloneable{
int sid;
Student3(int sid){
this.sid=sid;
}
public static void main(String[]args)throws Exception{
Student3 st1=new Student3(11);
Student3 st2=(Student3)st1.clone();
System.out.println(st1==st2);
System.out.println(st1.sid);
System.out.println(st2.sid);
}
}
75:
class TestExceptionPropagation1{
void m(){
int data=50/0;
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal flow...");
}
}
76:
import java.io.*;
class Lab01{
public static void main(String args[]){
System.out.println("Enter data");
try{
BufferedInputStream bis=new BufferedInputStream(System.in);
while(true){
int asc=bis.read();
if(asc==13)break;
char ch=(char)asc;
System.out.println(ch);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
77:
import java.io.*;
class Lab02{
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("abc.txt");
String s="Sachin Tendulkar is my favourite player";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
}catch(Exception e)
{System.out.println(e);}
}
}
78:
import java.io.*;
class Lab03{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("abc.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
79:
import java.io.*;
class Lab04{
public static void main(String args[])throws Exception{
FileInputStream fin=new FileInputStream("abc.txt");
FileOutputStream fout=new FileOutputStream("M.java");
int i=0;
while((i=fin.read())!=-1){
fout.write((byte)i);
}
fin.close();
}
}
80:
import java.io.*;
class Lab05{
public static void main(String args[])throws Exception{
FileOutputStream fout1=new FileOutputStream("f1.txt");
FileOutputStream fout2=new FileOutputStream("f2.txt");
ByteArrayOutputStream bout=new ByteArrayOutputStream();
bout.write(139);
bout.writeTo(fout1);
bout.writeTo(fout2);
bout.flush();
bout.close();//has no effect
System.out.println("success...");
}
}
81:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Lab06 extends JPanel{
public void paintComponant(Graphics g){
super.paintComponents(g);
this.setBackground(Color.MAGENTA);
g.setColor(Color.BLACK);
g.fillRect(10, 20, 50, 25);
g.setColor(new Color(120,51,78));
g.fillRect(10,25,50,25);
g.setColor(Color.BLUE);
g.drawString("Some text is there", 20,30);
}
}
82:
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
class Lab07{
public static void main(String args[])throws Exception{
FileOutputStream fout1=new FileOutputStream("f1.txt");
FileOutputStream fout2=new FileOutputStream("f2.txt");
ByteArrayOutputStream bout=new ByteArrayOutputStream();
bout.write(139);
bout.writeTo(fout1);
bout.writeTo(fout2);
bout.flush();
bout.close();//has no effect
System.out.println("success...");
}
}
83:
import java.io.*;
class Lab08{
public static void main(String args[])throws Exception{
FileInputStream fin1=new FileInputStream("f1.txt");
FileInputStream fin2=new FileInputStream("f2.txt");
SequenceInputStream sis=new SequenceInputStream(fin1,fin2);
int i;
while((i=sis.read())!=-1){
System.out.println((char)i);
}
sis.close();
fin1.close();
fin2.close();
}
}
84:
import java.io.*;
class Lab09{
public static void main(String args[])throws Exception{
FileInputStream fin1=new FileInputStream("f1.txt");
FileInputStream fin2=new FileInputStream("f2.txt");
FileOutputStream fout=new FileOutputStream("f3.txt");
SequenceInputStream sis=new SequenceInputStream(fin1,fin2);
int i;
while((i=sis.read())!=-1)
{
fout.write(i);
}
sis.close();
fout.close();
fin1.close();
fin2.close();
}
}
85:
import java.io.*;
import java.util.*;
class Lab10{
public static void main(String args[])throws IOException{
//creating the FileInputStream objects for all the files
FileInputStream fin=new FileInputStream("a.java");
FileInputStream fin2=new FileInputStream("abc2.txt");
FileInputStream fin3=new FileInputStream("abc.txt");
FileInputStream fin4=new FileInputStream("B.java");
//creating Vector object to all the stream
Vector v=new Vector();
v.add(fin);
v.add(fin2);
v.add(fin3);
v.add(fin4);
//creating enumeration object by calling the elements method
Enumeration e=v.elements();
//passing the enumeration object in the constructor
SequenceInputStream bin=new SequenceInputStream(e);
int i=0;
while((i=bin.read())!=-1){
System.out.print((char)i);
}
bin.close();
fin.close();
fin2.close();
}
}
86:
import java.io.*;
class Lab11{
public static void main(String args[])throws Exception{
CharArrayWriter out=new CharArrayWriter();
out.write("my name is");
FileWriter f1=new FileWriter("a.txt");
FileWriter f2=new FileWriter("b.txt");
FileWriter f3=new FileWriter("c.txt");
FileWriter f4=new FileWriter("d.txt");
out.writeTo(f1);
out.writeTo(f2);
out.writeTo(f3);
out.writeTo(f4);
f1.close();
f2.close();
f3.close();
f4.close();
}
}
87:
import java.io.*;
class Lab12{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter your name");
String name=br.readLine();
System.out.println("Welcome "+name);
}
}
88:
import java.io.*;
class Lab13{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
//String name="";
String name=br.readLine();
while(name.equals("stop")){
System.out.println("Enter data: ");
//String name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}
89:
import java.io.*;
public class Lab14 {
public static void main(String[] args)throws Exception {
try{
FileOutputStream fos=new FileOutputStream("G:\\Info.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);{
Address ad=new Address("33/1","Mathikere");
Student st=new Student(99,"Sri",541312534,ad);
Student.count=9;
System.out.println(st);
oos.writeObject(st);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Address{
String sid;
String street;
Address(String sid,String street){
this.sid=sid;
this.street=street;
}
}
class Student implements Serializable{
int sid;
transient String name="XXXX";
long phone=11213513;
static int count=3;
Address add;
Student(int sid,String name,long phone,Address add){
this.sid=sid;
this.name=name;
this.phone=phone;
this.add=add;
}
public String toString(){
return sid+"\t"+name+"\t"+phone+"\t"+add;
}
}
90:
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Lab15 {
public static void main(String[] args)throws Exception {
try{
FileOutputStream fos=new FileOutputStream("G:\\Info.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);{
Student1 st=new Student1(99,"Sri",541312534);
Student1.count=9;
System.out.println(st);
oos.writeObject(st);
System.out.println("object Serialized");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Student1 implements Serializable{
int sid;
transient String name="XXXX";
long phone=11213513;
static int count=3;
Student1(int sid,String name,long phone){
this.sid=sid;
this.name=name;
this.phone=phone;
}
public String toString(){
return sid+"\t"+name+"\t"+phone;
}
}
91:
import java.io.*;
public class Lab16 {
public static void main(String[] args)throws Exception {
try{
FileOutputStream fos=new FileOutputStream("G:\\Info.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);{
Address1 ad=new Address1("33/1","Mathikere");
Student2 st=new Student2(99,"Sri",541312534,ad);
Student2.count=9;
System.out.println(st);
oos.writeObject(st);
System.out.println("Object Serialized");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
class Address1{
String aid;
String street;
Address1(String sid,String street){
this.aid=sid;
this.street=street;
}
public String toString(){
return aid+"\t"+street;
}
}
class Student2 implements Externalizable{
int sid;
transient String name="XXXX";
long phone=11213513;
static int count=3;
Address1 add;
public Student2(){}
Student2(int sid,String name,long phone,Address1 add){
this.sid=sid;
this.name=name;
this.phone=phone;
this.add=add;
}
public String toString(){
return sid+"\t"+name+"\t"+phone+"\t"+add;
}
public void writeExternal(ObjectOutput out)throws IOException{
out.write(count);
out.writeObject(name);
out.writeObject(add.aid);
out.writeObject(add.street);
}
public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException{
count=in.readInt();
name=in.readObject().toString();
//add=new Address1();
add.aid=in.readObject().toString();
add.street=in.readObject().toString();
}
}
92:
import java.util.*;
public class Lab17 {
public static void main(String[] args) {
Collection col=new ArrayList();
System.out.println(col);
System.out.println("Size :"+col.size());
System.out.println("Empty :"+col.isEmpty());
System.out.println();
col.add("Sri");
col.add("Nivash");
col.add("Dande");
col.add("Sd");
Object arr[]=col.toArray();
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
arr[i]="jlc";
}
System.out.println("1 :"+col);
System.out.println("2 :"+col.contains("Srinivas"));
System.out.println("3 :"+col.contains("Sri"));
System.out.println("4 :"+col.contains("Radha"));
System.out.println("5 :"+col);
System.out.println("6 :"+col.contains("Srinivas"));
System.out.println("7 :"+col.contains("Sd"));
System.out.println("8 :"+col);
System.out.println("Size :"+col.size());
System.out.println("Empty :"+col.isEmpty());
col.clear();
System.out.println("9 :"+col);
System.out.println("Size :"+col.size());
}
}
93:
public class Lab18 {
public static void main(String[] args) {
System.out.println("Main Started");
Thread th=Thread.currentThread();
ThreadGroup tg=th.getThreadGroup();
System.out.println(th.getId()+"\t"+th.getName()+"t"+tg.getName());
for(int i=0;i<args.length;i++){
String st=args[i];
System.out.println(th.getId()+"t"+th.getName()+"t"+tg.getName()+"t"+st);
}
System.out.println("Main Completed");
}
}
94:
public class Lab19 {
public static void main(String[] args) {
MyThread t1=new MyThread();
MyThread t2=new MyThread();
t1.start();
t2.start();
Thread t=Thread.currentThread();
for(int i=100;i>=90;i--){
System.out.println(t.getName()+"-value is :"+i);
}
}
}
class MyThread extends Thread{
public void run(){
Thread th=Thread.currentThread();
for(int i=20;i>=10;i--){
System.out.println(th.getName()+"-value is :"+i);
try{
Thread.sleep(100);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
95:
public class Lab20 {
public static void main(String[] args) {
MyThread1 t1=new MyThread1();
MyThread1 t2=new MyThread1();
}
}
class MyThread1 extends Thread{
MyThread1(){
//start();
Thread t=new Thread(this);
t.start();
}
public void run(){
Thread th=Thread.currentThread();
ThreadGroup tg=th.getThreadGroup();
System.out.println("Thread Name :"+th.getName());
System.out.println("Thread Group Name :"+tg.getName());
}
}
96:
public class Lab21 {
public static void main(String[] args) {
MyThread2 t1=new MyThread2("Hello");
MyThread2 t2=new MyThread2("Hai");
}
}
class MyThread2 extends Thread{
MyThread2(String tname){
super(tname);
//Thread t=new Thread(this);
//t.start();
start();
}
public MyThread2() {
// TODO Auto-generated constructor stub
}
public void run(){
Thread th=Thread.currentThread();
ThreadGroup tg=th.getThreadGroup();
System.out.println("Thread Name :"+th.getName());
System.out.println("Thread Group :"+tg.getName());
}
}
97:
public class Lab22 {
public static void main(String[] args) {
//ThreadGroup tg=new ThreadGroup("jlc");
MyThread3 t1=new MyThread3("Hello");
MyThread3 t2=new MyThread3("Hai");
}
}
class MyThread3 implements Runnable{
MyThread3(String tname){
Thread t=new Thread(this,tname);
t.start();
}
public void run(){
Thread th=Thread.currentThread();
ThreadGroup tg=th.getThreadGroup();
System.out.println("Thread Name :"+th.getName());
System.out.println("Thread Group :"+tg.getName());
}
}
98:
public class Lab23 {
public static void main(String[] args) {
ThreadGroup tg=new ThreadGroup("jlc");
MyThread4 t1=new MyThread4(tg,"Hello");
MyThread4 t2=new MyThread4(tg,"Hai");
}
}
class MyThread4 extends Thread{
MyThread4(ThreadGroup tg,String tname){
super(tg,tname);
start();
}
public void run(){
Thread th=Thread.currentThread();
ThreadGroup tg=th.getThreadGroup();
System.out.println("Thread Name :"+th.getName());
System.out.println("Thread Group :"+tg.getName());
}
}
99:
public class Lab28 {
public static void main(String args[]){
Multi t1=new Multi();
t1.run();
t1.start();
}
}
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
}
100:
public class Lab29 {
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
//Thread t2 =new Thread(m1);
t1.start();
t1.run();
}
}
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
}
101;
public class Lab30 {
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
//t1.start();
//t2.start();
t1.run();
//t2.resume();
//t2.run();
//t1.start();
//t2.start();
t2.run();
//t2.start();
//t1.run();
//t1.start();
}
}
class TestSleepMethod1 extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);
}catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
}
}
}
102:
public class Lab31 {
public static void main(String args[]){
TestJoinMethod3 t1=new TestJoinMethod3();
TestJoinMethod3 t2=new TestJoinMethod3();
System.out.println("Name of t1:"+t1.getName());
System.out.println("Name of t2:"+t2.getName());
System.out.println("id of t1:"+t1.getId());
t1.start();
t2.start();
t1.setName("Soni");
System.out.println("After changing name of t1:"+t1.getName());
System.out.println("id of t2:"+t1.getId());
}
}
class TestJoinMethod3 extends Thread{
public void run(){
System.out.println("running...");
}
}
103:
public class Lab32 {
public static void main(String[] args) {
MyThread8 t1=new MyThread8();
//t1.setPriority(3);
//t1.setPriority(11);
//t1.setPriority(0);
//t1.setPriority(1);
System.out.println(t1);
t1.setName("Hello");
System.out.println(t1);
}
}
class MyThread8 extends Thread{}
104:
public class Lab33 {
public static void main(String args[]){
TestJoinMethod4 t1=new TestJoinMethod4();
TestJoinMethod4 t2=new TestJoinMethod4();
t1.start();
t2.start();
}
}
class TestJoinMethod4 extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
105:
public class Lab34 {
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
class TestMultiPriority1 extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
}
106:
public class Lab35{
public static void main(String[] args){
TestDaemonThread3 t1=new TestDaemonThread3();//creating thread
TestDaemonThread3 t2=new TestDaemonThread3();
TestDaemonThread3 t3=new TestDaemonThread3();
t1.setDaemon(true);//now t1 is daemon thread
t1.start();//starting threads
t2.start();
t3.start();
}
}
class TestDaemonThread3 extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){//checking for daemon thread
System.out.println("daemon thread work");
}else{
System.out.println("user thread work");
}}}
107:
public class Lab36 {
public static void main(String[] args){
TestDaemonThread2 t1=new TestDaemonThread2();
TestDaemonThread2 t2=new TestDaemonThread2();
t1.start();
t1.setDaemon(true);//will throw exception here
t2.start();
}
}
class TestDaemonThread2 extends Thread{
public void run(){
System.out.println("Name: "+Thread.currentThread().getName());
System.out.println("Daemon: "+Thread.currentThread().isDaemon());
}
}
108:
public class Lab37{
public static void main(String[] args)throws Exception {
MyThread10 th=new MyThread10();
Thread t1=new Thread(th);
System.out.println(t1.getState());
t1.start();
Thread.sleep(500);
System.out.println(t1.getState());
Thread.sleep(5000);
System.out.println(t1.getState());
}
}class MyThread10 extends Thread{
public void run(){
Thread th=Thread.currentThread();
for(int i=0;i<=10;i++){
System.out.println(th.getName()+"-value is :"+i+"\t"+th.getState());
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
109:
public class Lab38 {
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
t3.join();
}catch(Exception e)
{System.out.println(e);}
t2.start();
/* try{
t2.join();
}catch(Exception e){
System.out.println(e);
}*/
t3.start();
}
}
class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
}
110:
public class Lab39 {
public static void main(String args[]){
TestJoinMethod2 t1=new TestJoinMethod2();
TestJoinMethod2 t2=new TestJoinMethod2();
TestJoinMethod2 t3=new TestJoinMethod2();
t1.start();
try{
t1.join(1500);
}catch(Exception e){System.out.println(e);}
t2.start();
/*try{
t2.join(2000);
}catch(Exception e){System.out.println(e);}
*/
t3.start();
}
}
class TestJoinMethod2 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
}
111:
public class Lab40 {
public static void main(String args[]){
TestJoinMethod5 t1=new TestJoinMethod5();
TestJoinMethod5 t2=new TestJoinMethod5();
System.out.println("Name of t1:"+t1.getName());
System.out.println("Name of t2:"+t2.getName());
System.out.println("id of t1:"+t1.getId());
t1.start();
t2.start();
t1.setName("Sonoo Jaiswal");
System.out.println("After changing name of t1:"+t1.getName());
}
}
class TestJoinMethod5 extends Thread{
public void run(){
System.out.println("running...");
}
}
112:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Lab41 {
public static void main(String[] args) {
WorkerThread wt=new WorkerThread();
//WorkerThread wt1=new WorkerThread();
wt.run();
//wt1.run();
}
}
class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s){
this.message=s;
}
public WorkerThread() {
// TODO Auto-generated constructor stub
}
public void run() {
System.out.println(Thread.currentThread().getName()+
" (Start) message = "+message);
processmessage();
//call processmessage method that sleeps the thread for 2 seconds
System.out.println(Thread.currentThread().getName()+" (End)");
//prints thread name
}
private void processmessage() {
try { Thread.sleep(2000);
} catch (InterruptedException e)
{ e.printStackTrace(); }
}
}
113:
public class Lab42 {
public static void main(String args[]){
TestMultitasking1 t1=new TestMultitasking1();
TestMultitasking1 t2=new TestMultitasking1();
TestMultitasking1 t3=new TestMultitasking1();
t1.start();
t2.start();
t3.start();
}
}
class TestMultitasking1 extends Thread{
public void run(){
System.out.println("task one");
}
}
114:
class Simple1 extends Thread{
public void run(){
System.out.println("task one");
}
}
class Simple2 extends Thread{
public void run(){
System.out.println("task two");
}
}
class Lab43{
public static void main(String args[]){
Simple1 t1=new Simple1();
Simple2 t2=new Simple2();
t1.start();
t2.start();
}
}
115:
class Lab44{
public static void main(String args[]){
Thread t1=new Thread(){
public void run(){
System.out.println("task one");
}
};
Thread t2=new Thread(){
public void run(){
System.out.println("task two");
}
};
t1.start();
t2.start();
}
}
116:
class Table1{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
class MyThread15 extends Thread{
Table t;
MyThread15(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread16 extends Thread{
Table t;
MyThread16(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
public class Lab46{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread15 t1=new MyThread15(obj);
MyThread16 t2=new MyThread16(obj);
t1.start();
t2.start();
}
}
117:
class Table3{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}
}
}
public class Lab47{
public static void main(String args[]){
final Table obj = new Table();//only one object
Thread t1=new Thread(){
public void run(){
obj.printTable(5);
}
};
Thread t2=new Thread(){
public void run(){
obj.printTable(100);
}
};
t1.start();
t2.start();
}
}
118:
class Table4{
synchronized static void printTable(int n){
for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
}
}
}
public class Lab48 {
public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
Table4.printTable(1);
}
};
Thread t2=new Thread(){
public void run(){
Table4.printTable(10);
}
};
Thread t3=new Thread(){
public void run(){
Table4.printTable(100);
}
};
Thread t4=new Thread(){
public void run(){
Table4.printTable(1000);
}
};
t1.start();
t2.start();
t3.start();
t4.start();
}
}
119:
public class Lab49 {
public static void main(String[] args) {
Account acc=new Account();
new AccountThread(acc);
}
}
class Account{
int bal=950;
public void withdraw(int amt){
if(bal>=amt){
System.out.println(Thread.currentThread().getName()+
"is going to withdraw........:"+bal);
try{
}catch(Exception e){}
bal-=amt;
System.out.println(Thread.currentThread().getName()+
"is completed withdraw.......:"+bal);
}
else{
System.out.println("No Funds for :"+Thread.currentThread().getName());
}
}
public int getBal(){
return bal;
}
}
class AccountThread implements Runnable{
Account acc=null;
AccountThread(Account acc){
this.acc=acc;
Thread t1=new Thread(this,"A ");
Thread t2=new Thread(this,"B ");
t1.start();
t2.start();
}
public void run(){
for(int i=0;i<1;i++){
acc.withdraw(800);
if(acc.getBal()<0){
System.out.println("Account is Ocerdrawn.............");
}
}
}
}
120:
public class LAB50 {
public static void main(String[] args) {
Hello11 h1=new Hello11();
Hello11 h2=new Hello11();
MyThread18 th1=new MyThread18(h1);
//MyThread18 th2=new MyThread18(h1);
MyThread18 th3=new MyThread18(h2);
th1.start();
//th2.start();
th3.start();
}
}
class MyThread18 extends Thread{
Hello11 h=null;
MyThread18(Hello11 h){
this.h=h;
}
public void run(){
h.show();
}
}
class Hello11{
synchronized void show(){
Thread th=Thread.currentThread();
for(int i=0;i<3;i++){
System.out.println(th.getName()+"-SHOW :"+"\t"+this);
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
121:
import java.io.*;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}
122:
public class TestDaemonThread1 extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){//checking for daemon thread
System.out.println("daemon thread work");
}
else{
System.out.println("user thread work");
}
}
public static void main(String[] args){
TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
TestDaemonThread1 t2=new TestDaemonThread1();
TestDaemonThread1 t3=new TestDaemonThread1();
t1.setDaemon(true);//now t1 is daemon thread
t1.start();//starting threads
t2.start();
t3.start();
}
}
123:
class MyThread12 extends Thread{
public void run(){
System.out.println("shut down hook task completed..");
}
}
public class TestShutdown1{
public static void main(String[] args)throws Exception {
Runtime r=Runtime.getRuntime();
r.addShutdownHook(new MyThread12());
System.out.println("Now main sleeping... press ctrl+c to exit");
try{Thread.sleep(3000);}catch (Exception e) {}
}
}
124:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);//creating a pool of 5 threads
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);//calling execute method of ExecutorService
}
executor.shutdown();
while (!executor.isTerminated()) { }
System.out.println("Finished all threads");
}
}
125:
class Lab01 {
public static void main(String args[]) throws ClassNotFoundException{
Class c=Class.forName("Simple");
System.out.println(c.getName());
}
}
class Simple{}
126:
class Simple1{}
class Lab02{
void printName(Object obj){
Class c=obj.getClass();
System.out.println(c.getName());
}
public static void main(String args[]){
Simple1 s=new Simple1();
Lab02 t=new Lab02();
t.printName(s);
}
}
127:
class Lab03{
public static void main(String args[]){
Class c = boolean.class;
System.out.println(c.getName());
Class c2 = Lab03.class;
System.out.println(c2.getName());
}
}
128:
class Simple3{}
interface My{}
class Lab04{
public static void main(String args[]){
try{
Class c=Class.forName("Simple3");
System.out.println(c.isInterface());
Class c2=Class.forName("My");
System.out.println(c2.isInterface());
}catch(Exception e){System.out.println(e);}
}
}
129:
public class Lab05{
public static void main(String[] args) {
Class cls1=int.class;
Class cls2=Cloneable.class;
Class cls3=args.getClass();
Class cls4=Color.class;
verifyClass(cls1);
verifyClass(cls2);
verifyClass(cls3);
verifyClass(cls4);
}
static void verifyClass(Class cls){
System.out.println("\n****Name :"+cls.getName());
System.out.println("Primitive :"+cls.isPrimitive());
System.out.println("Interface :"+cls.isInterface());
System.out.println("Array :"+cls.isArray());
System.out.println("Enum :"+cls.isEnum());
}
}
enum Color{
RED,BLUE
}
130:
import java.lang.reflect.*;
class Lab06{
public static void main(String[] args) {
try{
Class cls=Class.forName("JlcStudent");
System.out.println("Public :"+Modifier.PUBLIC);
System.out.println("Priv :"+Modifier.PRIVATE);
System.out.println("Prot :"+Modifier.PROTECTED);
System.out.println("Fina :"+Modifier.FINAL);
System.out.println("Stat :"+Modifier.STATIC);
System.out.println("Nati :"+Modifier.NATIVE);
System.out.println("Abst :"+Modifier.ABSTRACT);
System.out.println("Synch :"+Modifier.SYNCHRONIZED);
Method ms[]=cls.getDeclaredMethods();
for(int i=0;i<ms.length;i++){
Method m=ms[i];
System.out.println("\n******"+m);
int mod=m.getModifiers();
System.out.println("Modifier :"+mod);
System.out.println("PUBLIC :"+Modifier.isPublic(mod));
System.out.println("PRIVATE :"+Modifier.isPrivate(mod));
System.out.println("PROTECTED :"+Modifier.isProtected(mod));
System.out.println("FINAL :"+Modifier.isFinal(mod));
System.out.println("STATIC :"+Modifier.isStatic(mod));
System.out.println("NATIVE :"+Modifier.isNative(mod));
System.out.println("ABSTRACT :"+Modifier.isAbstract(mod));
System.out.println("SYNCHRONIZED :"+Modifier.isSynchronized(mod));
}
}catch(Exception e){
System.out.println("JLC Student class Not Found");
e.printStackTrace();
}
}
}
abstract class JlcStudent{
public void m1(){}
native final void m2();
public synchronized void m3(){}
protected abstract void m4();
private static final void m5(){}
void m6(){}
}
131:
import java.lang.reflect.*;
public class Lab07{
public static void main(String[] args)throws Exception {
Class c=Class.forName(args[0]);
System.out.println("Fields........");
Field f[]=c.getDeclaredFields();
for(int i=0;i<f.length;i++)
System.out.println(f[i]);
System.out.println("Constructors........");
Constructor con[]=c.getDeclaredConstructors();
for(int i=0;i<con.length;i++)
System.out.println(con[i]);
System.out.println("Methods........");
Method m[]=c.getDeclaredMethods();
for(int i=0;i<m.length;i++)
System.out.println(m[i]);
}
}
132:
import java.lang.reflect.*;
public class Lab09 {
public static void main(String[] args)throws Exception {
String str="ABC";
System.out.println(str);
Util.changeStringValue(str,"jlc");
}
}
class Util{
static void changeStringValue(String str,String str1)throws Exception{
Class cls=str.getClass();
Field field=cls.getDeclaredField("value");
//field set as Accessiable(true)
Object obj=field.get(str);
char ch[]=(char[])obj;
for(int i=0;i<ch.length;i++){
ch[i]=str1.charAt(i);
}
}
}
133:
public class abstractAndIntefaceTest {
public static void main(String[] args) {
}
}
abstract class classA01{
static int a=13;
static final int s=3;
abstract void m1();{
System.out.println("cllassA01-m1()");
}
abstract class testNew
//implemets
extends classA01{
}
/*abstract class clTest implements classA01{
//superinterface must be interface
}*/
}
interface TestClassA01{
final int a=12;
public String str="";
static int s=10;
void m1();
void m2();
}
//final
abstract class Test //extends
implements TestClassA01{
}
interface TestClassA02 extends TestClassA01{
}
final class Tes01 implements TestClassA01{
public void m1() {
// TODO Auto-generated method stub
}
public void m2() {
// TODO Auto-generated method stub
}
}
abstract class Az implements TestClassA01{
}
134:
public class ambigutyTest {
public static void main(String[] args) {
}
}
class intA{
void sum(int a,int b){
System.out.println(a+b);
}
}
class longA extends intA{
void sum(long a,long b){
System.out.println(a+b);
}
}
135:
import java.util.Scanner;
public class AssertionTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your age");
int value=sc.nextInt();
//assert value>=18 :"Not valid";
System.out.println("Value :-"+value);
}
}
136:
public class classTest0 {
public static void main(String[] args) {
Av av=new Av();
Ab ab=new Ab();
av.toString();
ab.toString();
av.hashCode();
ab.hashCode();
}
}
class Av{
static class Av0{
void m1(){
System.out.println("Av-Av0-m1()");
}
}
}
class Ab extends Av{
void m1(){
System.out.println("Ab-m1()");
}
}
137:
public class classtest01 {
final void testx0(){
final String x="12";
final int i=10;
}
}
class testx extends classtest01{
//void testx0(){} //cannot override final method from classtest01
String x="";
final int i=11;
}
138:
public class OOPstest {
}
class A01{
void m1(){
System.out.println("A01-m1()");
}
static void m01(){
System.out.println("A01-static-m01()");
}
}
class B01 extends A01{
void m1(){
System.out.println("B01-m1()");
}
final static void m01(){
System.out.println("B01-static-m01()");
}
}
class C01 extends B01{
String sname="raju";
String sid="01RA";
String Add="Delhi";
String j="It";
String work="Desigening";
String post="JD";
String Apps="Andriods";
String workson="Mobile";
String desigen="MobileParts";
String tools="MoileTools";
void Details_m2(){
System.out.println("C01-Details_m2()");
System.out.println(sname+"\t"+sid+"\t"+Add+"\t"+j);
}
void Tools_m3(){
System.out.println();
}
void CustomerRequi_m4(){
}
}
139:
public class staticClassTest {
public static void main(String[] args) {
}
}
final class Ac{
static class Ac0{
static class Ac1{
interface Ac2{
abstract class Ac3{
static class Ac4{
final class Ac5{
}
}
}
}
}
}
}
140:
public class staticKeywordTest {
public static void main(String[] args) {
Aa0 aa0=new Bb0();
aa0.abc();
//Aa0.ab();
//Bb0.ab();
aa0.ab();
}
}
class Aa0{
int a=10;
//private
//final
void abc(){
System.out.println("Aa0-abc()");
}
static void ab(){
System.out.println("static-Aa0-ab()");
}
static void abc(int a,int b){
System.out.println(a+b);
}
}
class Bb0 extends Aa0{
int a=20;
//private
void abc(){
System.out.println("Bb0-abc()");
}
static void ab(){
System.out.println("static-Bb0-ab()");
}
static void abc(int a,int b){
System.out.println(a+b);
}
}
141:
public class testFinal {
public final static void main(Object[] args) {
A1 a1=new A1();a1.m1();
System.out.println("main");
}
public static void main(String[] args) {
A1 a1=new A1();
//a1.m1();
C1 c0=new C1();
//c0.c1();
finalA2 fa2=new finalA2();
fa2.finalX2();fa2.finalX1();
}
}
class A1{
int a;
static String b;
void m1(){
System.out.println(a);
System.out.println(b);
}
}
class B1{
final String a1="Str";
final String b1="";
//final String c1;
static final int c2=0;
}
final class C1{
int a1;
//a1=10;
String str;
//str="raju";
void c1(){
System.out.println(str);
System.out.println(a1);
}
}
class A2{
static void ma2(){
System.out.println("A2-ma2()");
}
class A01{
public void ma2(){
System.out.println("A01-ma2()");
}
class A02{
void ma2(){
System.out.println("A02-ma2()");
}
class A03{
protected void ma2(){
System.out.println("A03-ma2()");
}
}
}
}
}
interface A3{
interface A0{
class A1{
static class A2{
}
}
}
}
class A4{
interface A04{
class A14{
interface A24{
static class A34{
}
}
}
}
}
class finalA1{
final int a=10;
final String str="";
final void finalX1(){
System.out.println("finalA1-finalX1()");
}
}
class finalA2 extends finalA1{
int a=10;
String str="raju";
// final void finalX1(){}//cannot override the final method
final void finalX2(){
System.out.println("finalA2-finalX2()");
System.out.println(a+"\t"+str);
}
}
142:
public class TestMainMethod {
}
class AS{
void XA(){
main(12);
System.out.println("A-XA()");
}
public static void main(int i) {
}
public static void main(String[] args) {
}
}
class BS{
}
143:
public class testSuper {
public static void main(String[] args) {
testSuper ts=new testSuper();
ts.m1();
}
void m1(){
System.out.println("testSuper-m1()");
}
}
class B extends testSuper{
void m1(){
System.out.println("B-m1()");
super.m1();
}
}
class C extends B{
void m2(){
System.out.println();
super.m1();
}
}
144:
class Hello{
public String data;
int value;
public final void processData(int ab,String st){}
boolean isValid(){return false;}
public class In1{}
class In2{}
}
public class Hai extends Hello {
String name;
public int id;
protected double fee;
private long phone;
void show(){}
private Hai(){}
private void Hai(int ab,String st){}
private Hai(char ch,long val,boolean b1){}
Hai(int a){}
public Hai(String ab){}
protected Hai(byte by,boolean b1,float f2){}
public static void main(String[] args) {
Hai h = new Hai();
}
}
145:
public class Lab1155{
public static void main(String[] args){
Long ref1=new Long(126L);
Long ref2=new Long(130L);
Long ref3=new Long(126L);
System.out.println("--hashCode()--");
System.out.println(ref1.hashCode());
System.out.println(ref2.hashCode());
System.out.println(ref3.hashCode());
System.out.println("--equals()--");
System.out.println(ref1.equals(ref2));
System.out.println(ref1.equals(ref3));
System.out.println("--compareTo(Long)--");
System.out.println(ref1.compareTo(ref1));
System.out.println(ref1.compareTo(ref2));
System.out.println(ref1.compareTo(ref3));
}}
146:
public class Lab1156{
public static void main(String[] args){
Long ref1=new Long(126L);
Long ref2=new Long(130L);
Long ref3=new Long(126L);
System.out.println("--hashCode()--");
System.out.println(ref1.hashCode());
System.out.println(ref2.hashCode());
System.out.println(ref3.hashCode());
System.out.println("--equals()--");
System.out.println(ref1.equals(ref2));
System.out.println(ref1.equals(ref3));
}}
147:
public class Lab1179{
public static void main(String[] args){
Byte b1=123;
Byte b2=123;
System.out.println(b1==b2);
byte b=123;
Byte b3=new Byte(b);
Byte b4=new Byte(b);
System.out.println(b3==b4);
}}
148:
public class Lab1190{
public static void main(String[] args){
Void v=null;
JlcService.show(null);
JlcService.show(v);
}}
class JlcService{
static void show(Void v){
System.out.println("show(Void)");
}}
149:
public class Lab1191{
public static void main(String[] args){
JlcService serv=new JlcService();
byte b1=12;
byte b2=23;
serv.show(b1,b2);
}}
class JlcService{
static void show(int ab,int bc){
System.out.println("show(int,int)");
}
void show(byte ab,byte bc){
System.out.println("show(byte,byte)");
}
void show(Byte ab,Byte bc){
System.out.println("show(Byte,Byte)");
}
void show(byte...ab){
System.out.println("show(byte...ab)");
}
}
150:
public class Lab1198{
public static void main(String[] args)throws Exception{
Object ref1=new int[]{12,45,42,34,45,15};
Object ref2=new int[]{101,102,103,105,109};
int obj[]=new int[10];
System.arraycopy(ref1,0,obj,0,6);
System.out.println("First Array Data");
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
System.out.println("Second Array Data");
System.arraycopy(ref2,1,obj,6,3);
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
}
}
151:
public class Lab1198A{
public static void main(String[] args)throws Exception{
Object ref1=new int[]{12,45,42,34,45,15};
Object ref2=new int[]{101,102,103,105,109};
int obj[]=new int[10];
int obj1[]=new int[12];
System.arraycopy(ref1,0,obj,0,6);
System.out.println("First Array Data");
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
System.out.println("==========================");
System.arraycopy(ref1,0,obj1,0,6);
System.out.println("First Array Data");
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
System.out.println("Second Array Data");
System.arraycopy(ref2,1,obj,6,3);
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
System.out.println("==========================");
System.arraycopy(ref1,0,obj1,0,6);
System.out.println("First Array Data");
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
System.out.println("Second Array Data");
System.arraycopy(ref2,1,obj1,6,3);
for(int i=0;i<obj.length;i++){
System.out.println(i+"\t"+obj[i]);
}
}
}
152:
class Lab1199{
public static void main(String[]arg){
int arr1[]={45,48,25,36};
String arr3[]=new String[5];
//System.arraycopy(arr1,0,arr3,0,arr1.length);
//System.arraycopy(arr3,0,arr1,0,arr3.length);
int arr2[]=new int[5];
System.arraycopy(arr1,0,arr2,0,arr1.length);
//System.arraycopy(arr1,0,arr2,0,arr2.length);
}
}
153:
public class Lab1201{
public static void main(String[] args){
Runtime rt=null;
//rt=new Runtime();
System.out.println(Runtime.getRuntime());
System.out.println(Runtime.getRuntime());
rt=Runtime.getRuntime();
System.out.println("A.P :"+rt.availableProcessors());
System.out.println("M.M :"+rt.maxMemory());
System.out.println("T.M "+rt.totalMemory());
System.out.println("F.M :"+rt.freeMemory());
}}
154:
public class Lab1202{
public static void main(String[] args)throws Exception{
Runtime rt=null;
//rt=new Runtime();
System.out.println(Runtime.getRuntime());
System.out.println(Runtime.getRuntime());
rt=Runtime.getRuntime();
String cmds[]={"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe","https://www.facebook.com"};
Process p=rt.exec(cmds);
System.out.println("Press Enter to close all process");
System.in.read();
p.destroy();
System.out.println("Main Completed");
}}
155:
public class Lab1249{
public static void main(String[] args){
System.out.println("Main Started");
String nm="";
try{
StudentService serv=new StudentService();
//nm=serv.getNameBySid(null);
//nm=serv.getNameBySid("");
//nm=serv.getNameBySid("JLC-088");
//nm=serv.getNameBySid("JLC-099");
System.out.println("Name :"+nm);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("Main Completed");
}}
class EmptySidException extends RuntimeException{}
class StudentService{
String getNameBySid(String sid){
if(sid==null)
throw new NullPointerException();
else if(sid.isEmpty())
throw new EmptySidException();
else if(sid.equals("JLC-099"))
return "srinivas";
else
throw new StudentNotFoundException(sid);
}}
class StudentNotFoundException extends RuntimeException{
StudentNotFoundException(String sid){
super(sid);}}
156:
class Hello{
void m1()throws ArithmeticException,
ArrayIndexOutOfBoundsException{
try{
m2();
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("m1-ok");
if(1==2){
throw new ArrayIndexOutOfBoundsException();
}
else{
throw new ArithmeticException("Don't do this");
}}}
void m2() throws ArrayIndexOutOfBoundsException{
m3();
}
void m3()throws ArrayIndexOutOfBoundsException{
try{
m4();
}
catch(ArithmeticException e){
System.out.println("m3-ok");
throw new ArrayIndexOutOfBoundsException();
}}
void m4()throws ArithmeticException{
m5();
}
void m5() throws ArithmeticException{
int x=10/0;
}}
public class Lab1255{
public static void main(String[] args){
try{
Hello h=new Hello();
h.m1();
}
catch(Exception e){
e.printStackTrace();
}}}
157:
import java.util.*;
public class Lab1278{
public static void main(String[] args){
Properties p=new Properties();
p.put("jlc-99","sri");
p.put("jlc-88","vas");
p.put("jlc-77","dande");
System.out.println(p);
}
}
158:
import java.util.*;
public class Lab1282{
public static void main(String[] args){
Vector v=new Vector();
Student st=new Student(99);
v.addElement(st);
System.out.println(v.contains(new Student(99)));
}}
class Student{
int sid;
Student(int sid){
this.sid=sid;
}
public String toString(){
return""+sid;
}
public boolean equals(Object obj){
System.out.println("**equals()**");
if(obj instanceof Student){
Student st=(Student)obj;
return this.sid==st.sid;
}
return false;
}}
158:
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){
System.out.println("running safely with 60km");}
}
class LabBike{
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
}
}
159:
class LabStringtoCharChangeTest{
public static void main(String [] args){
}
}
class A{
void m(){
String str1="APPLE";
String str2="ELPPA";
for(int i=0;i<str1.length();i++){
String a_letter=Charater.toString(str1.charAt(a));}
for(int j=0;j<str2.length();j++){
String b_letter=Charater.toString(str2.charAt(b));}
}
}
160:
import java.util.*;
import java.math.*;
class Lab02Math{
public static void main(String[] arg){
String euquestion1="(2x+4y)";
equestion1="100";
int<Integer>var1=new int<Integer>("variable",value);
161:
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
} }
public class MultiA{
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
162:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.omg.SendingContext.RunTime;
public class ReadingTextFile {
public static void main(String[] args) throws IOException {
//Runtime rt=null;
//rt=new Runtime();
//String cmds[]={"notepad","G:\\pdf.txt"};
//Process p1=rt.exec(cmds);
//name of txt file to open
String fileName="G:\\pdf.txt";
try{
//read byte data
byte[] buffer=new byte[5015];
FileInputStream inputStream=new FileInputStream(fileName);
// read fills buffer with data and returns
// the number of bytes read (which of course
// may be less than the buffer size, but
// it will never be more).
int total=0;
int nRead=0;
while((nRead=inputStream.read(buffer))!=-1){
// Convert to String so we can display it.
// Of course you wouldn't want to do this with
// a 'real' binary file.
System.out.println(new String(buffer));
total+=nRead;
}
// Always close files.
inputStream.close();
System.out.println("Read"+" "+total+" "+"Byte");
}catch(FileNotFoundException fe){
System.out.println("Error File Not Found"+" "+fileName+" ");
}catch(IOException x){
System.out.println("Error Reading File"+" "+fileName+" ");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
163:
class TestSleepMethod1 extends Thread{
public void run(){
for(int i=5;i>0;i--){
try{Thread.sleep(1000);}
catch(InterruptedException e)
{System.out.println(e);}
System.out.println(i);
}
} }
public class SleepA{
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
//TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
//t2.start();
}
}
164:
public class StringGetBytesExample{
public static void main(String args[]){
String s1="ABCDEFGHIJKLMNOPQRSTWXYZ";
byte[] barr=s1.getBytes();
for(int i=0;i<barr.length;i++){
System.out.println(barr[i]);
}
}}
165:
public class StringGetBytesExample1{
public static void main(String args[]){
String s1="ABCDEFG";
byte[] barr=s1.getBytes();
String s2="abcdefghijk";
byte[] barr1=s2.getBytes();
for(int i=0;i<barr.length;i++){
System.out.println("S1 "+barr[i]);
}
for(int j=0;j<barr1.length;j++){
System.out.println("S2 :"+barr1[j]);
}
}}
166:
class Student9{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student9(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student9.change();
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
Student9 s3 = new Student9 (333,"Sonoo");
s1.display();
s2.display();
s3.display();
}
}
167:
class Student10{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student10(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college);
class Student11 extends Student10{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student11(int r, String n){
rollno = r;
name = n;
}
void display (){System.out.println(rollno+" "+name+" "+college); }
public static void main(String args[]){
Student9.change();
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
Student9 s3 = new Student9 (333,"Sonoo");
s1.display();
s2.display();
s3.display();
}
}
168:
class Student12{
public static void main(String args[]){
B1 b=new B1();
b.show();
}
}
class A1{
int a1=0;
static int b1=12;
void show(){
System.out.println("A1");}
}
class B1 extends A1{}
169:
class Test0A1{
public static void main(String [] args){
System.out.println("main :");
}
class A{
static int a;
//static
{
int b=10;
System.out.println("A" +b);
System.out.println("a" +a);
};}
//System.out.println("end :"+a);
}
170:
interface Printable{}
class A implements Printable{
public void a(){System.out.println("a method");}
}
class B implements Printable{
public void b(){System.out.println("b method");}
}
class Call{
void invoke(Printable p){//upcasting
if(p instanceof A){
A a=(A)p;//Downcasting
a.a();
}
if(p instanceof B){
B b=(B)p;//Downcasting
b.b();
}
}
}//end of Call class
class Test4{
public static void main(String args[]){
Printable p=new B();
Call p1=new Printable();
Call c=new Call();
c.invoke(p);
p1.invoke(p);
}
}
171:
public class Test048 {
public static void main(String[] args) {
SuperClass sub=new SubClass();
sub.dispaly();
sub.run();
//SubClass sup=new SuperClass();
SuperClass sup=new SuperClass();
sup.dispaly();
sup.run();
SubClass sub0=new SubClass();
sub0.dispaly();
sub0.run();
}
}
class SuperClass{
public void dispaly(){
System.out.println("super");
}
public static void run(){
System.out.println("super-run");
}
}
class SubClass extends SuperClass{
public void display(){
System.out.println("sub");
}
public static void run(){
System.out.println("sub-run");
}
}
172:
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();
//In real scenario, object is provided through method e.g. getShape() method
Shape s1=new Rectangle();
s.draw();
s1.draw();
}
}
173:
public class TestArray1 {
//static char arr[];
public static void main(String[] args) {
int arr[];
//System.out.println(arr.length);
System.out.println(arr[0]);
}
}
//TestArray1.java:6: error: variable arr might not have been initialized
174:
class Testarray5{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];
//adding and printing addition of 2 matrices
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
175:
class Testarray6{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
//int d[][]={{1,3,4},{3,4,5}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[3][3];
//adding and printing addition of 2 matrices
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]*b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}}
176:
class TestCallRun2 extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestCallRun2 t1=new TestCallRun2();
TestCallRun2 t2=new TestCallRun2();
//t1.run();
// t2.run();
t1.start();
t2.start();
}
}
177:
import java.util.*;
class TestCollection01{
public static void main(String[] args){
List<String> ls=new ArrayList<String>();
ls.add("Radha");
ls.add("Radhika");
ls.add("Radhu");
Iterator itr=ls.iterator();
while(itr.hasNext()){
System.out.println(itr.next());}
}
}
178:
import java.util.*;
class TestCollection1{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();//creating arraylist
al.add("Ravi");//adding object in arraylist
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
179:
import java.util.*;
public class TestCollection7{
public static void main(String [] args){
LinkedList<String> al=new LinkedList<String>();
ArrayList<String> al1=new ArrayList<String>();
al1.add("Radha");
al1.add("Radhika");
al1.add("Radha");
al1.add("Krishna");
al.add("Radha");
al.add("Radhika");
al.add("Rama");
al.add("Radha");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
System.out.println("--------------");
Iterator<String> itr1=al1.iterator();
while(itr1.hasNext()){
System.out.println(itr1.next());
}
}
}
180:
public class TestDaemonThread1 extends Thread{
public void run(){
if(Thread.currentThread().isDaemon()){//checking for daemon thread
System.out.println("daemon thread work");
}
else{
System.out.println("user thread work");
}
}
public static void main(String[] args){
TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
TestDaemonThread1 t2=new TestDaemonThread1();
TestDaemonThread1 t3=new TestDaemonThread1();
t1.setDaemon(true);//now t1 is daemon thread
t1.start();//starting threads
t2.start();
t3.start();
}
}
181:
class TestDaemonThread2 extends Thread{
public void run(){
System.out.println("Name: "+Thread.currentThread().getName());
System.out.println("Daemon: "+Thread.currentThread().isDaemon());
}
public static void main(String[] args){
TestDaemonThread2 t1=new TestDaemonThread2();
TestDaemonThread2 t2=new TestDaemonThread2();
t1.start();
t1.setDaemon(true);//will throw exception here
t2.start();
}
}
182:
class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
//t2.start();
//t3.start();
try{
t1.join();
//t2.join();
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}
183:
class TestJoinMethod2 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod2 t1=new TestJoinMethod2();
TestJoinMethod2 t2=new TestJoinMethod2();
TestJoinMethod2 t3=new TestJoinMethod2();
t1.start();
try{
t1.join(1500);
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}
184:
class TestMultiPriority1 extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
185:
class Simple1 extends Thread{
public void run(){
System.out.println("task one");
}
}
class Simple2 extends Thread{
public void run(){
System.out.println("task two");
}
}
class TestMultitasking3{
public static void main(String args[]){
Simple1 t1=new Simple1();
Simple2 t2=new Simple2();
t1.start();
t2.start();
}
}
186:
class TestMultitasking4{
public static void main(String args[]){
Thread t1=new Thread(){
public void run(){
System.out.println("task one");
}
};
Thread t2=new Thread(){
public void run(){
System.out.println("task two");
}
};
t1.start();
t2.start();
}
}
187:
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
188:
class B{
interface A {
interface Message{
void msg();
}
} }
class TestNestedInterface2 implements B.A.Message{
public void msg(){System.out.println("Hello nested interface");}
public static void main(String args[]){
B.A.Message message=new TestNestedInterface2();//upcasting here
message.msg();
}
}
189:
public class TestThreadTwice1 extends Thread{
public void run(){
System.out.println("running...");
} }
public class ThreadTestA{
public static void main(String args[]){
TestThreadTwice1 t1=new TestThreadTwice1();
t1.start();
t1.start();
}
}
190:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s){
this.message=s;
}
public void run() {
System.out.println(Thread.currentThread().getName()+" (Start) message = "+message);
processmessage();//call processmessage method that sleeps the thread for 2 seconds
System.out.println(Thread.currentThread().getName()+" (End)");//prints thread name
}
private void processmessage() {
try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); }
}
}
191:
public class AccessControlDemo {
//Private member variable
private String privateMemberVariable = null;
//Private member method
private String privateMethod(){
return privateMemberVariable;
}
public AccessControlDemo(String str) {
privateMemberVariable = str;
}
public void demoAccessOtherClass(AccessControlDemo otherInstance)
{
//Access private members of second instance
System.out.println("Private member variable :" + otherInstance.privateMemberVariable);
System.out.println("Private member method :" + otherInstance.privateMethod());
}
public static void main(String[] args) {
AccessControlDemo firstInstance = new AccessControlDemo("first instance");
AccessControlDemo secondInstance = new AccessControlDemo("second instance");
//firstInstance.demoAccessOtherClass(secondInstance);
//firstInstance.demoAccessOtherClass(firstInstance);
//secondInstance.demoAccessOtherClass(secondInstance);
secondInstance.demoAccessOtherClass(firstInstance);
}
}
192:
import java.util.ArrayList;
public class ArrayClass {
public static void main(String[] args) {
int a[]=new int[12];
a[0]=12;
//a[12]=1;
//a[13]=14;
a[11]=1;
for(int a1:a)
System.out.println(a1);
ArrayList al=new ArrayList();
al.add(12);
al.add("raju");
al.add("rajesh");
al.add('a');
System.out.println(al);
al.add(102);
System.out.println(al);
System.out.println(al.contains('a'));
//al.remove(12);
//al.remove(0);
Integer a2=new Integer(5);
//al.remove(a2);
//al.remove(0);
al.add(a2);
System.out.println(al);
al.remove(a2);
System.out.println(al);
}
}
193:
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayList0 {
public static void main(String[] args) {
Emp e1=new Emp(111,"rajesh");
Emp e2=new Emp(222,"raju");
Emp e3=new Emp(333,"rani");
ArrayList<Emp>al=new ArrayList<Emp>();
al.add(e1);
al.add(e2);
System.out.println(al.size());
Iterator<Emp> it=al.iterator();
while(it.hasNext()){
/*for(Emp e:al){
System.out.println(e.eid+"--"+e.name);
}*/
}
ArrayList<Emp>al1=new ArrayList<Emp>();
al1.add(e2);
al1.add(e3);
al1.addAll(al);
for(Emp e:al1){
System.out.println("elements of al1"+e.eid+"--"+e.name);}
}
}
class Emp{
int eid;
String name;
Emp(int eid,String name){
}
}
class Student{
}
class Faculy{
}
194:
import java.util.ArrayList;
public class ArrayList1 {
public static void main(String[] args) {
new Stu3();
ArrayList al=new ArrayList();
al.add(123);
al.add("rajesh");
al.add('n');
System.out.println(al);
ArrayList<Emp1> al0=new ArrayList<Emp1>();
Emp1 e1=new Emp1(111,"raju");
Emp1 e2=new Emp1(222,"rajesh");
Emp1 e3=new Emp1(333,"rani");
Emp1 e4=new Emp1(444,"rajneet");
al0.add(e1);
al0.add(e2);
ArrayList<Emp1> al1=new ArrayList<Emp1>();
al1.add(e3);
al1.add(e4);
ArrayList<Emp1> al2=new ArrayList<Emp1>();
al2.add(e1);
al2.add(e3);
al2.addAll(al1);
for(Emp1 e:al0){
System.out.println(e.eid+"-----"+e.ename);
}
System.out.println("+++++++++++++++++");
for(Emp1 e:al1){
System.out.println(e.eid+"-----"+e.ename);
}
System.out.println("+++++++++++++++++");
for(Emp1 e:al2){
System.out.println(e.eid+"-----"+e.ename);
}
Stu1 s1=new Stu1(001,"rani");
Stu1 s2=new Stu1(002,"raaj");
Stu1 s3=new Stu1(003,"roli");
Stu1 s4=new Stu1(004,"rekha");
Stu1 s5=new Stu1(005,"radhika");
Stu1 s6=new Stu1(006,"ramu");
ArrayList<Stu1> al3=new ArrayList<Stu1>();
al3.add(s1);
al3.add(s2);
al3.add(s3);
al3.add(s4);
ArrayList<Stu1> al4=new ArrayList<Stu1>();
al4.add(s5);
al4.add(s6);
ArrayList alo=new ArrayList();
alo.addAll(al3);
alo.addAll(al0);
System.out.println("--------------------");
for(Stu1 e:al3){
System.out.println(e.sid+"-----"+e.sname);
}
System.out.println("--------------------");
for(Stu1 e:al4){
System.out.println(e.sid+"-----"+e.sname);
}
System.out.println("--------------------");
for(Object e:alo){
System.out.println(e.toString());
}
}}
class Emp1{
int eid;
String ename;
Emp1(int id,String name){
eid=id;
ename=name;
//System.out.println("emp class--->");
}
}
class Stu1{
int sid;
String sname;
Stu1(int id,String name){
sid=id;
sname=name;
//System.out.println("emp class--->");
}
}
class Stu2{
int a=0;
void m1(){
System.out.println("Stu2 m1()-->");
}
static void m2(){
System.out.println("static Stu2 m2()-->");
}
}
class Stu3 extends Stu2{
void m1(){
System.out.println("Stu3 m1()-->");
}
static void m2(){
System.out.println("static Stu3 m2()-->");
}
public static void main(String[] args) {
m2();
Stu2 s=new Stu2();
s.m1();
Stu3 s1=new Stu3();
s1.m1();
//Stu3 s2=new Stu2();
Stu2 s3=new Stu3();
s3.m1();
s3.m2();
}
}
195:
class Parents{
Parents(){
System.out.println("p-0-arg");
}
Parents(int a){
System.out.println("p-1-arg");
}
}
public class Child extends Parents{
Child(){
super(10);
System.out.println("c-0-arg");
}
public static void main(String[] args) {
new Child();
}
}
196:
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
System.out.println("Perform beforeExecute() logic");
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t != null) {
System.out.println("Perform exception handler logic");
}
System.out.println("Perform afterExecute() logic");
}
}
197:
public class DemoThread implements Runnable
{
private String name = null;
public DemoThread(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Executing : " + name);
}
public static void main(String[] args) {
DemoThread t=new DemoThread("t1");
t.run();
//t.toString();
}
}
198:
import java.util.Hashtable;
public class MyHashtableClear {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
//add key-value pair to Hashtable
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println("My Hashtable content:");
System.out.println(hm);
System.out.println("Clearing Hashtable:");
hm.clear();
System.out.println("Content After clear:");
System.out.println(hm);
}
}
199:
import java.util.HashMap;
import java.util.Hashtable;
public class MyHashtableCopy {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
//add key-value pair to Hashtable
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
HashMap<String, String> subMap = new HashMap<String, String>();
subMap.put("s1", "S1 VALUE");
subMap.put("s2", "S2 VALUE");
hm.putAll(subMap);
System.out.println(hm);
}
}
200:
import java.util.Hashtable;
import java.util.Set;
import java.util.Map.Entry;
public class MyHashtableEntrySet {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
Set<Entry<String, String>> entires = hm.entrySet();
for(Entry<String,String> ent:entires){
System.out.println(ent.getKey()+" ==> "+ent.getValue());
}
}
}
201:
import java.util.Enumeration;
import java.util.Hashtable;
public class MyHashtableEnumaration {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
//add key-value pair to Hashtable
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
Enumeration<String> keys = hm.keys();
while(keys.hasMoreElements()){
String key = keys.nextElement();
System.out.println("Value of "+key+" is: "+hm.get(key));
}
}
}
202:
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableKeys {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
Set<String> keys = hm.keySet();
for(String key: keys){
System.out.println(key);
}
}
}
203:
import java.util.Hashtable;
public class MyHashtableKeySearch {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
if(hm.containsKey("first")){
System.out.println("The Hashtable contains key first");
} else {
System.out.println("The Hashtable does not contains key first");
}
if(hm.containsKey("fifth")){
System.out.println("The Hashtable contains key fifth");
} else {
System.out.println("The Hashtable does not contains key fifth");
}
}
}
204:
import java.util.Hashtable;
public class MyHashtableOperations {
public static void main(String a[]){
Hashtable<String,String> ht = new Hashtable<String,String>();
//put key-value pair to hashtable
ht.put("first", "FIRST INSERTED");
ht.put("second", "SECOND INSERTED");
ht.put("fourth","FOURTH INSERTED");
ht.put("third","THIRD INSERTED");
ht.put("zero","ZERO INSERTED");
System.out.println(ht);
//getting value for the given key from hashtable
System.out.println("Value of key 'second': "+ht.get("second"));
System.out.println("Is Hashtable empty? "+ht.isEmpty());
ht.remove("third");
System.out.println(ht);
System.out.println("Size of the Hashtable: "+ht.size());
}
}
205:
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableRead {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
//add key-value pair to Hashtable
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
Set<String> keys = hm.keySet();
for(String key: keys){
System.out.println("Value of "+key+" is: "+hm.get(key));
}
}
}
206:
import java.util.Hashtable;
public class MyHashtableValueSearch {
public static void main(String a[]){
Hashtable<String, String> hm = new Hashtable<String, String>();
hm.put("first", "FIRST INSERTED");
hm.put("second", "SECOND INSERTED");
hm.put("third","THIRD INSERTED");
System.out.println(hm);
if(hm.containsValue("SECOND INSERTED")){
System.out.println("The Hashtable contains value SECOND INSERTED");
} else {
System.out.println("The Hashtable does not contains value SECOND INSERTED");
}
if(hm.containsValue("first")){
System.out.println("The Hashtable contains value first");
} else {
System.out.println("The Hashtable does not contains value first");
}
}
}
207:
public class Overloading {
public static void main(String[] args) {
AT01 t=new AT01();
t.m1(0);
t.m1(0.0f);
t.m1('a');
//t.m1("a");
At t0=new At();
t0.mt("rajesh");
t0.mt(null);
t0.mt(2);
t0.mt("raju");
t0.mt(0.0f);
t0.mt('b');
//t0.mt();
}
}
class AT01{
/*void m1(int a){
System.out.println("1-int arg");
}*/
void m1(float a){
System.out.println("1-float arg");
}
/*void m1(char a){
System.out.println("1-char arg");
}*/
}
class At{
/*static void mt(String s){
System.out.println("String mt");
}*/
static void mt(Object s){
System.out.println("Object mt");
}
/*static void mt(char s){
System.out.println("char mt");
}*/
}
208:
public class Tcasting {
public static void main(String[] args) {
/*Object o=new String("raju");
Object o1=new String();
StringBuffer s1=(StringBuffer)o;
System.out.println(s1);
String s=(String)o;
//StringBuffer s2=(StringBuffer)s;
System.out.println(s);*/
A12 a12=new A12(20);
//A22 a22=(A22)a12;
//A22 a221=(A33)a12;
System.out.println("------");
A12 a123=new A22(20);
A22 a222=(A22)a123;
System.out.println("------");
A12 a1231=new A33(20);
A22 a2221=(A22)a1231;
System.out.println("------");
A22 a22=new A22(20);
A12 a120=(A12)a22;
}
}
class A12{
A12(int a){
System.out.println("A12==");
}
}
class A22 extends A12{
A22(int a) {
super(a);
System.out.println("A22==");
}
}
class A33 extends A22{
A33(int a){
super(a);
System.out.println("A33--");
}
}
209:
public class Test001 {
public static void main(String[] args) {
Test01 t1=new Test01();
//t1.m1();
//t1.m2();
Test02 t2=new Test01();
t2.m1();
//t2.m2();
t2.m3();
Test02 t4=new Test01();
//Test01 t3=new Test02();
}
}
class Test01 extends Test02{
void m1(){
System.out.println("test01 m1 child");
}
void m2(){
System.out.println("test01 m2 child");
}
}
class Test02{
void m1(){
System.out.println("test02 parrents");
}
/*void m2(){
System.out.println("test02 m2 parrents");
}*/
void m3(){
System.out.println("test02 m3 parrents");
}
}
210:
public class Test002 {
public static void main(String[] args) {
Pa p=new Pa();
p.m1(12);
p.m2(2);
p.m3(0);
Ch c=new Ch();
c.m1(10);
c.m2(3);
c.m3(0);
c.m3(12);
Pa p1=new Ch();
p1.m1(2);
p1.m2(8);
p1.m3(45);
}
}
class Pa{
void m1(int... x){
System.out.println("parrents-m1()");
}
void m2(int x){
System.out.println("parrents-m2()");
}
final static void m3(int... x){
System.out.println("parrents-m3() final static");
}
}
class Ch extends Pa{
void m1(int x){
System.out.println("child-m1()");
}
void m2(int x){
System.out.println("child-m2()");
}
/*final static void m3(int... x){
System.out.println("child-m2()");
}*/
final static void m3(int x){
System.out.println("child-m3() final");
}
}
211:
import java.util.concurrent.Callable;
public class MyCallable implements Callable {
public Object call() throws Exception {
for(int i=0;i<3;i++){
System.out.println("inside call");
}
return null;
}
public static void main(String[] args) throws Exception {
System.out.println("inside main");
MyCallable mc=new MyCallable();
mc.call();
for(int i=0;i<3;i++){
System.out.println("inside call main");
}
}
}
212:
import java.util.concurrent.Callable;
public class MyThread implements Runnable{
public void run() {
for(int i=0;i<5;i++){
System.out.println("child mythred");
}
}
static class DemonThread{
public static void main(String[] args) {
MyThread mt=new MyThread();
Thread t=new Thread(mt);
//t.start();
//System.out.println("----------");
//t.run();
for(int i=0;i<5;i++){
System.out.println("thread");
}
Runnable r=new MyThread();
r.run();
}
}
}
class MyThread1 implements Callable{
public Object call() throws Exception {
System.out.println("--------------");
Runnable r=new MyThread();
r.run();
return null;
}
}
213:
public class MyThread0 extends Thread{
public static void main(String[] args) {
MyThread0 t1=new MyThread0();
t1.start();
for(int i=0;i<2;i++){
System.out.println("myThead0");
}
t1.run();
for(int i=0;i<2;i++){
System.out.println("run mythread0");
}
t1.resume();
t1.clone(new Thread());
}
private void clone(Thread thread) {
Thread();
System.out.println("for clone");
}
private void Thread() {
new MyThread0();
}
}
214:
//run in java 6 or before
public class Test {
static Test t=new Test();
{
System.out.println(" test me");
System.exit(0);
}
}
215:
public class AmbigutyTest0 {
public static void main(String[] args) {
AmbigutyTest0 at=new AmbigutyTest0();
sum(1, 12);
at.add(128, 15);
}
static void sum(int a,int b){
System.out.println("a+b "+(a+b));
}
static void sum(long a,int b){
System.out.println("a+b :-"+(a+b));
}
void add(int b,int a){
System.out.println(a+b);
}
void add(long a,int b){
System.out.println("a+b L:-"+(a+b));
}
}
216:
public class CheckPrimeNumber {
public static void main(String[] args) {
primeNumber();
System.out.println();
}
static boolean primeNumber(){
for(int i=1;i<=100;i++){
for(int j=2;j<=i;j++){
if(i%j==0){
//System.out.println(i);
return false;
//System.out.println(i);
}
//System.out.println(i);
}
//System.out.println(i);
}
return true;
}
}
217:
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ExceptionTest {
public void fileAccess(){
double a,c,h;
try{
a=12;h=0;
c=a/h;
FileReader fr=new FileReader("Test");
System.out.println("Rahul");
}catch(ArithmeticException e){
System.out.println("ArithmeticException="+e.getMessage());
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
e.printStackTrace();
}finally{
System.out.println("Run:-");
ExceptionTest et=new ExceptionTest();
et.fileAccess();
}
}
public static void main(String[] args) {
ExceptionTest et=new ExceptionTest();
et.fileAccess();
}
}
218:
public class abstractAndIntefaceTest {
public static void main(String[] args) {
}
}
abstract class classA01{
static int a=13;
static final int s=3;
abstract void m1();{
System.out.println("cllassA01-m1()");
}
abstract class testNew
//implemets
extends classA01{
}
/*abstract class clTest implements classA01{
//superinterface must be interface
}*/
}
interface TestClassA01{
final int a=12;
public String str="";
static int s=10;
void m1();
void m2();
}
//final
abstract class Test //extends
implements TestClassA01{
}
interface TestClassA02 extends TestClassA01{
}
final class Tes01 implements TestClassA01{
public void m1() {
// TODO Auto-generated method stub
}
public void m2() {
// TODO Auto-generated method stub
}
}
abstract class Az implements TestClassA01{
}
219:
package demo.test;
public class ambigutyTest {
public static void main(String[] args) {
MethodOverloading at=new MethodOverloading();
MethodOverloading.sum(1, 12);
at.add(128, 15);
at.show('a','v',1,4);
at.show(1,4,'c',4);
}}
class MethodOverloading{
static void sum(int a,int b){
System.out.println("a+b "+(a+b));
}
static void sum(long a,int b){
System.out.println("a+b :-"+(a+b));
}
void add(int b,int a){
System.out.println(a+b);
}
void add(long a,int b){
System.out.println("a+b L:-"+(a+b));
}
// this method is common for all the four methods mentioned bellow (1,2,3,4) .
void show(char a,char b, int c, int d){
System.out.println("(char,char,int,int)");
}
// out of the following four methods (1,2,3,4) only one will be present at a time for method overloading.
void show(int a,int b, int c,char d){
System.out.println("(int,int,int,char)");
}// it shows ambiguity error
void show(int a,int b, char c,int d){
System.out.println("(int,int,char,int)");
}// it shows ambiguity error
void show(char a,int b, int c,int d){
System.out.println("(char,int,int,int)");
}// no error compiles and void show(char a,char b, int c, int d) is invoked
void show(int a,char b, int c,char d){
System.out.println("(int,char,int,int)");
}// no error compiles and void show(char a,char b, int c, int d) is invoked
}
220:
package demo.test;
import java.util.Arrays;
import java.util.List;
public class ArraySorting {
public static void main(String[] args) {
Integer[] arr = { 4, 7, 4, 8, 9, 14, 25, 1, 0, -4 };
ArraySorting arrSort = new ArraySorting();
System.out.println("ArrayDataInAsecdingOrder");
arr = arrSort.sortArrayData(arr);
arrSort.printArrayData(arr);
System.out.println(" ");
System.out.println("ArrayDataInDesecdingOrder");
arr=arrSort.sortArrayDataInDesecdingOrder(arr);
arrSort.printArrayDataInDesecdingOrder(arr);
}
/*
* int a; int arr[]={0,77,3,4,8,7,9,11,14,5,17,-10}; for(int
* i=0;i<arr.length;i++){ for(int j=i+1;j<arr.length;j++){
* if(arr[i]>arr[j]){ a=arr[i]; arr[i]=arr[j]; arr[j]=a; } }
* System.out.println(arr[i]); }
*/
Integer[] sortArrayData(Integer arr[]) {
int a;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
return arr;
}
Integer[] printArrayData(Integer[] arr) {
List<Integer> list= Arrays.asList(arr);
for(int a: list){
System.out.println(a);
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+",");
}
return arr;
}
Integer[] sortArrayDataInDesecdingOrder(Integer arr[]) {
int a;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] < arr[j]) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
}
return arr;
}
Integer[] printArrayDataInDesecdingOrder(Integer[] arr) {
List<Integer> list= Arrays.asList(arr);
for(int a: list){
System.out.println(a);
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+",");
}
return arr;
}
}
221:
package demo.test;
import java.util.Scanner;
public class AssertionTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your age");
int value=sc.nextInt();
//assert value>=18 :"Not valid";
System.out.println("Value :-"+value);
}
}
222:
package demo.test;
import java.util.Scanner;
public class CheckPalindrom {
public static void main(String[] args) {
CheckPalindrom ch=new CheckPalindrom();
}/*
CheckPalindrom(){
String resverse="";
String str="";
Scanner sc=new Scanner(System.in);
str=sc.nextLine();
for(int i=str.length()-1;i>=0;i--)
resverse=resverse+str.charAt(i);
if(str.equals(resverse))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
*/
CheckPalindrom(){
String resverse="";
String str="";
if(str.length()>1){
for(int i=str.length()-1;i>=0;i--)
resverse=resverse+str.charAt(i);
if(str.equals(resverse))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
else{
System.out.println("Not A String");
}
}
/*CheckPalindrom(){
String str="madam";
String reverse="";
if(str.length()>1){
int i,firstChar,middleChar,lastChar;
firstChar=0;
lastChar=str.length()-1;
middleChar=(firstChar+lastChar)/2;
for(i=firstChar;i<=lastChar;i++){
if(str.charAt(firstChar)==str.charAt(lastChar)){
firstChar++;
lastChar--;
}else{
break;
}
}
if(i==middleChar-1){
System.out.println("String is Palindrome");
}
else{
System.out.println("Not A Palindrome");
}
}
else{
System.out.println("Not A String");
}
}*/
}
223:
package demo.test;
public class ConsCounting {
static int count=0;
ConsCounting(){
count++;
}
public static void main(String[] args) {
new ConsCounting();
new ConsCounting();
new ConsCounting();
new ConsCounting();
new ConsCounting();
new ConsCounting();
System.out.println(count);
new Aza1();
}
}
class Aza1{
static int i=0;
Aza1(){
i++;
main(1);
//System.out.println("i "+i);
}
public static void main(int args) {
//new Aza1();
System.out.println(i);
}
}
224:
package demo.test;
public class ConstructorChaining {
int x=10;
static int a=10;
static void ConstructorChaining0(){
System.out.println("ststicMethod ConstructorChaining0");
}
void ConstructorChaining1(){
System.out.println("Non static Method ConstructorChaining1");
}
ConstructorChaining (){
this("raja_","rani");
System.out.println("ConstructorChaining");
}
ConstructorChaining (int a){
//this(10,12);
System.out.println("a-"+a);
}
ConstructorChaining(int a,int b){
this(5);
System.out.println("a+b-"+a+b);
}
ConstructorChaining(int a,int b,int c){
this(1,3);
System.out.println("a+b+c-"+a+b+c);
}
ConstructorChaining(String str,String str0){
this(1,4,6);
System.out.println("str+str0-"+str+str0);
}
public static void main(String[] args) {
ConstructorChaining cc=new ConstructorChaining();
//cc.x=20;
//System.out.println(cc.a=2);
System.out.println(ConstructorChaining.a);
ConstructorChaining0();
cc.ConstructorChaining1();
//System.out.println(cc.x=3);
//System.out.println(cc.x=10);
//new ConstructorChaining(10);
//new ConstructorChaining(12,4);
//new ConstructorChaining();
}
}
225:
package demo.test;
public class EmpVariTest {
int a=12;
static int b=5;
public int show(){
System.out.println("int-ahow()");
new EmpVariTest();
return a;
}
static void abc(){
System.out.println("abc()");
}
public static void main(String[] args) {
EmpVariTest evt=new EmpVariTest();
System.out.println(evt.a);
//EmpVariTest.abc();
System.out.println(evt.show());
evt.show();
EmpVariTest ev=new EmpVariTest();
System.out.println(ev.show());
ev.show();
}
}
226:
package demo.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ExceptionTest {
public void fileAccess(){
try{
FileReader fr=new FileReader("Test");
}catch(FileNotFoundException e){
System.out.println(e);
}finally{
System.out.println("Run:-");
}
}
public static void main(String[] args) {
ExceptionTest et=new ExceptionTest();
et.fileAccess();
}
}
227:
package demo.test;
public class ExceptionTest0 {
public void m1(){
int a=0;
int b=4;
int c;
this.m2();
try{
c=b/a;
this.m2();
}catch(ArithmeticException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("m1()");
}
public void m2(){
this.m3();
try
{
//this.m2();
}catch(Exception e){
System.out.println(e);
//e.printStackTrace();
}
System.out.println("m2()");
}
public void m3(){
this.m4();
try
{
//this.m2();
}catch(Exception e){
System.out.println(e);
//e.printStackTrace();
}
System.out.println("m3()");
}
public void m4(){
this.m5();
System.out.println("m4()");
}
public void m5(){
//this.m1();
System.out.println("m5()");
}
public static void main(String[] args) {
ExceptionTest0 et0=new ExceptionTest0();
et0.m1();
}
}
228:
package demo.test;
public class ForPassByVaOrPassByRef {
private String attribute;
ForPassByVaOrPassByRef(String a){
this.attribute=a;
System.out.println("ForPassByVaOrPassByRef "+a);
}
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
}
class Main{
public static void main(String[] args) {
ForPassByVaOrPassByRef p=new ForPassByVaOrPassByRef("p");
changeReference(p);
modifyReference(p);
}
public static void changeReference(ForPassByVaOrPassByRef a){
ForPassByVaOrPassByRef r=new ForPassByVaOrPassByRef("r");
//a=null;
a=r;
}
public static void modifyReference(ForPassByVaOrPassByRef c){
c.setAttribute("c");
}
}
229:
package demo.test;
public class HashAndEqualTest {
public static void main(String[] args) {
String s1=new String("B");
String s2=new String("B");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
StringBuffer sb1=new StringBuffer("a");
StringBuffer sb2=new StringBuffer("a");
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.hashCode());
System.out.println(sb2.hashCode());
StringBuilder sb3=new StringBuilder("a");
StringBuilder sb4=new StringBuilder("a");
System.out.println(sb3==sb4);
System.out.println(sb3.equals(sb4));
System.out.println(sb3.hashCode());
System.out.println(sb4.hashCode());
}
}
230:
package demo.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class HashMapDemo {
public static void main(String[] args) {
HashMap hm=new HashMap();
hm.put("Rahul", new Double(2233.1245));
hm.put("Rajesh", new Double(7002.4500));
hm.put("Raghu", new Double(7111.4875));
hm.put("Rani", new Double(3333.2222));
hm.put("Rajnesh", new Double(7843.9701));
hm.put("Souyma", new Double(4855.2455));
hm.put("Soni", new Double(4578.3247));
hm.put("Sunita", new Double(2933.9745));
hm.put("Anita", new Double(7233.1485));
hm.put("Anita", new Double(7478.1485));
Set set=hm.entrySet();
Iterator it=set.iterator();
while(it.hasNext()){
Map.Entry me=(Map.Entry)it.next();
System.out.print(me.getKey()+": ");
System.out.println(me.getValue());
}
System.out.println();
double balance=((Double)hm.get("Soni")).doubleValue();
hm.put("Soni", new Double(balance+1000));
System.out.println("Soni's new balance "+hm.get("Soni"));
double balance1=((Double)hm.get("Anita")).doubleValue();
hm.put("Anita", new Double(balance1+2000));
System.out.println("Anita's new Balance "+hm.get("Anita"));
//================================================================
HashMap<String,String>hashMap=new HashMap<String,String>();
hashMap.put("key1", "value1");
hashMap.put("key2", "value2");
hashMap.put("key3", "value3");
hashMap.put("key4", "value4");
hashMap.put("key5", "value5");
hashMap.put("key6", "value6");
hashMap.put("key7", "value7");
hashMap.put("key8", "value8");
Iterator<String>iterator=hashMap.keySet().iterator();
while(iterator.hasNext()){
iterator.next();
//iterator.remove();
//System.out.println(hashMap);
}
System.out.println(hashMap);
iterator.remove();
System.out.println(hashMap);
System.out.println(hashMap.isEmpty());
//=======================================================
ConcurrentHashMap<String,String> cMap=new ConcurrentHashMap<String,String>();
//======================================================================
}
}
231:
package demo.test;
import java.net.InetAddress;
import java.net.UnknownHostException;
class HostName{
public static void main(String[] args) throws UnknownHostException {
InetAddress ip = InetAddress.getByName("10.199.199.202");
String host = ip.getHostName();
String host0=ip.getHostAddress();
System.out.println(host);
System.out.println(host0);
/*InetAddress addr = InetAddress.getLocalHost();
String ipAddress = addr.getHostAddress();
System.out.println("IP address of localhost : " + ipAddress);
String hostname = addr.getHostName();
System.out.println("Name of hostname : " + hostname);
*/
}
}
232:
package demo.test;
import java.util.Date;
final class ImmutableFinalClass {
final private Date dateDob;
final private String empName;
final private int salary;
public Date getDateDob() {
//return (Date) dateDob.clone();
return new Date(dateDob.getTime());
//return dateDob;
}
public String getEmpName() {
return empName;
}
public int getSalary() {
return salary;
}
public ImmutableFinalClass(int salary, String empName, Date dateDob){
this.salary=salary;
this.empName=empName;
this.dateDob=dateDob;
}
public static void main(String[] args) {
ImmutableFinalClass im=new ImmutableFinalClass(1000, "anil", new Date());
System.out.println(im.getEmpName());
int sal= im.getSalary();
System.out.println(sal);
sal=200;
System.out.println(im.getSalary());
Date date = im.getDateDob();
System.out.println(date);
date.setYear(2017);
System.out.println(date);
System.out.println(im.getDateDob());
}
}
233:
package demo.test;
final class ImmutableTest {
final String Customer;
final private String Student0;
final private int i;
final private String str;
public String getCustomer() {
return Customer;
}
private ImmutableTest(){
this.i=10;
this.str="radhe";
this.Student0="";
this.Customer="";
}
ImmutableTest(int i,String str){
this.i=101;
this.str="aws";
this.Customer="rani";
this.Student0="rajesh";
System.out.println(i+"\t"+str);
}
ImmutableTest(int i,String Student0,String Customer){
this.i=101;
this.str="aws";
this.Customer="rani";
this.Student0="rajesh";
System.out.println(i+"\t"+str);
}
public static void main(String[] args) {
new ImmutableTest();
/*it.Student0();
it.Student1("Rajnessh");
it.Student2(123,"Rajneet");
System.out.println();*/
new ImmutableTest(123,"rahul");
}
}
234:
package demo.test;
final class ImutableA {
final int x=12;
final int y;//The blank final field y may not have been initialized
void Am1(){
System.out.println("Am1()");
}
ImutableA(){
//x=20;
y=12;
System.out.println("Construtor");
}
public static void main(String[] args) {
ImutableA iA=new ImutableA();
iA.Am1();
//iA.y=29;
}
}
/*class Av0 extends ImutableA{
//The type ATest0 cannot subclass the final class ImutableA
}*/
class Av1{
final static int x=13;
final int a=3;
final void show(){
System.out.println("Av1-show()");
}
class Av2 extends Av1{
final static int x=14;
final int a=5;
final void show1(){
}
}
}
235:
package demo.test;
public class InitStaticBlockTest {
{
System.out.println("init");
}
InitStaticBlockTest(){
System.out.println("Constructor");
}
static void m1(){
System.out.println("stataic m1()");
}
static{
System.out.println("Static Block");
//new InitStaticBlockTest();
}
public static void main(String[] args) {
/*InitStaticBlockTest ist=new InitStaticBlockTest();
ist.m1();
*/
new InitStaticBlockTest();
new InitStaticBlockTest();
new InitStaticBlockTest();
}}
236:
package demo.test;
interface InterfaceTest0{
int x=10;
static int a=12;
void scope();
void scope01();
}
interface F1 extends InterfaceTest0{
int x=10;
void display();
void display01();
}
interface F2 extends F1{
int x=10;
}
/*abstract class abstracTest{
}*/
class Test0 implements /*InterfaceTest0,*/F1,F2{
public void scope() {
System.out.println("Test0-scope()");
}
public void display() {
System.out.println("Test-displaly()");
}
public void scope01() {
System.out.println("Test-scope01()");
}
public void display01() {
System.out.println("Test-displaly01()");
}
}
class Test01 implements InterfaceTest0,F1,F2{
public void scope01() {
System.out.println("Test01-scope01()");
}
public void display01() {
System.out.println("Test01-display01()");
}
public void scope() {
System.out.println("Test01-scope()");
}
public void display() {
System.out.println("Test01-display()");
}
public static void main(String[] args) {
//InterfaceTest0 it0=new TestCheck0();
InterfaceTest0 it1=new Test01();
//it0.x=2;it0.a=2;
//it0.scope();//it0.scope01();
it1.scope();
//it0.display();
//F1 f1=new TestCheck0();
//f1.display();
//f1.display01();
it1.scope01();
}
}
237:
package demo.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
add();
put();
}
static void add(){
List l=new ArrayList(10);
//l.add(0,"A");
//l.add(1,"B");
//l.add(3,"C");
System.out.println(l.size());
System.out.println(l);
}
public static void put(){
HashMap hm=new HashMap();
hm.put(0, new Integer(142));
Object n= 12;//new Integer(12);
System.out.println("hs="+n.hashCode());
hm.put("raju", new Double(143));
hm.put(2, new String("144"));
System.out.println(hm);
System.out.println(hm.hashCode());
System.out.println("raju".hashCode());
System.out.println(hm.get(2));
int i=10;
}
static void putAll(){
}
}
238:
package demo.test;
interface My {
public void scope();
}
interface My1 extends My{
public void display();
}
abstract class ABc extends Ax0 implements My1 {
public void display() {
System.out.println("ABc-Display(InterfaceMy1)");
}
public void scope() {
System.out.println("ABc-scope(InterfaceMy)");
}
}
class Ax0{
public static void main(String[] args) {
//My my=new ABc();
//my.scope();
/*My1 my1=new ABc();//Cannot instantiate the type ABc
my1.scope();
my1.display();*/
}
}
239:
package demo.test;
public class OOPstest {
}
class A01{
void m1(){
System.out.println("A01-m1()");
}
static void m01(){
System.out.println("A01-static-m01()");
}
}
class B01 extends A01{
void m1(){
System.out.println("B01-m1()");
}
final static void m01(){
System.out.println("B01-static-m01()");
}
}
class C01 extends B01{
String sname="raju";
String sid="01RA";
String Add="Delhi";
String j="It";
String work="Desigening";
String post="JD";
String Apps="Andriods";
String workson="Mobile";
String desigen="MobileParts";
String tools="MoileTools";
void Details_m2(){
System.out.println("C01-Details_m2()");
System.out.println(sname+"\t"+sid+"\t"+Add+"\t"+j);
}
void Tools_m3(){
System.out.println();
}
void CustomerRequi_m4(){
}
}
240:
package demo.test;
public class OpratorTest {
public static void main(String[] args) {
String s1=new String("Raju");
String s2=new String("Raju");
String s3=s1;
String s4="Raju";
String s5=new String("Raju");
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1==s4);
System.out.println(s3==s4);
System.out.println(s4==s5);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s4.equals(s5));
}
}
241:
package demo.test;
public class PrintName {
String str="Rajesh";
char[] ch={};
String reverse="";
public void inputCharactorsOfName(){
for(int i=0;i<str.length();i++){
char[] ch=str.toCharArray();
System.out.println(ch[i]);
}
}
public void readNameFromBackSide(){
for(int i=str.length()-1;i>=0;i--){
reverse=reverse+str.charAt(i);
}
System.out.println(reverse);
ch=reverse.toCharArray();
for(int i=0;i<reverse.length();i++){
System.out.println(ch[i]);
}
}
public static void main(String[] args) {
PrintName pm=new PrintName();
pm.inputCharactorsOfName();
pm.readNameFromBackSide();
}
}
242:
package demo.test;
public class ReturnType {
public String return0(String name){
System.out.println(name);
return "raju";
}
public String return1(){
return return0("rajesh");
}
public static void main(String[] args) {
ReturnType rt=new ReturnType();
rt.return1();
}
}
243:
package demo.test;
public class ReverseTheString {
String str="Raju";
String reverse="";
public static void main(String[] args) {
new ReverseTheString();
}
ReverseTheString(){
for(int i=str.length()-1;i>=0;i--){
reverse=reverse+str.charAt(i);
}
System.out.println("Given String-"+str);
System.out.println("Reverse Sting-"+reverse);
}
}
244:
package demo.test;
public class RuntimePolymTest {
public static void main(String[] args) {
//SuperTest st=new SuperTest();
SuperTest01 stc=new Cc1();
//SuperTest01 stb=new Bb1();
//stb.m1();
stc.m1();
}
}
class SuperTest01 extends RuntimePolymTest{
void m1(){
System.out.println("SuperTest-m1()");
}
}
class Bb1 extends SuperTest01{
void m1(){
System.out.println("B-m1()");
}
}
class Cc1 extends Bb1{
void m2(){
System.out.println("C-m2()");
/*}
public void m1(){
System.out.println("C-m1()");
}*/
}
}
245:
package demo.test;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class SetTest {
public static void sethash(){
Set<String> sh=new HashSet<String>();
//sh.add("raju");
String i[]={"ramesh","rajesh","ratan","raghu"};
try{
for(int j=0;j<i.length;j++){
sh.add(i[j]);
}
System.out.println(sh);
TreeSet ts=new TreeSet(sh);
/*ts.add(sh);*/
System.out.println(ts);
System.out.println((String)ts.first());
System.out.println((String)ts.last());
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) {
sethash();
}
}
246:
package demo.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
public class SortingOfObjects {
public static void main(String[] args) {
Emp em0=new Emp(20,"Deempal","Delhi",2395);
Emp em1=new Emp(32,"Raju","Lucknow",2345);
Emp em2=new Emp(27,"Rajesh","Bangalore",2312);
Emp em3=new Emp(43,"Ratan","Patna",3395);
Emp em4=new Emp(25,"Rani","Ludhiyana",5395);
Emp em5=new Emp(31,"Dinesh","Punjab",4754);
Emp em6=new Emp(20,"Rjneesh","Delhi",5435);
ArrayList<Emp>alist=new ArrayList<Emp>();
alist.add(em0);
alist.add(em1);
alist.add(em2);
alist.add(em3);
alist.add(em4);
alist.add(em5);
alist.add(em6);
System.out.println("Sorting by Age");
Collections.sort(alist,Emp.EmpAgeCompare);
for(Emp e0: alist){
System.out.println(e0);
}
System.out.println("Sorting by Name");
Collections.sort(alist,Emp.EmpNameCompare);
for(Emp e0: alist){
System.out.println(e0);
}
}
}
class Emp{
int E_age;
String E_name;
float E_sallory;
String E_address;
String E_jobType;
public Emp(int age,String name,String address,float sallory){
this.E_age=age;
this.E_name=name;
this.E_address=address;
this.E_sallory=sallory;
System.out.println(age+"\t"+name+"\t"+address+"\t"+sallory);
}
public int getE_age() {
return E_age;
}
public void setE_age(int e_age) {
E_age = e_age;
}
public String getE_name() {
return E_name;
}
public void setE_name(String e_name) {
E_name = e_name;
}
public float getE_sallory() {
return E_sallory;
}
public void setE_sallory(float e_sallory) {
E_sallory = e_sallory;
}
public String getE_address() {
return E_address;
}
public void setE_address(String e_address) {
E_address = e_address;
}
public String getE_jobType() {
return E_jobType;
}
public void setE_jobType(String e_jobType) {
E_jobType = e_jobType;
}
public static Comparator<Emp>EmpAgeCompare=new Comparator<Emp>(){
public int compare(Emp o1, Emp o2) {
int E_age1=o1.getE_age();
int E_age2=o2.getE_age();
//ascending order
return E_age1-(E_age2);
/*//descending order
return E_age2-(E_age1);*/
}};
public static Comparator<Emp>EmpNameCompare=new Comparator<Emp>(){
public int compare(Emp o1, Emp o2) {
String E_name1=o1.getE_name().toUpperCase();
String E_name2=o2.getE_name().toUpperCase();
//ascending order
return E_name1.compareTo(E_name2);
/*//descending order
return E_name2.compareTo(E_name1);*/
}};
@Override
public String toString() {
return "[ E_Age=" + E_age + ", E_name=" + E_name + ",E_address =" +
E_address +",E_sallory= "+E_sallory+"]";
}
}
247:
package demo.test;
public class staticKeywordTest {
public static void main(String[] args) {
Aa0 aa0=new Bb0();
aa0.abc();
//Aa0.ab();
//Bb0.ab();
aa0.ab();
}
}
class Aa0{
int a=10;
//private
//final
void abc(){
System.out.println("Aa0-abc()");
}
static void ab(){
System.out.println("static-Aa0-ab()");
}
static void abc(int a,int b){
System.out.println(a+b);
}
}
class Bb0 extends Aa0{
int a=20;
//private
void abc(){
System.out.println("Bb0-abc()");
}
static void ab(){
System.out.println("static-Bb0-ab()");
}
static void abc(int a,int b){
System.out.println(a+b);
}
}
248:
package demo.test;
public class StringInternDemo {
static String str="Rajeev";
StringInternDemo(){
String str1="Rmu";
String s1=str.intern();
String s2=str1.intern();
String s3=s1.intern();
String s4="Rmu".intern();
System.out.println(s1==s4);
System.out.println(str.equals(str1));
System.out.println(s1==s3);
System.out.println(s1.equals(s3));
System.out.println(s1==s2);
System.out.println(s1.hashCode());
System.out.println(s3.hashCode());
}
public static void main(String[] args) {
new StringInternDemo();
}
}
249:
package demo.test;
public class StringtoString {
public static void main(String[] args) {
Student3 s1=new Student3(101,"Rajesh","New Delhi");
Student3 s2=new Student3(102,"Obama","washington");
Student3 s3=new Student3(103,"Ratrani","Bangalore");
Student3 s4=new Student3(104,"Haanoi","Hangkong");
Student3 s5=new Student3(105,"Dicosta","Hiroshima");
s1.change0();s1.display0();
s2.change1();s2.display0();
s3.change0();s3.display0();
s4.change2();s4.display0();
s5.change2();s5.display0();
}
}
class Student3{
int id;
String name;
String City;
static String Country;
void change0(){
Country="INDIA";
}
void change1(){
Country="USA";
}
void change2(){
Country="JAPAN";
}
Student3(int id,String name,String City){
this.name=name;
this.id=id;
this.City=City;
}
void display0(){
System.out.println(id+"\t"+name+"\t"+City+"\t"+Country);
}
}
250:
package demo.test;
class Students{
String name;
int id;
static String college="ITS";
static void change(){
college="GOEL";
}
Students(int i,String n){
id=i;
name=n;
}
void display(){
System.out.println(id+"\t"+name+"\t"+college);
}
public static void main(String[] args) {
Students.change();
Students s1=new Students(101,"Raju");
Students s2=new Students(102,"Rajesh");
Students s3=new Students(103,"Raghu");
s1.display();s2.display();s3.display();
}
}
251:
package demo.test;
import java.lang.reflect.Field;
import java.util.Arrays;
public class SubStringTest {
public static void main(String[] args) throws Exception{
/*//mainString
String mainString="Hey!_Hello_Anil";
//subString holds
String subString=mainString.substring(11);
System.out.println("mainString "+mainString);
System.out.println("subString "+subString);
//check inside String
Field innerCharArrays=String.class.getDeclaredField("value");
innerCharArrays.setAccessible(true);
char[] chars=(char[])innerCharArrays.get(mainString);
System.out.println(Arrays.toString(chars));
//check inside subString
chars=(char[])innerCharArrays.get(subString);
System.out.println(Arrays.toString(chars));
*/
/* //Our main String
String mainString = "i_love_java";
//Substring holds value 'java'
String subString = mainString.substring(7);
System.out.println(mainString);
System.out.println(subString);
//Lets see what's inside mainString
Field innerCharArray = String.class.getDeclaredField("value");
innerCharArray.setAccessible(true);
char[] chars = (char[]) innerCharArray.get(mainString);
System.out.println(Arrays.toString(chars));
//Now peek inside subString
chars = (char[]) innerCharArray.get(subString);
System.out.println(Arrays.toString(chars));
*/
//Our main String
String mainString = "i_love_java";
//Substring holds value 'java'
String subString = fancySubstring(7, mainString);
System.out.println(mainString);
System.out.println(subString);
//Lets see what's inside mainString
Field innerCharArray = String.class.getDeclaredField("value");
innerCharArray.setAccessible(true);
char[] chars = (char[]) innerCharArray.get(mainString);
System.out.println(Arrays.toString(chars));
//Now peek inside subString
chars = (char[]) innerCharArray.get(subString);
System.out.println(Arrays.toString(chars));
}
//Our new method prevents memory leakage
public static String fancySubstring(int beginIndex, String original)
{
return new String(original.substring(beginIndex));
}
}
252:
package demo.test;
public class SwapingtwoNoS {
public static void main(String[] args) {
AZz a=new AZz();
//a.swapWith3Variable();
a.swapWithout3RdVariable();
}
}
class AZz{
static int a=3;
static int b=8;
int c;
void swapWith3Variable(){
c=b;
b=a;
a=c;
System.out.println(a);
System.out.println(b);
}
void swapWithout3RdVariable(){
/*a=a+b;
b=a-b;
a=a-b;*/
/*b=a+b;
a=b-a;
b=b-a;*/
a=a*b;
b=a/b;
a=a/b;
/*a=a^b;
b=a^b;
a=a^b;
*/
System.out.println(a);
System.out.println(b);
}
}
253:
package demo.test;
public class TestCheckA0 {
public static void main(String[] args) {
String s1=null;
String s2=null;
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
254:
package demo.test;
import java.util.*;
public class TestCollection0 {
public static void main(String[] args) {
Student0 s1=new Student0(101,"Raju",20);
Student0 s2=new Student0(102,"Rajesh",21);
Student0 s3=new Student0(103,"Rani",19);
List<Student0>list=new ArrayList<Student0>();
list.add(s1);
list.add(s2);
list.add(s3);
Iterator<Student0> al=list.iterator();
while(al.hasNext()){
Student0 s0=(Student0)al.next();
System.out.println(s0.id+"\t"+s0.name+"\t"+s0.age);
}
}
}
class Student0{
int id;
int age;
String name;
Student0(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
}
255:
package demo.test;
public class TestCollection1 {
public static void main(String[] args) {
Student1 s1=new Student1(101,"Ranu",19);
Student1 s2=new Student1(102,"Rani",20);
Student1 s3=new Student1(103,"Raaj",18);
Student1 s4=new Student1(104,"Riya",20);
Student1 s5=new Student1(105,"Roli",21);
s1.change0();
s2.change1();
s3.change0();
s4.change0();
s5.change1();
s1.display0();
s2.display0();
s3.display1();
s4.display0();
s5.display1();
}
}
class Student1{
int id;
int age;
String name;
static String address0;
static String address1;
static void change0(){
address0="Old Delhi";
}
static void change1(){
address1="New Delhi";
}
Student1(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
void display0(){
System.out.println(id+"\t"+name+"\t"+age+"\t"+address0);
}
void display1(){
System.out.println(id+"\t"+name+"\t"+age+"\t"+address1);
}
}
256:
package demo.test;
import java.util.*;
public class TestCollection2 {
public static void main(String[] args) {
List<String>al=new ArrayList<String>();
List<String>ll=new LinkedList<String>();
al.add("Rani");
al.add("Rajlaxshmi");
al.add("Rajeshwari");
al.add("Roli");
al.add("Riya");
ll.add("Raju");
ll.add("Rajesh");
ll.add("Ramesh");
ll.add("Raghu");
ll.add("Rajneesh");
System.out.println("ArrayList-");
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("ArrayList-"+al);
Iterator il=ll.iterator();
System.out.println("LinkedKist-");
while(il.hasNext()){
System.out.println(il.next());
}
System.out.println("LinkedList-"+ll);
}}
257:
package demo.test;
public class testFinal {
public final static void main(Object[] args) {
A1 a1=new A1();a1.m1();
System.out.println("main");
}
public static void main(String[] args) {
A1 a1=new A1();
//a1.m1();
C1 c0=new C1();
//c0.c1();
finalA2 fa2=new finalA2();
fa2.finalX2();fa2.finalX1();
}
}
class A1{
int a;
static String b;
void m1(){
System.out.println(a);
System.out.println(b);
}
}
class B1{
final String a1="Str";
final String b1="";
//final String c1;
static final int c2=0;
}
final class C1{
int a1;
//a1=10;
String str;
//str="raju";
void c1(){
System.out.println(str);
System.out.println(a1);
}
}
class A2{
static void ma2(){
System.out.println("A2-ma2()");
}
class A01{
public void ma2(){
System.out.println("A01-ma2()");
}
class A02{
void ma2(){
System.out.println("A02-ma2()");
}
class A03{
protected void ma2(){
System.out.println("A03-ma2()");
}
}
}
}
}
interface A3{
interface A0{
class A1{
static class A2{
}
}
}
}
class A4{
interface A04{
class A14{
interface A24{
static class A34{
}
}
}
}
}
class finalA1{
final int a=10;
final String str="";
final void finalX1(){
System.out.println("finalA1-finalX1()");
}
}
class finalA2 extends finalA1{
int a=10;
String str="raju";
// final void finalX1(){}//cannot override the final method
final void finalX2(){
System.out.println("finalA2-finalX2()");
System.out.println(a+"\t"+str);
}
}
258:
package demo.test;
public class TestMainMethod {
}
class AS{
void XA(){
main(12);
System.out.println("A-XA()");
}
public static void main(int i) {
}
public static void main(String[] args) {
}
}
class BS{
}
259:
package demo.test;
import java.util.ArrayList;
public class TestOnArrays {
//String tem[]=new String[10];
//tem[]=new String[24];
//arraylength=tem.length(10);
/*ArrayList<Integer> lLength=new ArrayList<Integer>();
arraylength=lLength.add(12);
lLength.size();
*/
}
260:
package demo.test;
public class TestOnCase {
String s1="Anil Tiwari";
static char ch;
String s2="";
public void lowerInCase(){
for(int i=0;i<s1.length();i++){
ch=s1.charAt(i);
if(65<=ch&&ch<=90){
ch=(char)(ch+32);
}
System.out.println(ch);
s2=s2+ch;
}
System.out.println(s2);
}
public void upperInCase(){
for(int i=0;i<s1.length();i++){
ch=s1.charAt(i);
if(97<=ch&&ch<=123){
ch=(char)(ch-32);
}
System.out.println(ch);
s2=s2+ch;
}
System.out.println(s2);
}
public void firstLatterInUpperInCase(){
ch=s1.charAt(0);
if(97<=ch&&ch<=123){
ch=(char)(ch-32);
}
System.out.println(ch);
System.out.println(s2);
}
public void firstLatterInLowerInCase(){
ch=s1.charAt(0);
if(65<=ch&&ch<=90){
ch=(char)(ch+32);
}
System.out.println(ch);
System.out.println(s2);
}
public String inUpperByFunction(){
System.out.println("Upper Case");
s1=s1.toUpperCase();
System.out.println(s1);
return s1;
}
public String inLowerByFunction(){
System.out.println("Lower Case");
s1=s1.toLowerCase();
System.out.println(s1);
return s1;
}
/*public String conLoToUppAndUppToLo(){
for(int i=0;i<s1.length();i++){
ch=s1.charAt(i);
if(Character.isUpperCase(ch)){
s2=s2.replace(ch, Character.toLowerCase(ch));
}
s2=s2+ch;
}
System.out.println(s2);
for(int i=0;i<s2.length();i++){
ch=s2.charAt(i);
if(97<=ch&&ch<=123){ch=(char)(ch-32);
}
s2=s2+ch;
}
System.out.println(s2);
return s2;
}*/
public static void main(String[] args) {
TestOnCase toc=new TestOnCase();
//toc.firstLatterInUpperInCase();
//toc.lowerInCase();
//toc.upperInCase();
//toc.firstLatterInLowerInCase();
//toc.inUpperByFunction();
//toc.inLowerByFunction();
//toc.conLoToUppAndUppToLo();
}
}
261:
package demo.test;
public class TestString0 {
public static void main(String[] args) {
String s="localhost";
//s.concat("Hai");
String s2=s.concat("Hai");
//System.out.println(s);
System.out.println(s2);
s2=s2.trim();
System.out.println(s2);
}
}
262:
package demo.test;
public class TestSuper {
public static void main(String[] args) {
TestSuper ts=new C();
ts.m1();
}
void m1(){
System.out.println("testSuper-m1()");
}
}
class C extends TestSuper{
void m2(){
System.out.println();
//super.m1();
}
public void m1(){
System.out.println("C-m1()");
// super.m1();
}
}
263:
package demo.test;
public class TimeDiffeStrSffBul {
public static void main(String[] args) {
long startTime=System.currentTimeMillis();
for(int i=0;i<50000;i++){
String str="StartRunning"+"by";
String hai="this.example"+"by";
}
long endTime=System.currentTimeMillis();
System.out.println("String");
System.out.println("the time taken"+(endTime-startTime)+"ms");
//System.out.println("---------StringBuffer-------------------");
long startTime1=System.currentTimeMillis();
for(int i=0;i<50000;i++){
StringBuffer hai=new StringBuffer();
hai.append("the");
hai.append("time");
hai.append("taken");
}
long endTime1=System.currentTimeMillis();
System.out.println("StringBuffer");
System.out.println("the time taken"+(endTime1-startTime1)+"ms");
//System.out.println("-----------StringBuilder----------------");
long startTime2=System.currentTimeMillis();
for(int i=0;i<50000;i++){
StringBuilder result=new StringBuilder();
result.append("the");
result.append("time");
result.append("taken");
}
long endTime2=System.currentTimeMillis();
System.out.println("StringBuilder");
System.out.println("the time taken"+(endTime2-startTime2)+"ms");
//------------------------------------------//
String s01="Hello";
String s00=s01.intern();
String s001="Hello";
String s02=new String("Hello");
String s03=new String("Hello");
StringBuffer sb0=new StringBuffer(s01);
StringBuffer sb01=new StringBuffer();
sb01.append("Hello");
System.out.println(s00==s01);
System.out.println(s01==s001);
System.out.println("s01+s02 "+(s01==s02));
System.out.println("s02+s03 "+(s02==s03));
System.out.println("sb0+sb01 "+(sb0==sb01));
System.out.println("s01.equals(s02) "+s01.equals(s02));
System.out.println("s02.equals(s03) "+s02.equals(s03));
System.out.println("sb0.equals(sb01) "+sb0.equals(sb01));
StringBuilder sb03=new StringBuilder(s01);
StringBuilder sb04=new StringBuilder(s01);
System.out.println("sb03.equals(sb04) "+sb03.equals(sb04));
System.out.println("sb03.equals(sb01) "+sb03.equals(sb01));
System.out.println("sb03+sb04 "+(sb03==sb04));
StringBuffer sbf=new StringBuffer("Raju is there ");
sbf.append("Hello Rajesh");
System.out.println(sbf);
StringBuffer sbf0=new StringBuffer("Raju is there ");
sbf.append(sbf0);
System.out.println(sbf);
StringBuilder sbu=new StringBuilder("Raju is there ");
sbu.append("Hello Rajesh");
System.out.println(sbu);
}
}
264:
Read and comments and upload new Example of real world and ask any question related to java
ReplyDelete