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

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

## 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[]) {

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

## 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) {

## 55 Elements common to both lists are: 2 3 5

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