Sei sulla pagina 1di 57

1

Rational Class

Program: public class testrationalclass { private int num; private int den; public testrationalclass(int numerator,int denominator,boolean level) { if(denominator==0) { throw new RuntimeException("Denominator is zero"); } int g=level?gcd(numerator,denominator):1; num=numerator/g; den=denominator/g; } public String toString() { if(den==1) { return num+" "; } else { return num+"/" +den; }} public testrationalclass Times(testrationalclass b) { return new testrationalclass(this.num*b.num,this.den*b.den,true); } public testrationalclass plus(testrationalclass b) { int numerator=(this.num*b.den)+(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass subtract(testrationalclass b) { int numerator=(this.num*b.den)-(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass doublevalue() { int numerator=(this.num*this.num); int denominator=(this.den*this.den); return new testrationalclass(numerator,denominator,false); } public testrationalclass reciprocal() { return new testrationalclass(den,num,true); } public testrationalclass divides(testrationalclass b) { return this.Times(b.reciprocal()); } private static int gcd(int m,int n) { if(0==n)

return m; else return gcd(n,m%n); } public static void main(String args[]) { testrationalclass r1=new testrationalclass(4,2,true); testrationalclass r2=new testrationalclass(2,3,true); System.out.println("rational number class"); System.out.println(r1+"+"+r2+"="+r1.plus(r2)); System.out.println(r1+"-"+r2+"="+r1.subtract(r2)); System.out.println(r1+"*"+r2+"="+r1.Times(r2)); System.out.println(r1+"/"+r2+"="+r1.divides(r2)); System.out.println("square of"+r2+"is"+r2.doublevalue()); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac testrationalclass.java C:\Program Files\Java\jdk1.6.0_03\bin>java testrationalclass rational number class 2 +2/3=8/3 2 -2/3=4/3 2 *2/3=4/3 2 /2/3=3 square of2/3is4/9

Implementation Of Date Class Program: public class Date { private final int month; private final int year; private final int day; public Date(int m,int d,int y) { if(!isValid(m,d,y)) throw new RuntimeException("Invalid"); month= m; day=d; year=y; } private static boolean isValid(int m,int d,int y) { int[]DAYS={0,31,29,31,30,31,31,30,31,30,31}; if(m<1||m>12) return false; if(d<1||d>DAYS[m]) return false; if(m==2&&d==29&&!isLeapYear(y)) return false; return true; } private static boolean isLeapYear(int y) { if(y%400==0) return true; if(y%100==0) return false; return(y%4==0); } public Date next() { if(isValid(month,day+1,year))

return new Date(month,day+1,year); else if(isValid(month+1,1,year)) return new Date(month+1,1,year); else return new Date(1,1,year+1); } public boolean isAfter(Date b) { return compareTo(b)>0; } public boolean isBefore(Date b) { return compareTo(b)<0; } public int compareTo (Date b) { if(year!=b.year) return year-b.year; if(month!=b.month) return month-b.month; return day-b.day; } public String toString() { return day+"-"+month+"-"+year; } public static void main(String[] args) { Date today=new Date(4,30,2010); System.out.println("today"); Date nextDate=today.next(); System.out.println(nextDate); System.out.println(today.isAfter(nextDate)); System.out.println(today.next().isAfter(today)); System.out.println(today.isBefore(nextDate)); System.out.println(nextDate.compareTo(today)); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Date.java

C:\Program Files\Java\jdk1.6.0_03\bin>java Date today 1-5-2010 false true true 1

LISP List In Java Program: import java.util.*; class Lisp { public int car(List l) { Object ob=l.get(0); String st=ob.toString(); return Integer.parseInt(st); } public List cdr(List l) { Object ob=l.remove(0); Object obj[]=l.toArray(); List list=Arrays.asList(obj); return list;

} public static void main(String args[]) { List<Integer>l=new ArrayList<Integer>(); l.add(3); l.add(0); l.add(2); l.add(5); Lisp L=new Lisp(); int Val=L.car(l); System.out.println(Val); List list=L.cdr(l); System.out.println(list); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Lisp.java C:\Program Files\Java\jdk1.6.0_03\bin>java Lisp 3 [0, 2, 5]

ADT Stack Program: import java.io.*; import java.lang.*; import java.util.*; interface MyInterface { int n=20; public void pop(); public void push(); public void peek(); public void display(); } class StackImplementation implements MyInterface { int arr[]=new int[n]; int top=-1; public void push() { try { DataIntputStream dis=new DataInputStream(System.in); System.out.println("enter element"); int ele=Integer.parseInt(dis.readLine()); arr[++top]=ele; } catch(Exception e) { System.out.println("e"); }} public void pop() { int popper=arr[top]; top--; System.out.println("popped element"+popper); } public void peek() { int popper=arr[top]; System.out.println("peek element"+popper); } public void display() { if(top<0)

{ System.out.println("stack is empty"); return; } else { String str=" "; for(int i=0;i<=top;i++) str=str+" "+arr[i]; System.out.println("elements are"+str); }}} class StackADT { public static void main(String args[])throws IOException { DataInputStream dis=new DataInputStream(System.in); StackImplementation stk=new StackImplementation(); int menu=5; do { System.out.println("1.push\n2.pop\n3.peek\n4.display\n5.exit"); System.out.println(); System.out.println("enter the choice:"); menu=Integer.parseInt(dis.readLine()); switch(menu) { case 1: stk.push(); break; case 2: stk.pop(); break; case 3: stk.peek(); break; case 4: stk.display(); break; case 5: System.exit(0); }} while(menu<=5); System.out.println(); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>java StackADT 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1

enter the element: 6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 4 element are : 6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 3 peek element6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 4 element are : 6 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 2 popped element8 1.push 2.pop 3.peek 4.display 5.exit

VEHICLE CLASS Program: class Vehicle { void test() { System.out.println("vehicle operation:"); } } class Bus extends Vehicle { void test() { System.out.println("it is a bus"); } } class Car extends Vehicle { void test() { System.out.println("it is a car"); } } class Bike extends Vehicle { void test() { System.out.println("it is a bike"); } } public class Polymorphism { public static void main(String[] args) { Vehicle[] v=new Vehicle[3]; v[0]=new Bus();

v[1]=new Car(); v[2]=new Bike(); for(int i=0;i<v.length;i++) { v[i].test(); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Polymorphism.java C:\Program Files\Java\jdk1.6.0_03\bin>java Polymorphism its a bus its a car its a bike

1 Program:

Rational Class

public class testrationalclass { private int num; private int den; public testrationalclass(int numerator,int denominator,boolean level) { if(denominator==0) { throw new RuntimeException("Denominator is zero"); } int g=level?gcd(numerator,denominator):1; num=numerator/g; den=denominator/g; } public String toString() { if(den==1) { return num+" "; } else { return num+"/" +den; }} public testrationalclass Times(testrationalclass b) { return new testrationalclass(this.num*b.num,this.den*b.den,true); } public testrationalclass plus(testrationalclass b) { int numerator=(this.num*b.den)+(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass subtract(testrationalclass b) { int numerator=(this.num*b.den)-(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass doublevalue() { int numerator=(this.num*this.num); int denominator=(this.den*this.den); return new testrationalclass(numerator,denominator,false); } public testrationalclass reciprocal() { return new testrationalclass(den,num,true); } public testrationalclass divides(testrationalclass b) { return this.Times(b.reciprocal()); } private static int gcd(int m,int n) { if(0==n) return m; else return gcd(n,m%n);

} public static void main(String args[]) { testrationalclass r1=new testrationalclass(4,2,true); testrationalclass r2=new testrationalclass(2,3,true); System.out.println("rational number class"); System.out.println(r1+"+"+r2+"="+r1.plus(r2)); System.out.println(r1+"-"+r2+"="+r1.subtract(r2)); System.out.println(r1+"*"+r2+"="+r1.Times(r2)); System.out.println(r1+"/"+r2+"="+r1.divides(r2)); System.out.println("square of"+r2+"is"+r2.doublevalue()); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac testrationalclass.java C:\Program Files\Java\jdk1.6.0_03\bin>java testrationalclass rational number class 2 +2/3=8/3 2 -2/3=4/3 2 *2/3=4/3 2 /2/3=3 square of2/3is4/9

Implementation Of Date Class Program: public class Date { private final int month; private final int year; private final int day; public Date(int m,int d,int y) { if(!isValid(m,d,y)) throw new RuntimeException("Invalid"); month= m; day=d; year=y; } private static boolean isValid(int m,int d,int y) { int[]DAYS={0,31,29,31,30,31,31,30,31,30,31}; if(m<1||m>12) return false; if(d<1||d>DAYS[m]) return false; if(m==2&&d==29&&!isLeapYear(y)) return false; return true; } private static boolean isLeapYear(int y) { if(y%400==0) return true; if(y%100==0) return false; return(y%4==0); } public Date next() { if(isValid(month,day+1,year)) return new Date(month,day+1,year); else if(isValid(month+1,1,year)) return new Date(month+1,1,year);

else return new Date(1,1,year+1); } public boolean isAfter(Date b) { return compareTo(b)>0; } public boolean isBefore(Date b) { return compareTo(b)<0; } public int compareTo (Date b) { if(year!=b.year) return year-b.year; if(month!=b.month) return month-b.month; return day-b.day; } public String toString() { return day+"-"+month+"-"+year; } public static void main(String[] args) { Date today=new Date(4,30,2010); System.out.println("today"); Date nextDate=today.next(); System.out.println(nextDate); System.out.println(today.isAfter(nextDate)); System.out.println(today.next().isAfter(today)); System.out.println(today.isBefore(nextDate)); System.out.println(nextDate.compareTo(today)); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Date.java C:\Program Files\Java\jdk1.6.0_03\bin>java Date today 1-5-2010

false true true 1

LISP List In Java Program: import java.util.*; class Lisp { public int car(List l) { Object ob=l.get(0); String st=ob.toString(); return Integer.parseInt(st); } public List cdr(List l) { Object ob=l.remove(0); Object obj[]=l.toArray(); List list=Arrays.asList(obj); return list; } public static void main(String args[]) {

List<Integer>l=new ArrayList<Integer>(); l.add(3); l.add(0); l.add(2); l.add(5); Lisp L=new Lisp(); int Val=L.car(l); System.out.println(Val); List list=L.cdr(l); System.out.println(list); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Lisp.java C:\Program Files\Java\jdk1.6.0_03\bin>java Lisp 3 [0, 2, 5]

ADT Stack Program: import java.io.*; import java.lang.*; import java.util.*; interface MyInterface { int n=20; public void pop(); public void push(); public void peek(); public void display(); } class StackImplementation implements MyInterface { int arr[]=new int[n]; int top=-1; public void push() { try { DataIntputStream dis=new DataInputStream(System.in); System.out.println("enter element"); int ele=Integer.parseInt(dis.readLine()); arr[++top]=ele; } catch(Exception e) { System.out.println("e"); }} public void pop() { int popper=arr[top]; top--; System.out.println("popped element"+popper); } public void peek() { int popper=arr[top]; System.out.println("peek element"+popper); } public void display() { if(top<0) { System.out.println("stack is empty"); return;

} else { String str=" "; for(int i=0;i<=top;i++) str=str+" "+arr[i]; System.out.println("elements are"+str); }}} class StackADT { public static void main(String args[])throws IOException { DataInputStream dis=new DataInputStream(System.in); StackImplementation stk=new StackImplementation(); int menu=5; do { System.out.println("1.push\n2.pop\n3.peek\n4.display\n5.exit"); System.out.println(); System.out.println("enter the choice:"); menu=Integer.parseInt(dis.readLine()); switch(menu) { case 1: stk.push(); break; case 2: stk.pop(); break; case 3: stk.peek(); break; case 4: stk.display(); break; case 5: System.exit(0); }} while(menu<=5); System.out.println(); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>java StackADT 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 6 1.push

2.pop 3.peek 4.display 5.exit enter your choice: 4 element are : 6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 3 peek element6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 4 element are : 6 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 2 popped element8 1.push 2.pop 3.peek 4.display 5.exit

VEHICLE CLASS Program: class Vehicle { void test() { System.out.println("vehicle operation:"); } } class Bus extends Vehicle { void test() { System.out.println("it is a bus"); } } class Car extends Vehicle { void test() { System.out.println("it is a car"); } } class Bike extends Vehicle { void test() { System.out.println("it is a bike"); } } public class Polymorphism { public static void main(String[] args) { Vehicle[] v=new Vehicle[3]; v[0]=new Bus(); v[1]=new Car(); v[2]=new Bike(); for(int i=0;i<v.length;i++)

{ v[i].test(); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Polymorphism.java C:\Program Files\Java\jdk1.6.0_03\bin>java Polymorphism its a bus its a car its a bike

1 Program: public class testrationalclass { private int num;

Rational Class

private int den; public testrationalclass(int numerator,int denominator,boolean level) { if(denominator==0) { throw new RuntimeException("Denominator is zero"); } int g=level?gcd(numerator,denominator):1; num=numerator/g; den=denominator/g; } public String toString() { if(den==1) { return num+" "; } else { return num+"/" +den; }} public testrationalclass Times(testrationalclass b) { return new testrationalclass(this.num*b.num,this.den*b.den,true); } public testrationalclass plus(testrationalclass b) { int numerator=(this.num*b.den)+(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass subtract(testrationalclass b) { int numerator=(this.num*b.den)-(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass doublevalue() { int numerator=(this.num*this.num); int denominator=(this.den*this.den); return new testrationalclass(numerator,denominator,false); } public testrationalclass reciprocal() { return new testrationalclass(den,num,true); } public testrationalclass divides(testrationalclass b) { return this.Times(b.reciprocal()); } private static int gcd(int m,int n) { if(0==n) return m; else return gcd(n,m%n); } public static void main(String args[]) {

testrationalclass r1=new testrationalclass(4,2,true); testrationalclass r2=new testrationalclass(2,3,true); System.out.println("rational number class"); System.out.println(r1+"+"+r2+"="+r1.plus(r2)); System.out.println(r1+"-"+r2+"="+r1.subtract(r2)); System.out.println(r1+"*"+r2+"="+r1.Times(r2)); System.out.println(r1+"/"+r2+"="+r1.divides(r2)); System.out.println("square of"+r2+"is"+r2.doublevalue()); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac testrationalclass.java C:\Program Files\Java\jdk1.6.0_03\bin>java testrationalclass rational number class 2 +2/3=8/3 2 -2/3=4/3 2 *2/3=4/3 2 /2/3=3 square of2/3is4/9

Implementation Of Date Class Program: public class Date { private final int month; private final int year; private final int day; public Date(int m,int d,int y) { if(!isValid(m,d,y)) throw new RuntimeException("Invalid"); month= m; day=d; year=y; } private static boolean isValid(int m,int d,int y) { int[]DAYS={0,31,29,31,30,31,31,30,31,30,31}; if(m<1||m>12) return false; if(d<1||d>DAYS[m]) return false; if(m==2&&d==29&&!isLeapYear(y)) return false; return true; } private static boolean isLeapYear(int y) { if(y%400==0) return true; if(y%100==0) return false; return(y%4==0); } public Date next() { if(isValid(month,day+1,year)) return new Date(month,day+1,year); else if(isValid(month+1,1,year)) return new Date(month+1,1,year); else return new Date(1,1,year+1); }

public boolean isAfter(Date b) { return compareTo(b)>0; } public boolean isBefore(Date b) { return compareTo(b)<0; } public int compareTo (Date b) { if(year!=b.year) return year-b.year; if(month!=b.month) return month-b.month; return day-b.day; } public String toString() { return day+"-"+month+"-"+year; } public static void main(String[] args) { Date today=new Date(4,30,2010); System.out.println("today"); Date nextDate=today.next(); System.out.println(nextDate); System.out.println(today.isAfter(nextDate)); System.out.println(today.next().isAfter(today)); System.out.println(today.isBefore(nextDate)); System.out.println(nextDate.compareTo(today)); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Date.java C:\Program Files\Java\jdk1.6.0_03\bin>java Date today 1-5-2010 false true true

LISP List In Java Program: import java.util.*; class Lisp { public int car(List l) { Object ob=l.get(0); String st=ob.toString(); return Integer.parseInt(st); } public List cdr(List l) { Object ob=l.remove(0); Object obj[]=l.toArray(); List list=Arrays.asList(obj); return list; } public static void main(String args[]) { List<Integer>l=new ArrayList<Integer>(); l.add(3); l.add(0);

l.add(2); l.add(5); Lisp L=new Lisp(); int Val=L.car(l); System.out.println(Val); List list=L.cdr(l); System.out.println(list); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Lisp.java C:\Program Files\Java\jdk1.6.0_03\bin>java Lisp 3 [0, 2, 5]

ADT Stack Program: import java.io.*; import java.lang.*; import java.util.*; interface MyInterface { int n=20; public void pop(); public void push(); public void peek(); public void display(); } class StackImplementation implements MyInterface { int arr[]=new int[n]; int top=-1; public void push() { try { DataIntputStream dis=new DataInputStream(System.in); System.out.println("enter element"); int ele=Integer.parseInt(dis.readLine()); arr[++top]=ele; } catch(Exception e) { System.out.println("e"); }} public void pop() { int popper=arr[top]; top--; System.out.println("popped element"+popper); } public void peek() { int popper=arr[top]; System.out.println("peek element"+popper); } public void display() { if(top<0) { System.out.println("stack is empty"); return; } else {

String str=" "; for(int i=0;i<=top;i++) str=str+" "+arr[i]; System.out.println("elements are"+str); }}} class StackADT { public static void main(String args[])throws IOException { DataInputStream dis=new DataInputStream(System.in); StackImplementation stk=new StackImplementation(); int menu=5; do { System.out.println("1.push\n2.pop\n3.peek\n4.display\n5.exit"); System.out.println(); System.out.println("enter the choice:"); menu=Integer.parseInt(dis.readLine()); switch(menu) { case 1: stk.push(); break; case 2: stk.pop(); break; case 3: stk.peek(); break; case 4: stk.display(); break; case 5: System.exit(0); }} while(menu<=5); System.out.println(); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>java StackADT 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 6 1.push 2.pop 3.peek 4.display

5.exit enter your choice: 4 element are : 6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 3 peek element6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 4 element are : 6 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 2 popped element8 1.push 2.pop 3.peek 4.display 5.exit

VEHICLE CLASS Program: class Vehicle { void test() { System.out.println("vehicle operation:"); } } class Bus extends Vehicle { void test() { System.out.println("it is a bus"); } } class Car extends Vehicle { void test() { System.out.println("it is a car"); } } class Bike extends Vehicle { void test() { System.out.println("it is a bike"); } } public class Polymorphism { public static void main(String[] args) { Vehicle[] v=new Vehicle[3]; v[0]=new Bus(); v[1]=new Car(); v[2]=new Bike(); for(int i=0;i<v.length;i++) { v[i].test(); }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Polymorphism.java C:\Program Files\Java\jdk1.6.0_03\bin>java Polymorphism its a bus its a car its a bike

Rational Class

Program: public class testrationalclass { private int num; private int den; public testrationalclass(int numerator,int denominator,boolean level) {

if(denominator==0) { throw new RuntimeException("Denominator is zero"); } int g=level?gcd(numerator,denominator):1; num=numerator/g; den=denominator/g; } public String toString() { if(den==1) { return num+" "; } else { return num+"/" +den; }} public testrationalclass Times(testrationalclass b) { return new testrationalclass(this.num*b.num,this.den*b.den,true); } public testrationalclass plus(testrationalclass b) { int numerator=(this.num*b.den)+(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass subtract(testrationalclass b) { int numerator=(this.num*b.den)-(this.den*b.num); int denominator=this.den*b.den; return new testrationalclass(numerator,denominator,true); } public testrationalclass doublevalue() { int numerator=(this.num*this.num); int denominator=(this.den*this.den); return new testrationalclass(numerator,denominator,false); } public testrationalclass reciprocal() { return new testrationalclass(den,num,true); } public testrationalclass divides(testrationalclass b) { return this.Times(b.reciprocal()); } private static int gcd(int m,int n) { if(0==n) return m; else return gcd(n,m%n); } public static void main(String args[]) { testrationalclass r1=new testrationalclass(4,2,true); testrationalclass r2=new testrationalclass(2,3,true); System.out.println("rational number class");

System.out.println(r1+"+"+r2+"="+r1.plus(r2)); System.out.println(r1+"-"+r2+"="+r1.subtract(r2)); System.out.println(r1+"*"+r2+"="+r1.Times(r2)); System.out.println(r1+"/"+r2+"="+r1.divides(r2)); System.out.println("square of"+r2+"is"+r2.doublevalue()); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac testrationalclass.java C:\Program Files\Java\jdk1.6.0_03\bin>java testrationalclass rational number class 2 +2/3=8/3 2 -2/3=4/3 2 *2/3=4/3 2 /2/3=3 square of2/3is4/9

Implementation Of Date Class Program: public class Date { private final int month; private final int year; private final int day; public Date(int m,int d,int y) { if(!isValid(m,d,y)) throw new RuntimeException("Invalid"); month= m; day=d; year=y; } private static boolean isValid(int m,int d,int y) { int[]DAYS={0,31,29,31,30,31,31,30,31,30,31}; if(m<1||m>12) return false; if(d<1||d>DAYS[m]) return false; if(m==2&&d==29&&!isLeapYear(y)) return false; return true; } private static boolean isLeapYear(int y) { if(y%400==0) return true; if(y%100==0) return false; return(y%4==0); } public Date next() { if(isValid(month,day+1,year)) return new Date(month,day+1,year); else if(isValid(month+1,1,year)) return new Date(month+1,1,year); else return new Date(1,1,year+1); } public boolean isAfter(Date b) { return compareTo(b)>0;

} public boolean isBefore(Date b) { return compareTo(b)<0; } public int compareTo (Date b) { if(year!=b.year) return year-b.year; if(month!=b.month) return month-b.month; return day-b.day; } public String toString() { return day+"-"+month+"-"+year; } public static void main(String[] args) { Date today=new Date(4,30,2010); System.out.println("today"); Date nextDate=today.next(); System.out.println(nextDate); System.out.println(today.isAfter(nextDate)); System.out.println(today.next().isAfter(today)); System.out.println(today.isBefore(nextDate)); System.out.println(nextDate.compareTo(today)); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Date.java C:\Program Files\Java\jdk1.6.0_03\bin>java Date today 1-5-2010 false true true 1

LISP List In Java Program: import java.util.*; class Lisp { public int car(List l) { Object ob=l.get(0); String st=ob.toString(); return Integer.parseInt(st); } public List cdr(List l) { Object ob=l.remove(0); Object obj[]=l.toArray(); List list=Arrays.asList(obj); return list; } public static void main(String args[]) { List<Integer>l=new ArrayList<Integer>(); l.add(3); l.add(0); l.add(2); l.add(5); Lisp L=new Lisp();

int Val=L.car(l); System.out.println(Val); List list=L.cdr(l); System.out.println(list); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Lisp.java C:\Program Files\Java\jdk1.6.0_03\bin>java Lisp 3 [0, 2, 5]

ADT Stack Program: import java.io.*; import java.lang.*; import java.util.*; interface MyInterface { int n=20; public void pop(); public void push(); public void peek(); public void display(); } class StackImplementation implements MyInterface { int arr[]=new int[n]; int top=-1; public void push() { try { DataIntputStream dis=new DataInputStream(System.in); System.out.println("enter element"); int ele=Integer.parseInt(dis.readLine()); arr[++top]=ele; } catch(Exception e) { System.out.println("e"); }} public void pop() { int popper=arr[top]; top--; System.out.println("popped element"+popper); } public void peek() { int popper=arr[top]; System.out.println("peek element"+popper); } public void display() { if(top<0) { System.out.println("stack is empty"); return; } else { String str=" "; for(int i=0;i<=top;i++) str=str+" "+arr[i];

System.out.println("elements are"+str); }}} class StackADT { public static void main(String args[])throws IOException { DataInputStream dis=new DataInputStream(System.in); StackImplementation stk=new StackImplementation(); int menu=5; do { System.out.println("1.push\n2.pop\n3.peek\n4.display\n5.exit"); System.out.println(); System.out.println("enter the choice:"); menu=Integer.parseInt(dis.readLine()); switch(menu) { case 1: stk.push(); break; case 2: stk.pop(); break; case 3: stk.peek(); break; case 4: stk.display(); break; case 5: System.exit(0); }} while(menu<=5); System.out.println(); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>java StackADT 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 4

element are : 6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 3 peek element6 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 1 enter the element: 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 4 element are : 6 8 1.push 2.pop 3.peek 4.display 5.exit enter your choice: 2 popped element8 1.push 2.pop 3.peek 4.display 5.exit

VEHICLE CLASS Program: class Vehicle { void test() { System.out.println("vehicle operation:"); } } class Bus extends Vehicle { void test() { System.out.println("it is a bus"); } } class Car extends Vehicle { void test() { System.out.println("it is a car"); } } class Bike extends Vehicle { void test() { System.out.println("it is a bike"); } } public class Polymorphism { public static void main(String[] args) { Vehicle[] v=new Vehicle[3]; v[0]=new Bus(); v[1]=new Car(); v[2]=new Bike(); for(int i=0;i<v.length;i++) { v[i].test(); } }

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Polymorphism.java C:\Program Files\Java\jdk1.6.0_03\bin>java Polymorphism its a bus its a car its a bike Classes that Randomly Generates Rupee & Dollar Object Program: import java.io.*; import java.util.*; class Currency implements Serializable { protected String Currency; protected int amount; protected Currency(String cur,int amt) { this.Currency=cur; this.amount=amt; } public String toString() { return Currency+amount; } public String dollarToRupee(int amt) { return"Rs"+amt*45; }} class Rupee extends Currency { public Rupee(int amt) {

super("Rs",amt); }} class Dollar extends Currency { public Dollar(int amt) { super("$",amt); }} public class Serializationwrite { public static void main(String args[]) { Random r=new Random(); try { Currency currency; FileOutputStream fos=new FileOutputStream("serial.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); System.out.println("writing to file using object serialization"); for(int i=1;i<25;i++) { Object obj[]={new Rupee(r.nextInt(5000)),new Dollar(r.nextInt(5000))}; currency=(Currency)obj[r.nextInt(2)]; System.out.println(currency); oos.writeObject(currency); oos.flush(); } oos.close(); } catch(Exception e) { System.out.println("exception during serialization:"+e); }}}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac Serializationwrite.java C:\Program Files\Java\jdk1.6.0_03\bin>java Serializationwrite writing to file using object serialization $1958 $2796 $2000 $3193 Rs1503 $203 Rs1520 $946 Rs4974 Rs278 Rs1810 $511 $2245 Rs2445 Rs3162 $4191 Rs1821 $3765 $1021 Rs3190 $807 $1833 $1279 $4972

Convert Dollar to Rupee Program: import java.io.*; import java.util.*;

public class SerializationRead { public static void main(String args[]) { try { Currency obj; FileInputStream fis=new FileInputStream("serial.txt"); ObjectInputStream ois=new ObjectInputStream(fis); System.out.println("reading from file using object Serialization:"); for(int i=1;i<=25;i++) { obj=(Currency)ois.readObject(); if((obj.Currency).equals("$")) System.out.println(obj+"="+obj.dollarToRupee(obj.amount)); else System.out.println(obj+"="+obj); } ois.close(); } catch(Exception e) { System.out.println("Exception during deserialization"+e); }}}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac SerializationRead.java C:\Program Files\Java\jdk1.6.0_03\bin>java SerializationRead reading from file using object Serialization: $1958=Rs88110 $2796=Rs125820 $2000=Rs90000 $3193=Rs143685 Rs1503=Rs1503 $203=Rs9135

Rs1520=Rs1520 $946=Rs42570 Rs4974=Rs4974 Rs278=Rs278 Rs1810=Rs1810 $511=Rs22995 $2245=Rs101025 Rs2445=Rs2445 Rs3162=Rs3162 $4191=Rs188595 Rs1821=Rs1821 $3765=Rs169425 $1021=Rs45945 Rs3190=Rs3190 $807=Rs36315 $1833=Rs82485 $1279=Rs57555 $4972=Rs223740

Multithreaded Program Program: import java.io.*; import java.util.*; class MyThread1 extends Thread { private PipedReader pr; private PipedWriter pw; MyThread1(PipedReader pr,PipedWriter pw) { this.pr=pr; this.pw=pw; } public void run() { try { int i; for(i=0;i<10;i++) { int j; for(j=2;j<i;j++) { int n=i%j; if(n==0) { break; }} if(i==j) { pw.write(i+"\n"); }} pw.close(); } catch(IOException e) {

}}} class MyThread2 extends Thread { private PipedReader pr; private PipedWriter pw; MyThread2(PipedReader pr,PipedWriter pw) { this.pr=pr; this.pw=pw; } public void run() { try { int f1=0,f2=1,f3=1; for(int i=1;i<10;i++) { pw.write(f3+"\n"); f1=f2; f2=f3; f3=f1+f2; } pw.close(); } catch(IOException e) { }}} class Multithreadedprogram { public static void main(String args[])throws Exception { ArrayList List1=new ArrayList(); ArrayList List2=new ArrayList(); PipedWriter pw1=new PipedWriter(); PipedReader pr1=new PipedReader(pw1); MyThread1 mt1=new MyThread1(pr1,pw1); System.out.println("prime numbers"); mt1.start(); int item1; while((item1=pr1.read())!=-1) { char ch1=((char)item1); System.out.print(Character.toString(ch1)); List1.add(Character.toString(ch1)); } pr1.close(); PipedWriter pw2=new PipedWriter(); PipedReader pr2=new PipedReader(pw2); MyThread2 mt2=new MyThread2(pr2,pw2); System.out.println("fibonacci Number:"); mt2.start(); int item2; while ((item2=pr2.read())!=-1) { char ch2=((char)item2); System.out.print(Character.toString(ch2)); List2.add(Character.toString(ch2)); } pr2.close(); System.out.println("Elements common to both lists are:");

List1.retainAll(List2); for(int i=0;i<List1.size();i++) { System.out.print(List1.get(i)); }}}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>java Multithreadedprogram prime numbers 2 3 5 7 fibonacci Number: 1 2 3 5 8 13 21 34

55 Elements common to both lists are: 2 3 5

Scientific Calculator Program: import java.io.*; import java.math.*; import java.awt.*; import java.applet.*; import java.awt.event.*; /*<Applet> <Applet code="calci.class" height="100" width="100"> </Applet>*/ public class calci extends Applet implements ActionListener { Label l1,l2,l3,l4; TextField t1,t2,t3; Button add,sub,mul,div,sqrt,sin,cos; TextArea a1; public void init() { l1=new Label("Number 1"); l2=new Label("Number 2"); l3=new Label("Result"); l4=new Label("enter your name:"); t1=new TextField(10); t2=new TextField(10); t3=new TextField(10); add=new Button("add"); sub=new Button("sub"); mul=new Button("mul"); div=new Button("div"); sqrt=new Button("sqrt"); sin=new Button("sin"); cos=new Button("cos"); a1=new TextArea(2, 10); add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); add(l4); add(a1); add(add); add(sub); add(mul); add(div); add(sqrt); add(sin); add(cos); add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this);

sqrt.addActionListener(this); sin.addActionListener(this); cos.addActionListener(this); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==add) { setBackground(Color.red); int sum=Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText()); t3.setText(String.valueOf(sum)); } if(ae.getSource()==sub) { setBackground(Color.yellow); int sum=Integer.parseInt(t1.getText())-Integer.parseInt(t2.getText()); t3.setText(String.valueOf(sum)); } if(ae.getSource()==mul) { setBackground(Color.blue); int sum=Integer.parseInt(t1.getText())*Integer.parseInt(t2.getText()); t3.setText(String.valueOf(sum)); } if(ae.getSource()==div) { setBackground(Color.green); int sum=Integer.parseInt(t1.getText())/Integer.parseInt(t2.getText()); t3.setText(String.valueOf(sum)); } if(ae.getSource()==sqrt) { setBackground(Color.pink); Double sum=Math.sqrt(Integer.parseInt(t1.getText())); t3.setText(String.valueOf(sum)); } if(ae.getSource()==sin) { setBackground(Color.yellow); Double sum=Math.sin(Integer.parseInt(t1.getText())); t3.setText(String.valueOf(sum)); } if(ae.getSource()==cos) { setBackground(Color.orange); Double sum=Math.cos(Integer.parseInt(t1.getText())); t3.setText(String.valueOf(sum)); } repaint(); } public void paint(Graphics g) { g.drawRect(180,180,220,220); g.drawString("Number1="+t1.getText(),200,200); g.drawString("Number2="+t2.getText(),200,210); g.drawString("Result="+t3.getText(),200,220); g.drawString("Name="+a1.getText(),200,230); }}

Output: C:\Program Files\Java\jdk1.6.0_03\bin>javac calci.java C:\Program Files\Java\jdk1.6.0_03\bin>appletviewer calci.java

Mini Project Program: import java.awt.*; import java.awt.event.*; public class NotepadLayout extends Frame implements WindowListener { MenuBar mb = new MenuBar(); Menu f1 = new Menu("File"); Menu f2 = new Menu("Edit"); Menu f3 = new Menu("Help"); MenuItem a1 = new MenuItem("N e w"); MenuItem a2 = new MenuItem("O p e n"); MenuItem a3 = new MenuItem("S a v e"); MenuItem a4 = new MenuItem("E x i t"); MenuItem a5 = new MenuItem("C u t"); MenuItem a6 = new MenuItem("C o p y "); MenuItem a7 = new MenuItem("P a s t e "); MenuItem a8= new MenuItem("S e l e c t A l l "); MenuItem a9 = new MenuItem("H e l p T o p i c"); TextArea ta = new TextArea(); NotepadLayout() { setTitle("Notepad developed by Surya & Vivek"); f1.add(a1); f1.addSeparator(); f1.add(a2); f1.addSeparator(); f1.add(a3); f1.addSeparator(); f1.add(a4); f2.add(a5); f2.addSeparator(); f2.add(a6); f2.addSeparator(); f2.add(a7); f2.addSeparator(); f2.add(a8); f3.add(a9);

mb.add(f1); mb.add(f2); mb.add(f3); setMenuBar(mb); ta.setFont(new Font("Courier New",1,16)); add(ta); setSize(400,300); addWindowListener(this); show(); } public void windowOpened(WindowEvent e) { System.out.println("Window Opened"); } public void windowClosing(WindowEvent e) { System.out.println("Window Closing"); hide(); } public void windowClosed(WindowEvent e) { System.out.println("Window Closed"); } public void windowActivated(WindowEvent e) { System.out.println("Window Activated"); } public void windowDeactivated(WindowEvent e) { System.out.println("Window Dectivated"); } public void windowIconified(WindowEvent e) { System.out.println("Window Minimize"); } public void windowDeiconified(WindowEvent e) { System.out.println("Window Maximize"); } public static void main(String aa[]) { NotepadLayout n = new NotepadLayout(); } }

OPAC Library Management System Program: import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class Library implements ActionListener { JRadioButton rbauthor = new JRadioButton("Search by Author name"); JRadioButton rbbook = new JRadioButton("Search by Book name"); JTextField textfld = new JTextField(30);

JLabel label = new JLabel("Enter Search Key"); JButton searchbutton = new JButton("Search"); JFrame frame = new JFrame(); JTable table; DefaultTableModel model; String query = "select * from Library"; public Library() { frame.setTitle("Online Public Access Catalog"); frame.setSize(500,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); p1.setLayout(new FlowLayout()); p1.add(label); p1.add(textfld); ButtonGroup bg = new ButtonGroup(); bg.add(rbauthor); bg.add(rbbook); p2.setLayout(new FlowLayout()); p2.add(rbauthor); p2.add(rbbook); p2.add(searchbutton); searchbutton.addActionListener(this); p3.setLayout(new BorderLayout()); p3.add(p1,BorderLayout.NORTH); p3.add(p2,BorderLayout.CENTER); frame.add(p3,BorderLayout.NORTH); addTable(query); frame.setVisible(true); } public void addTable(String s) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:odbc:Library","",""); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(s); ResultSetMetaData md = rs.getMetaData(); int cols = md.getColumnCount(); model = new DefaultTableModel(1,cols); table = new JTable(model); String[] tabledata = new String[cols]; int i=0; while(i<cols) { tabledata[i] = md.getColumnName(i+1); i++: } model.addRow(tabledata); while(rs.next()) { for(i=0;i<cols;i++) tabledata[i] = rs.getObject(i+1).toString(); model.addRow(tabledata); } frame.add(table,BorderLayout.CENTER);

conn.close(); }catch(Exception e){ } public void actionPerformed(ActionEvent evt) { if(rbauthor.isSelected()) query = "select * from Library where AuthorName like '"+textfld.getText()+"%'"; if(rbbook.isSelected() query = "select * from Library where BookName like '"+textfld.getText()+"%'"; while(model.getRowCount()>0) model.removeRow(0); frame.remove(table); addTable(query); } public static void main(String[] args) { new Library(); }}

Output: (Opening Screen) (Author Search) (Book Name)