You are on page 1of 92

1

Program 1
Aim : Program To Generation of Next Date Program :
import java.io.*; class Nextdate { public static void main(String args[])throws IOException { int dd,mm,yyy; int nd=0,nm=0,ny=0; DataInputStream o=new DataInputStream(System.in); System.out.println("Current Date is......."); dd=Integer.parseInt(o.readLine()); System.out.println("Current Month is........"); mm=Integer.parseInt(o.readLine()); System.out.println("Current Year is........"); yyy=Integer.parseInt(o.readLine()); if(mm==12) { if(dd==31) { nd=1; nm=1; ny=yyy+1; } } else if(dd==28) { if(mm==2) { if((yyy%4==0) || (yyy%100==0)) { nd=dd+1; nm=mm; ny=yyy; } else { nd=1; nm=mm+1;
M.Sc Computer Science Assumption College, Changanacherry.

ny=yyy; } } } else if(dd==29) { if(mm==2) { nd=1; nm=mm+1; ny=yyy; } } else if(dd==30) { if( mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10) { nd=dd+1; nm=mm; ny=yyy; } else { nd=1; nm=mm+1; ny=yyy; } } else if(dd==31) { nd=1; nm=mm+1; ny=yyy; } else { nd=dd+1; nm=mm; ny=yyy; }

M.Sc Computer Science Assumption College, Changanacherry.

System.out.println("Next Date is........"); System.out.println(nd+"/"+nm+"/"+ny); } }

M.Sc Computer Science Assumption College, Changanacherry.

Output :
Current Date is....... 4 Current Month is........ 11 Current Year is........ 2011 Next Date is........ 5/11/2011

M.Sc Computer Science Assumption College, Changanacherry.

Program 2
Aim : Program To Sum Of Diagonal Elements Of A Square Matrix. Program :
import java.io.*; import java.lang.*; class Diasum { public static void main(String args[])throws IOException { int i,j,m,s=0; int a[][]=new int[3][3]; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the order of matrix"); m=Integer.parseInt(in.readLine()); System.out.println("Enter the matrix elements"); for(i=0;i<m;i++) { System.out.println("\n"); for(j=0;j<m;j++) { a[i][j]=Integer.parseInt(in.readLine()); } } System.out.println("The matrix is"); for(i=0;i<m;i++) { System.out.println("\n"); for(j=0;j<m;j++) { System.out.print(a[i][j] +"\t"); } } for(i=0;i<m;i++) { System.out.println("\n"); for(j=0;j<m;j++) { if(i==j) {
M.Sc Computer Science Assumption College, Changanacherry.

s=s+a[i][j]; } } } System.out.println("The sum of the diagonal elemnts is "+s); } }

M.Sc Computer Science Assumption College, Changanacherry.

Output :
Enter the order of matrix 3 Enter the matrix elements 9 6 3 8 5 2 7 4 1 The matrix is 9 8 7 6 5 4 3 2 1

The sum of the diagonal elemnts is 15

M.Sc Computer Science Assumption College, Changanacherry.

Program 3
Aim : Program to Book Shop. Program :
import java.io.*; class Book { String author; String title; String publisher; int price; int stock; Book(String t,String at,int p,String pb, int stck) { title=t; author=at; price=p; publisher=pb; stock=stck; } void display() { System.out.println("Book Title:"+title); System.out.println("Author:"+author); System.out.println("Book Price:"+price); System.out.println("Book Publisher:"+publisher); System.out.println("No.of copies:"+stock); } int order(int noc) { int f=0; if(noc<=stock) { int totcost=price*noc; System.out.println("Total cost of Book:"+totcost); stock=stock-noc; System.out.println("Order is successfully placed"); f=1; }

M.Sc Computer Science Assumption College, Changanacherry.

else { System.out.println("Required no. of copies not in stock"); f=0; } return f; } void update(int p) { price=p; System.out.println("New price updated"); } } class Bookshop { public static void main(String arg[]) throws IOException { int scount=0,fcount=0; int op=0; Book b[]=new Book[10]; int ch,n,f=0; DataInputStream d=new DataInputStream(System.in); System.out.println("Add new book details"); System.out.println("Enter the no. of books"); n=Integer.parseInt(d.readLine()); for(int i=0;i<n;i++) { System.out.println("Enter the book title:"); String titl=d.readLine(); System.out.println("Enter the author:"); String auth=d.readLine(); System.out.println("Enter the price:"); int prc=Integer.parseInt(d.readLine()); System.out.println("Enter the publisher:"); String publ=d.readLine(); System.out.println("Enter the no.of copies:"); int s=Integer.parseInt(d.readLine()); b[i]=new Book(titl,auth,prc,publ,s); } do { System.out.println("Bookshop Menu"); System.out.println("----------------"); System.out.println("1.Order of Books");

M.Sc Computer Science Assumption College, Changanacherry.

10

System.out.println("2.Update Price"); System.out.println("3.Transaction Details"); System.out.println("Enter your choice"); ch=Integer.parseInt(d.readLine()); switch(ch) { case 1: System.out.println("Enter the title and author of the book:"); String titl=d.readLine(); String auth=d.readLine(); for(int i=0;i<n;i++) { if((b[i].title.compareTo(titl)==0)&& (b[i].author.compareTo(auth))==0) { f=1; System.out.println("***BOOK DETAILS***"); b[i].display(); System.out.println("Enter the no.of copies required"); int nc=Integer.parseInt(d.readLine()); int p=b[i].order(nc); if(p==1) scount++; else fcount++; } if(f==0) { System.out.println("Sorry!the book is not available"); fcount++; } } break; case 2: System.out.println("Enter the book title"); String til=d.readLine(); System.out.println("Enter the new book price:"); int pr=Integer.parseInt(d.readLine()); for(int i=0;i<n;i++) {

M.Sc Computer Science Assumption College, Changanacherry.

11

if(b[i].title.compareTo(til)==0) { b[i].update(pr); b[i].display(); } } break; case 3: System.out.println("No.of successful transactions:"); System.out.println(scount); System.out.println("No.of unsuccessful transactions:"); System.out.println(fcount); break; default: System.out.println("Wrong choice !"); break; } System.out.println("Do you want to continue (1/0)?"); op=Integer.parseInt(d.readLine()); } while(op==1); } }

M.Sc Computer Science Assumption College, Changanacherry.

12

Output :
Add new book details Enter the no. of books 2 Enter the book title: Java Enter the author: Balaguruswamy Enter the price: 350 Enter the publisher: McGraw Enter the no.of copies: 2 Enter the book title: Computer Fundamental Enter the author: Sahni Enter the price: 450 Enter the publisher: Universities Press Enter the no.of copies: 3 Bookshop Menu ---------------1.Order of Books 2.Update Price 3.Transaction Details Enter your choice 1 Enter the title and author of the book: Java Balaguruswamy ***BOOK DETAILS*** Book Title:Java Author:Balaguruswamy Book Price:350 Book Publisher:McGraw No.of copies:2 Enter the no.of copies required 2

M.Sc Computer Science Assumption College, Changanacherry.

13

Total cost of Book:700 Order is successfully placed Do you want to continue (1/0)? 1 Bookshop Menu ---------------1.Order of Books 2.Update Price 3.Transaction Details Enter your choice 2 Enter the book title Computer Fundamental Enter the new book price: 500 New price updated Book Title:Computer Fundamental Author:Sahni Book Price:500 Book Publisher:Universities Press No.of copies:3 Do you want to continue (1/0)? 1 Bookshop Menu ---------------1.Order of Books 2.Update Price 3.Transaction Details Enter your choice 3 No.of successful transactions: 1 No.of unsuccessful transactions: 0 Do you want to continue (1/0)? 1 Bookshop Menu ---------------1.Order of Books 2.Update Price 3.Transaction Details Enter your choice 1

M.Sc Computer Science Assumption College, Changanacherry.

14

Enter the title and author of the book: Java Balaguruswamy ***BOOK DETAILS*** Book Title:Java Author:Balaguruswamy Book Price:350 Book Publisher:McGraw No.of copies:0 Enter the no.of copies required Do you want to continue (1/0)? 0

M.Sc Computer Science Assumption College, Changanacherry.

15

Program 4
Aim : Program to Sorting of Strings. Program :
import java.io.*; import java.lang.*; class Sortnames { public static void main(String args[])throws IOException { BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); int n,i,j; String temp; String x[]= new String[10]; System.out.println("Enter the limit"); n=Integer.parseInt(in.readLine()); System.out.println("Enter the Names"); for(i=0;i<n;i++) x[i]=in.readLine(); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(x[j].compareTo(x[i])<0) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } } System.out.println("The names after the sorting is "); for(i=0;i<n;i++) { System.out.println(x[i]+ "\n"); } }

M.Sc Computer Science Assumption College, Changanacherry.

16

Output :
Enter the limit 5 Enter the elements RAM ANNA GEETHA BALU ROY The names after the sorting is ANNA BALU GEETHA RAM ROY

M.Sc Computer Science Assumption College, Changanacherry.

17

Program 5
Aim : Program to String Palindrome Using Command Line Argument. Program :
import java.io.*; import java.lang.*; class Comndpali { public static void main(String args[]) { char ch,ch1; boolean flag=true; int l,i,j; String p =args[0]; l=p.length(); for( j=0,i=l-1 ;j<l/2; j++,i--) { ch=p.charAt(j); ch1=p.charAt(i); if(ch != ch1) { flag=false; break; } } if(flag==true) System.out.println("Palindrome"); else System.out.println("Not Palindrome"); } }

M.Sc Computer Science Assumption College, Changanacherry.

18

Output :
H:\crt>java Comndpali COMPUTER Not Palindrome H:\crt>java Comndpali POP Palindrome

M.Sc Computer Science Assumption College, Changanacherry.

19

Program 6
Aim : Program To Simple Interest Using Constructor Overloading. Program :
import java.io.*; class Deposit { int p,n; float r; Deposit() { } Deposit(int pr,int nu,float ra) { p=pr; n=nu; r=ra; } Deposit(int pr,float ra) { p=pr; r=ra; n=5; } Deposit(int nu,int pr) { n=nu; p=pr; r=10; } public void interest() { float si=p*n*r/100; float a=si+p; System.out.println("SIMPLE INTEREST :\t" +si); System.out.println("AMOUNT :\t" +a); } }

M.Sc Computer Science Assumption College, Changanacherry.

20

class Simpleinterest { public static void main(String arg[]) throws IOException { int p,n; float r; DataInputStream d= new DataInputStream(System.in); Deposit d1=new Deposit(); System.out.println("Enter the principle amount"); p=Integer.parseInt(d.readLine()); System.out.println("Enter the no: of years"); n=Integer.parseInt(d.readLine()); System.out.println("Enter the rate"); r=Float.parseFloat(d.readLine()); Deposit d2=new Deposit(p,n,r); d2.interest(); System.out.println(); System.out.println("SIMPLE INTEREST AFTER 5YRS"); System.out.println("Enter the principle amount"); p=Integer.parseInt(d.readLine()); System.out.println("Enter the rate"); r=Float.parseFloat(d.readLine()); Deposit d3=new Deposit(p,r); d3.interest(); System.out.println(); System.out.println("SIMPLE INTEREST WITH RATE 10%"); System.out.println("Enter the principle amount"); p=Integer.parseInt(d.readLine()); System.out.println("Enter the no: of years"); n=Integer.parseInt(d.readLine()); Deposit d4=new Deposit(n,p); d4.interest(); } }

M.Sc Computer Science Assumption College, Changanacherry.

21

Output :
Enter the principle amount 25000 Enter the no: of years 5 Enter the rate 10 SIMPLE INTEREST : 12500.0 AMOUNT : 37500.0 SIMPLE INTEREST AFTER 5YRS Enter the principle amount 5600 Enter the rate 12 SIMPLE INTEREST : 3360.0 AMOUNT : 8960.0 SIMPLE INTEREST WITH RATE 10% Enter the principle amount 5000 Enter the no: of years 4 SIMPLE INTEREST : 2000.0 AMOUNT : 7000.0

M.Sc Computer Science Assumption College, Changanacherry.

22

Program 7
Aim : Program To Publication. Program :
import java.io.*; import java.lang.*; class Publication { String title; float price; public void getdata(String t,float p) { title=t; price=p; } public void putdata() { System.out.println("TITLE :\t" +title); System.out.println("PRICE :\t" +price); } } class Books extends Publication { int pagecnt; public void getd(String t,float p,int pg) { getdata(t,p); pagecnt=pg; } public void putd() { putdata(); System.out.println("PAGE COUNT :\t" +pagecnt); } } class Tape extends Publication { float playtime; public void getd(String t,float p,float pt) {
M.Sc Computer Science Assumption College, Changanacherry.

23

getdata(t,p); playtime=pt; } public void putd() { putdata(); System.out.println("PLAY TIME :\t" +playtime); } } class Publicationdemo { public static void main(String arg[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String ta; float p,pl; int pa,n,i,m; Books b[]=new Books[10]; Tape t[]=new Tape[10]; System.out.println("Enter the number of books"); n=Integer.parseInt(br.readLine()); System.out.println("ENTER BOOK DETAILS"); for(i=0;i<n;i++) { b[i]=new Books(); System.out.println("Enter the title of "+(i+1)+"th"+" book"); ta=br.readLine(); System.out.println("Enter the price of "+(i+1)+"th"+" book"); p=Float.parseFloat(br.readLine()); System.out.println("Enter the pagecount of "+(i+1)+ "th"+" book"); pa=Integer.parseInt(br.readLine()); b[i].getd(ta,p,pa); } System.out.println(); System.out.println("Enter the number of tapes"); m=Integer.parseInt(br.readLine()); System.out.println("ENTER TAPE DETAILS"); for(i=0;i<m;i++) { t[i]=new Tape();

M.Sc Computer Science Assumption College, Changanacherry.

24

System.out.println("Enter the title of "+(i+1)+"th"+" tape"); ta=br.readLine(); System.out.println("Enter the price of "+(i+1)+"th"+" tape"); p=Float.parseFloat(br.readLine()); System.out.println("Enter the play time of "+(i+1)+ "th"+" tape"); pl=Float.parseFloat(br.readLine()); t[i].getd(ta,p,pl); } System.out.println("DETAILS OF BOOKS"); for(i=0;i<n;i++) b[i].putd(); System.out.println("DETAILS OF TAPES"); for(i=0;i<m;i++) t[i].putd(); } }

M.Sc Computer Science Assumption College, Changanacherry.

25

Output
Enter the number of books 1 ENTER BOOK DETAILS Enter the title of 1th book Java Enter the price of 1th book 525 Enter the pagecount of 1th book 1250 Enter the number of tapes 2 ENTER TAPE DETAILS Enter the title of 1th tape New Malayalam Songs Enter the price of 1th tape 125.95 Enter the play time of 1th tape 56.30 Enter the title of 2th tape English Rap Enter the price of 2th tape 99.99 Enter the play time of 2th tape 10.10 DETAILS OF BOOKS TITLE : Java PRICE : 525.0 PAGE COUNT : 1250 DETAILS OF TAPES TITLE : New Malayalam Songs PRICE : 125.95 PLAY TIME : 56.3 TITLE : English Rap PRICE : 99.99 PLAY TIME : 10.1

M.Sc Computer Science Assumption College, Changanacherry.

26

Program 8
Aim : Program To Employee Database Using Abstract Class Program :
import java.io.*; import java.lang.*; abstract class Employee { int id; String name; Employee(int i,String c) { id=i; name=c; } abstract void display(); } class Teaching extends Employee { String dept1;double salary; Teaching(int i,String c,String dept,double sal) { super(i,c); dept1=dept; salary=sal; } void display() { System.out.println("Id : "+id); System.out.println("Name : "+name); System.out.println("Department : "+dept1); System.out.println("Salary : "+salary); } } class NonTeaching extends Employee { double salary1; NonTeaching(int i,String c,double sal1)
M.Sc Computer Science Assumption College, Changanacherry.

27

{ super(i,c); salary1=sal1; } void display() { System.out.println("Id System.out.println("Name System.out.println("Salary }

: "+id); : "+name); : "+salary1);

} class Professor extends Teaching { Professor(int i,String c,String dept,double sal) { super(i,c,dept,sal); } void display() { System.out.println("Id : "+id); System.out.println("Name : "+name); System.out.println("Department : "+dept1); System.out.println("Salary : "+salary); } } class Lecture extends Teaching { String grade; Lecture(int i,String c,String dept,double sal,String g) { super(i,c,dept,sal); grade=g; } void display() { System.out.println("Id : "+id); System.out.println("Name : "+name); System.out.println("Department : "+dept1); System.out.println("Salary : "+salary); System.out.println("Grade : "+grade); } }

M.Sc Computer Science Assumption College, Changanacherry.

28

class Clerical extends NonTeaching { String grade1; Clerical(int i,String c,double sal1,String g1) { super(i,c,sal1); grade1=g1; } void display() { System.out.println("Id : "+id); System.out.println("Name : "+name); System.out.println("Salary : "+salary1); System.out.println("Grade : "+grade1); } } class Administrator extends NonTeaching { Administrator(int i,String c,double sal1) { super(i,c,sal1); } void display() { System.out.println("Id : "+id); System.out.println("Name : "+name); System.out.println("Salary : "+salary1); } } class Employeedet { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Employee e; int i; String c; String dept; double sal;double sal1; String g; String g1;

M.Sc Computer Science Assumption College, Changanacherry.

29

System.out.println("Enter The Teacher Details"); System.out.println("Id of Teacher is"); i=Integer.parseInt(br.readLine()); System.out.println("Name of the Teacher"); c=br.readLine(); System.out.println("Department of Teacher"); dept=br.readLine(); System.out.println("Salary of a Teacher"); sal=Integer.parseInt(br.readLine()); Teaching t=new Teaching(i,c,dept,sal); System.out.println("Enter The NonTeaching details"); System.out.println("Id of nonTeacher is"); i=Integer.parseInt(br.readLine()); System.out.println("Name of non Teacher"); c=br.readLine(); System.out.println("Salary of non Teacher"); sal1=Integer.parseInt(br.readLine()); NonTeaching nt=new NonTeaching(i,c,sal1); System.out.println("Enter The Professor Details"); System.out.println("Id of Professor is"); i=Integer.parseInt(br.readLine()); System.out.println("Name of the Professor"); c=br.readLine(); System.out.println("Department of Professor"); dept=br.readLine(); System.out.println("Salary of a Professor"); sal=Integer.parseInt(br.readLine()); Professor p=new Professor(i,c,dept,sal); System.out.println("Enter The Lecture Details"); System.out.println("Id of Lecture is"); i=Integer.parseInt(br.readLine()); System.out.println("Name of the Lecture"); c=br.readLine(); System.out.println("Department of Lecture"); dept=br.readLine(); System.out.println("Salary of a Lecture"); sal=Integer.parseInt(br.readLine()); System.out.println("Grade of Lecture"); g=br.readLine(); Lecture l=new Lecture(i,c,dept,sal,g); System.out.println("Enter The Clerical details"); System.out.println("Id of Clerk is"); i=Integer.parseInt(br.readLine()); System.out.println("Name of the Clerk");

M.Sc Computer Science Assumption College, Changanacherry.

30

c=br.readLine(); System.out.println("Department of Clerk"); dept=br.readLine(); System.out.println("Salary of a Clerk"); sal=Integer.parseInt(br.readLine()); System.out.println("Grade of Clerk"); g1=br.readLine(); Clerical cl=new Clerical(i,c,sal1,g1); System.out.println("Enter The Administrator Details"); System.out.println("Id of Administrator is"); i=Integer.parseInt(br.readLine()); System.out.println("Name of the Administrator"); c=br.readLine(); System.out.println("Salary of a Clerk"); sal=Integer.parseInt(br.readLine()); Administrator a=new Administrator(i,c,sal1); System.out.println("Teacher Details"); t.display(); System.out.println("NonTeaching details"); nt.display(); System.out.println("Professor Details"); p.display(); System.out.println("Lecture Details"); l.display(); System.out.println("Clerical details"); cl.display(); System.out.println("Administrator Details"); a.display(); } }

M.Sc Computer Science Assumption College, Changanacherry.

31

Output :
Enter The Teacher Detail Id of Teacher is 55 Name of the Teacher Anna Department of Teacher Computer Science Salary of a Teacher 15000 Enter The NonTeaching de Id of nonTeacher is 77 Name of non Teacher Babu Salary of non Teacher 10000 Enter The Professor Deta Id of Professor is 40 Name of the Professor Gopal Department of Professor Biology Salary of a Professor 25000 Enter The Lecture Detail Id of Lecture is 10 Name of the Lecture Geetha Department of Lecture Maths Salary of a Lecture 18000 Grade of Lecture A Enter The Clerical detai Id of Clerk is 101 Name of the Clerk Leela Department of Clerk
M.Sc Computer Science Assumption College, Changanacherry.

32

Maths Salary of a Clerk 14000 Grade of Clerk C Enter The Administrator Id of Administrator is 401 Name of the Administrato Raju Salary of a Clerk 20000 Teacher Details Id : 55 Name : Anna Department : Computer Sc Salary : 15000.0 NonTeaching details Id : 77 Name : Babu Salary : 10000.0 Professor Details Id : 40 Name : Gopal Department : Biology Salary : 25000.0 Lecture Details Id : 10 Name : Geetha Department : Maths Salary : 18000.0 Grade :A Clerical details Id : 101 Name : Leela Salary : 10000.0 Grade :C Administrator Details Id : 401 Name : Raju Salary : 10000.0

M.Sc Computer Science Assumption College, Changanacherry.

33

Program 9
Aim : Program To Result Preparation. Program :
import java.io.*; interface Sports { final int smark=10; public void putwt(); } class Student { String name; int rollno; void getno(String a, int b) { name=a; rollno=b; } void putno() { System.out.println("Roll number is " +rollno); System.out.println("Name of the student is " +name); } } class Test extends Student { int m1,m2; void getmark(int x,int y) { m1=x; m2=y; } void putmark() { System.out.println("Mark of Subject1 " +m1); System.out.println("Mark of Subject2 " +m2); } }

M.Sc Computer Science Assumption College, Changanacherry.

34

class Result extends Test implements Sports { double res; public void putwt() { System.out.println("Weightage mark for sports " +smark); } void display() { putno(); putmark(); putwt(); res=m1+m2+smark; System.out.println("Total mark of Student " +res); } } class StudentInter { public static void main(String args[])throws IOException { int l,s1,s2,i; Result r[]=new Result[10]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the number of students..."); l=Integer.parseInt(br.readLine()); for(i=1;i<=l;i++) { r[i]=new Result(); System.out.println("Enter the name..."); String m=br.readLine(); System.out.println("Enter the rollno...."); int n=Integer.parseInt(br.readLine()); r[i].getno(m,n); System.out.println("Enter the two subject's marks of a student."); s1=Integer.parseInt(br.readLine()); s2=Integer.parseInt(br.readLine()); r[i].getmark(s1,s2); } System.out.println("DETAILS OF STUDENT");

M.Sc Computer Science Assumption College, Changanacherry.

35

for(i=1;i<=l;i++) { r[i].display(); } } }

M.Sc Computer Science Assumption College, Changanacherry.

36

Output
Enter the number of students... 2 Enter the name... Anna Enter the rollno.... 12 Enter the two subject's marks of a student. 87 99 Enter the name... Appu Enter the rollno.... 20 Enter the two subject's marks of a student. 89 90 DETAILS OF STUDENT Roll number is 12 Name of the student isAnna Mark of Subject187 Mark of Subject299 Weightage mark for sports 10 Total mark of Student 196.0 Roll number is 20 Name of the student isAppu Mark of Subject189 Mark of Subject290 Weightage mark for sports 10 Total mark of Student 189.0

M.Sc Computer Science Assumption College, Changanacherry.

37

Program 10
Aim : Program To Implementing Interface Program :
import java.io.*; interface Quad { void area(); void volume(); } class Rectangle implements Quad { int l,b,h; Rectangle(int length,int breadth,int height) { l=length; b=breadth; h=height; } public void area() { int ar=l*b; System.out.println("The area of Rectangle is "+ar); } public void volume() { int v=l*h*b; System.out.println("The volume of Rectangle is "+v); } } class Square implements Quad { int s; Square(int side) { s=side; } public void area( ) { int ar=s*s; System.out.println("The area of Square is "+ar);

M.Sc Computer Science Assumption College, Changanacherry.

38

} public void volume() { int v=s*s*s; System.out.println("The volume of Square is "+v); } } class Areavolume { public static void main(String args[])throws IOException { BufferedReader bi=new BufferedReader(new InputStreamReader(System.in)); int le,br,ht,sqr; System.out.println("Enter the length,breadth and height of rectangle "); le=Integer.parseInt(bi.readLine()); br=Integer.parseInt(bi.readLine()); ht=Integer.parseInt(bi.readLine()); Rectangle r=new Rectangle(le,br,ht); r.area(); r.volume(); System.out.println("Enter the sides of square"); sqr=Integer.parseInt(bi.readLine()); Square sq = new Square(sqr); sq.area(); sq.volume(); } }

M.Sc Computer Science Assumption College, Changanacherry.

39

Output :
Enter the length,breadth and height of rectangle 15 65 45 The area of Rectangle is 975 The volume of Rectangle is 43875 Enter the sides of square 7 The area of Square is 49 The volume of Square is 343

M.Sc Computer Science Assumption College, Changanacherry.

40

Program 11
Aim : Program To Implementation Of Two Packages. Program :
/*Student Package*/ package Stud; public class Student { int rol; String name; public Student(int a,String n) { rol=a; name=n; } public void show() { System.out.println("Student Roll no:"+rol); System.out.println("Student Name:"+name); } } /*Teacher Package*/ package Teach; public class Teacher { int id; String tname; public Teacher(int a,String n) { id=a; tname=n; } public void showt() { System.out.println("Teacher ID:"+id); System.out.println("Teacher Name:"+tname); } }

M.Sc Computer Science Assumption College, Changanacherry.

41

/*Main Program*/
import Stud.Student; import Teach.Teacher; import java.io.*; class StudTeachPack { public static void main(String args[])throws IOException { int n1,n2; String na1,na2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter The Student Details"); System.out.println("Enter the rollnum"); n1=Integer.parseInt(br.readLine()); System.out.println("Enter the name"); na1=br.readLine(); Student s=new Student(n1,na1); System.out.println("Enter The Teacher Details"); System.out.println("Enter the id"); n2=Integer.parseInt(br.readLine()); System.out.println("Enter the name"); na2=br.readLine(); Teacher t=new Teacher(n2,na2); System.out.println("Student Details"); System.out.println("-----------------------); s.show(); System.out.println("Teacher Details"); System.out.println("-----------------------); t.showt(); } }

M.Sc Computer Science Assumption College, Changanacherry.

42

Output :
H:\crt>javac Stud/Student.java H:\crt>javac Teach/Teacher.java H:\crt>javac StudTeachPack.java H:\crt>java StudTeachPack Enter The Student Details Enter the rollnum 1168 Enter the name Ram Enter The Teacher Details Enter the id 101 Enter the name Balu Student Details -------------------Student Roll no:1168 Student Name:Ram Teacher Details --------------------Teacher ID:101 Teacher Name:Balu

M.Sc Computer Science Assumption College, Changanacherry.

43

Program 12
Aim : Program To Exception Handling. Program :
import java.io.*; class Driverdemo extends Exception { public static void main(String args[])throws IOException { String name,add; int n,i,a; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the limit"); n=Integer.parseInt(in.readLine()); for(i=0;i<n;i++) { System.out.println("Enter the name"); name=in.readLine(); System.out.println("Enter the address"); name=in.readLine(); System.out.println("Enter the age"); a=Integer.parseInt(in.readLine()); try { if(a>60) { System.out.println("Exception occured"); throw new Driverdemo(); } } catch(Driverdemo e) { System.out.println("Exception caught "+e); } } } }

M.Sc Computer Science Assumption College, Changanacherry.

44

Output :
Enter the limit 2 Enter the name Ram Enter the address Ramvilla Enter the age 56 Enter the name Babu Enter the address Geetham Enter the age 61 Exception occured Exception caught Driverdemo

M.Sc Computer Science Assumption College, Changanacherry.

45

Program 13
Aim : Program To Counter Using Thread. Program :
import java.io.*; class Counterthread { public static void main(String arg[])throws IOException { int n; DataInputStream p; p=new DataInputStream(System.in); System.out.println("Enter the limit"); n=Integer.parseInt(p.readLine()); Thread t=Thread.currentThread(); try { for(int i=0;i<n;i++) { System.out.print(i); System.out.println(); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println("Interrupted"); } } }

M.Sc Computer Science Assumption College, Changanacherry.

46

Output :
Enter the limit 10 0 1 2 3 4 5 6 7 8 9

M.Sc Computer Science Assumption College, Changanacherry.

47

Program 14
Aim : Program To Threads using thread priority 1.1,2,3,........... 2.1,3,5,7............... 3.2,4,6,8................. Program :
import java.io.*; class Pattern1 implements Runnable { Thread t; int n; Pattern1(int p,int nu) { t=new Thread(this,"PATTERN1"); t.setPriority(p); n=nu; t.start(); } public void run() { for(int i=1;i<=n;i++) System.out.print(i+","); System.out.println(); } } class Pattern2 implements Runnable { Thread t; int n; Pattern2(int p,int nu) { t=new Thread(this,"PATTERN2"); t.setPriority(p); n=nu; t.start(); } public void run() { System.out.println();

M.Sc Computer Science Assumption College, Changanacherry.

48

for(int i=1;i<=n;i=i+2) System.out.print(i + ","); } } class Pattern3 implements Runnable { Thread t; int n; Pattern3(int p,int nu) { t=new Thread(this,"PATTERN3"); t.setPriority(p); n=nu; t.start(); } public void run() { System.out.println(); for(int i=2;i<=n;i=i+2) System.out.print(i + ","); } } class Threadpattern { public static void main(String arg[]) throws IOException { DataInputStream d=new DataInputStream(System.in); System.out.println("Enter the limit"); int n=Integer.parseInt(d.readLine()); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Pattern1 p1=new Pattern1(Thread.NORM_PRIORITY+4,n); Pattern2 p2=new Pattern2(Thread.NORM_PRIORITY+2,n); Pattern3 p3=new Pattern3(Thread.NORM_PRIORITY-1,n); try { p1.t.join(); System.out.println(); p2.t.join(); System.out.println(); p3.t.join(); } catch(InterruptedException e) {

M.Sc Computer Science Assumption College, Changanacherry.

49

System.out.println("Thread Interrupted"); } } }

M.Sc Computer Science Assumption College, Changanacherry.

50

Output :
Enter the limit 10 1,2,3,4,5,6,7,8,9,10, 1,3,5,7,9, 2,4,6,8,10,

M.Sc Computer Science Assumption College, Changanacherry.

51

Program 15
Aim : Program To Marquee Using Applet. Program :
import java.awt.*; import java.applet.*; /* <applet code="Marque" width=300 height=50> </applet> */ public class Marque extends Applet implements Runnable { String msg="Hai Friends......How Are You? "; Thread t=null; int state; boolean stopFlag; public void init() { setBackground(Color.cyan); setForeground(Color.red); } public void start() { t=new Thread(this); stopFlag=false; t.start(); } public void run() { char ch; for( ; ;) { try { repaint(); Thread.sleep(250); ch=msg.charAt(0); msg=msg.substring(1,msg.length()); msg +=ch; if(stopFlag) break;

M.Sc Computer Science Assumption College, Changanacherry.

52

} catch(InterruptedException e) { } } } public void stop() { stopFlag=true; t=null; } public void paint(Graphics g) { g.drawString(msg,50,30); } }

M.Sc Computer Science Assumption College, Changanacherry.

53

Output :

M.Sc Computer Science Assumption College, Changanacherry.

54

Program 16
Aim : Program To Graphics Using Applet Program :
import java.awt.*; import java.applet.*; /* <applet code="Picturen" width=700 height=550> </applet> */ public class Picturen extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawLine(231,22,210,10); g.drawLine(220,30,210,10); g.drawLine(256,22,256,5); g.drawLine(245,20,256,5); g.drawOval(210,20,75,75); g.setColor(Color.blue); g.fillOval(210,80,100,100); g.setColor(Color.red); g.fillOval(233,36,10,10); g.fillOval(245,35,10,10); g.setColor(Color.black); g.drawLine(244,45,245,58); g.drawLine(233,59,255,58); g.drawArc(145,60,145,120,-70,-100); } }

M.Sc Computer Science Assumption College, Changanacherry.

55

Output :

M.Sc Computer Science Assumption College, Changanacherry.

56

Program 17
Aim : Program to Event Handling Program Program :
import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="KeyEventHand" width=300 height=100> </applet> */ public class KeyEventHand extends Applet implements KeyListener { String msg=""; int X=10,Y=20; public void init() { addKeyListener(this); } public void keyPressed(KeyEvent ke) { showStatus("Key Down"); } public void keyReleased(KeyEvent ke) { showStatus("Key Up"); } public void keyTyped(KeyEvent ke) { msg+=ke.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg,X,Y); } }

Output :
M.Sc Computer Science Assumption College, Changanacherry.

57

Program 18
M.Sc Computer Science Assumption College, Changanacherry.

58

Aim : Program to Prepare a Feedback Form using AWT Control Program :


import java.awt.*; import java.applet.*; import java.awt.event.*; /* <applet code="Feedbackdemo" width=600 height=500> </applet> */ public class Feedbackdemo extends Applet implements ActionListener { String msg=" "; String str; Label form,name,ge,pl,em,co; TextField namet,emt; TextArea com; Button res,sub; CheckboxGroup gen; Checkbox male,female; Choice loc; public void init() { setLayout(null); form=new Label("FEEDBACK FORM"); form.setBounds(400,10,150,50); add(form); name=new Label("NAME"); name.setBounds(100,100,100,50); add(name); namet=new TextField(20); namet.setBounds(350,100,200,30); add(namet); ge=new Label("GENDER"); ge.setBounds(100,150,100,50); add(ge); gen=new CheckboxGroup(); male=new Checkbox("MALE",gen,true); male.setBounds(350,150,200,40); add(male); female=new Checkbox("FEMALE",gen,false);
M.Sc Computer Science Assumption College, Changanacherry.

59

female.setBounds(550,150,250,40); add(female); pl=new Label("PLACE"); pl.setBounds(100,190,200,50); add(pl); loc=new Choice(); loc.setBounds(350,200,200,140); loc.add("-Select-"); loc.add("Trivandrum"); loc.add("Kollam"); loc.add("Alappuzha"); loc.add("Pathanamthitta"); loc.add("Kottayam"); loc.add("Idukki"); loc.add("Eranakulam"); loc.add("Thrissur"); loc.add("Palakkad"); loc.add("Mallapuram"); loc.add("Kozhikode"); loc.add("Wayanad"); loc.add("Kannur"); loc.add("Kasargod"); add(loc); em=new Label("EMAIL"); em.setBounds(100,250,100,50); add(em); emt=new TextField(20); emt.setBounds(350,250,200,30); add(emt); co=new Label("COMMENTS"); co.setBounds(100,300,100,50); add(co); com=new TextArea(); com.setBounds(350,300,400,100); add(com); sub=new Button("SUBMIT"); sub.setBounds(400,550,50,30); add(sub); res=new Button("RESET"); res.setBounds(500,550,50,30); add(res); sub.addActionListener(this); res.addActionListener(this);
M.Sc Computer Science Assumption College, Changanacherry.

60

} public void actionPerformed(ActionEvent ae) { str=ae.getActionCommand(); if(str.equals("SUBMIT")) { msg="FEEDBACK FORM SUBMITTED SUCCESSFULLY"; } else if(str.equals("RESET")) { msg="FORM RESETTED. ENTER NEW DATA"; namet.setText(" "); emt.setText(" "); com.setText(" "); gen.setSelectedCheckbox(male); male.setState(false); female.setState(false); loc.select("-Select-"); } else { msg="WRONG COMMAND"; } repaint(); } public void paint(Graphics g) { g.drawString(msg,400,650); } }

Output :
M.Sc Computer Science Assumption College, Changanacherry.

61

Program 19
M.Sc Computer Science Assumption College, Changanacherry.

62

Aim : Program To Flow Laylout And Border Layout Program :


/** Flow Laylout */ import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="FlowLayoutDemo" width=250 height=200> </applet> */ public class FlowLayoutDemo extends Applet implements ItemListener { String msg=" "; Checkbox crab,prawn,tuna,shark; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); crab = new Checkbox("CRAB",null,true); prawn= new Checkbox("PRAWN"); tuna = new Checkbox("TUNA"); shark = new Checkbox("SHARK"); add(crab); add(prawn); add(tuna); add(shark); crab.addItemListener(this); prawn.addItemListener(this); tuna.addItemListener(this); shark.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg= "Current state: "; g.drawString(msg,6,80); msg= "CRAB: "+ crab.getState();
M.Sc Computer Science Assumption College, Changanacherry.

63

g.drawString(msg,6,100); msg= "PRAWN: "+ prawn.getState(); g.drawString(msg,6,120); msg= "TUNA: "+ tuna.getState(); g.drawString(msg,6,140); msg= "SHARK: "+ shark.getState(); g.drawString(msg,6,160); } }

Output :
M.Sc Computer Science Assumption College, Changanacherry.

64

Program
M.Sc Computer Science Assumption College, Changanacherry.

65

/** Border Layout */


import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="BorderLayoutDemo" width=250 height=200> </applet> */ public class BorderLayoutDemo extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("Kerala Tourism"),BorderLayout.NORTH); add(new Button("Beaches"),BorderLayout.SOUTH); add(new Button("Heritage sites"),BorderLayout.EAST); add(new Button("Backwaters"),BorderLayout.WEST); String msg=" Kerala is known for its tropical backwaters and\n " +"\npristine beaches such as Kovalam \n" + "\n Popular attractions in the state " +"\n include the beaches at Kovalam Cherai and Varkala \n"+ "\n The hill stations of Munnar\n"+ "\nNelliampathi Ponmudi and Wayanad "; add(new TextArea(msg),BorderLayout.CENTER); } }

Output :
M.Sc Computer Science Assumption College, Changanacherry.

66

Program 20
M.Sc Computer Science Assumption College, Changanacherry.

67

Aim : Program To PopUp Menu Program :


import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="Popupmenu" width=600 height=600> </applet> */ class MenuFrame extends Frame implements ActionListener { String msg=" "; MenuFrame(String title) { super(title); MenuBar mbar=new MenuBar(); setMenuBar(mbar); Menu file=new Menu("File"); MenuItem nw,open,save,line,print,exit; file.add(nw=new MenuItem("New"+" "+"Ctrl+N")); file.add(open=new MenuItem("Open"+" "+"Ctrl+O")); file.add(save=new MenuItem("Save"+" "+"Ctrl+S")); file.add(line=new MenuItem("-")); file.add(print=new MenuItem("Print"+" "+"Ctrl+p")); file.add(exit=new MenuItem("Exit")); mbar.add(file); Menu view=new Menu("View"); Menu explorerbars=new Menu("Explorer Bar"); MenuItem history,search; explorerbars.add(history=new MenuItem("History")); explorerbars.add(search=new MenuItem("Search")); view.add(explorerbars); mbar.add(view); nw.addActionListener(this); open.addActionListener(this); save.addActionListener(this); line.addActionListener(this); print.addActionListener(this); exit.addActionListener(this); history.addActionListener(this);
M.Sc Computer Science Assumption College, Changanacherry.

68

search.addActionListener(this); } public void actionPerformed(ActionEvent ae) { msg="You have selected \t "; String arg=(String)ae.getActionCommand(); msg+=arg; repaint(); } public void paint(Graphics g) { g.drawString(msg,10,220); } } public class Popupmenu extends Applet { Frame f; public void init() { f=new MenuFrame("Menu Demo"); int width=Integer.parseInt(getParameter("width")); int height=Integer.parseInt(getParameter("height")); setSize(new Dimension(width,height)); f.setSize(width,height); f.setVisible(true); } public void start() { f.setVisible(true); } public void stop() { f.setVisible(false); } }

Output :
M.Sc Computer Science Assumption College, Changanacherry.

69

Program 21
M.Sc Computer Science Assumption College, Changanacherry.

70

Aim : Program To Simple calculator using RMI. Program : /**Interface */


import java.rmi.*; public interface Calintf extends Remote { int add(int d1,int d2) throws RemoteException; int sub(int d1,int d2) throws RemoteException; int mul(int d1,int d2) throws RemoteException; int div(int d1,int d2) throws RemoteException; }

/**Implement ing Interface */


import java.rmi.*; import java.rmi.server.*; public class Calimp extends UnicastRemoteObject implements Calintf { public Calimp() throws RemoteException { } public int add(int d1,int d2) throws RemoteException { return d1+d2; } public int sub(int d1,int d2) throws RemoteException { return d1-d2; } public int mul(int d1,int d2) throws RemoteException { return d1*d2; } public int div(int d1,int d2) throws RemoteException { return d1/d2; } }

M.Sc Computer Science Assumption College, Changanacherry.

71

/**Server class*/
import java.net.*; import java.rmi.*; public class Calserver { public static void main(String args[]) { try { Calimp c=new Calimp(); Naming.rebind("rmi://localhost/calserver",c); System.out.println("Server is ready"); } catch(Exception e) { System.out.println("Exception "+e); } } }

/**Client Class*/
import java.rmi.*; import java.io.*; public class Calclient { public static void main(String arg[]) { BufferedReader br=new BufferedReader( new InputStreamReader(System.in)); String ch; int cn; String name="rmi://localhost/calserver"; try { Calintf cal=(Calintf)Naming.lookup(name); do { System.out.println("Enter first no : "); int d1=Integer.parseInt(br.readLine()); System.out.println("Enter second no : ");
M.Sc Computer Science Assumption College, Changanacherry.

72

int d2=Integer.parseInt(br.readLine()); System.out.println("Enter the operator(+,-,*,/)"); ch=br.readLine(); if(ch.equals("+")) System.out.println("Sum : "+cal.add(d1,d2)); else if(ch.equals("-")) System.out.println("Difference : "+cal.sub(d1,d2)); else if(ch.equals("*")) System.out.println("Product : "+cal.mul(d1,d2)); else if(ch.equals("/")) System.out.println("Quotient : "+cal.div(d1,d2)); else System.out.println("Wrong Choice"); System.out.println("Do you want to continue?(0/1)"); cn=Integer.parseInt(br.readLine()); }while(cn!=0); } catch(Exception e) { System.out.println("Exception : "+e); } } }

Output :
M.Sc Computer Science Assumption College, Changanacherry.

73

H:\crt\RMI>javac Calintf.java H:\crt\RMI>javac Calimp.java H:\crt\RMI>javac Calserver.java H:\crt\RMI>javac Calclient.java H:\crt\RMI>rmic Calimp H:\crt\RMI>start rmiregistry H:\crt\RMI>java Calserver Server is ready H:\crt\RMI>java Calclient 192.168.1.2 Enter first no : 23 Enter second no : 44 Enter the operator(+,-,*,/) + Sum : 67 Do you want to continue?(0/1) 0 H:\crt\RMI>java Calclient 192.168.1.2 Enter first no : 11 Enter second no : 2 Enter the operator(+,-,*,/) * Product : 22 Do you want to continue?(0/1) 1 Enter first no : 11

Enter second no : 33
M.Sc Computer Science Assumption College, Changanacherry.

74

Enter the operator(+,-,*,/) + Sum : 44 Do you want to continue?(0/1) 1 Enter first no : 55 Enter second no : 33 Enter the operator(+,-,*,/) Difference : 22 Do you want to continue?(0/1) 1 Enter first no : 78 Enter second no : 2 Enter the operator(+,-,*,/) / Quotient : 39 Do you want to continue?(0/1) 0

Project I
M.Sc Computer Science Assumption College, Changanacherry.

75

q1:

Employee and Department database


Create table Emptable and Depttable and Insert values . Depttable :-Dno(primary key),Dname,Floor Emptable:-Empno(primary key),Empname,Dno(Foreign Key),Salary,Age

Ans:
SQL> create table depttable(Dno varchar(10) PRIMARY KEY,Dname varchar(20),Floor varchar(10)); Table created. SQL> insert into depttable values('D-501','Toy','F-102'); 1 row created. SQL> insert into depttable values('D-503','Shoe','S-202'); 1 row created. SQL> insert into depttable values('D-505','Clothes','T-302'); 1 row created. SQL> insert into depttable values('D-506','Books','F-109'); 1 row created. SQL> insert into depttable values('D-507','Toy','S-210'); 1 row created. SQL> insert into depttable values('D-504','Sweets','T-307'); 1 row created. SQL> insert into depttable values('D-509','Jewels','F-110'); 1 row created.
M.Sc Computer Science Assumption College, Changanacherry.

76

SQL> select * from depttable ; DNO DNAME FLOOR ---------- -------------------- ---------D-501 Toy F-102 D-503 Shoe S-202 D-505 Clothes T-302 D-506 Books F-109 D-507 Toy S-210 D-504 Sweets T-307 D-509 Jewels F-110 7 rows selected. SQL> create table emptable(Empno varchar(10) PRIMARY KEY,Ename varchar(20), Dno varchar(20),Salary numeric(10),Age numeric(10), FOREIGN KEY(Dno) REFERENCES depttable(Dno)); Table created. SQL> insert into emptable values('E12','Anna','D-507',8000,35); 1 row created. SQL> insert into emptable values('E15','Ram','D-505',5000,55); 1 row created. SQL> insert into emptable values('E16','Raj','D-503',3000,45); 1 row created. SQL> insert into emptable values('E17','Leela','D-504',6500,40); 1 row created. SQL> insert into emptable values('E18','Kuttu','D-501',4000,42); 1 row created. SQL> insert into emptable values('E20','Biju','D-504',9500,30); 1 row created.

M.Sc Computer Science Assumption College, Changanacherry.

77

SQL> insert into emptable values('E25','Siju','D-509',1000,25); 1 row created. SQL> insert into emptable values('E26','Giri','D-506',2500,20); 1 row created. SQL> select * from emptable; EMPNO ENAME DNO SALARY AGE ---------- -------------------- -------------------- ---------- -E12 Anna D-507 8000 35 E15 Ram D-505 5000 55 E16 Raj D-503 3000 45 E17 Leela D-504 6500 40 E18 Kuttu D-501 4000 42 E20 Biju D-504 9500 30 E25 Siju D-509 1000 25 E26 Giri D-506 2500 20 8 rows selected.

q2: Ans :
Find out the name of all employees.

SQL> Select Ename from emptable; ENAME -------------------Anna Ram Raj Leela Kuttu Biju Siju Giri 8 rows selected.

q3:

M.Sc Computer Science Assumption College, Changanacherry.

78

Retrive the names of depatartment in Assending order employees in desending order.

and their

Ans :
SQL> select Dname ,Ename from depttable,emptable where depttable.Dno=emptable.Dno order by Dname as c, Ename desc; DNAME -------------------Books Clothes Jewels Shoe Sweets Sweets Toy Toy ENAME ----------Giri Ram Siju Raj Leela Biju Kuttu Anna

8 rows selected.

q4 : Ans :
Find Name,Age and Salary of Raj and Ram.

SQL> select Ename,Age,Salary from emptable where Ename IN('Raj','Ram'); ENAME AGE SALARY ------- ------------------------Ram 55 5000 Raj 45 3000

q5 :
Find out all the employee names of emptable with a any where in the name.

Ans :

M.Sc Computer Science Assumption College, Changanacherry.

79

SQL> select Ename from emptable where Ename like ('%a%'); ENAME -------------------Anna Ram Raj Leela

q6 : Ans :
Find out which employee either works in Toy or Shoe department.

SQL> select Ename, Dname from emptable,depttable where emptable.Dno=depttable.Dno and Dname IN('Toy','Shoe'); ENAME DNAME ---------- -----------Anna Toy Raj Shoe Kuttu Toy

q7: Ans :
Dispaly the Department of employee Raj.

SQL> select Dname from depttable,emptable where Ename='Raj' and emptable.Dno=depttable.Dno; DNAME ---------Shoe

q8 :

M.Sc Computer Science Assumption College, Changanacherry.

80

Modify the emptable.To include 2 more fields,Manager varchar(10) and Job varchar(10) and Insert values;

Ans :
SQL> alter table emptable add(Manager varchar(25),Job varchar(10)); Table altered. SQL> select * from emptable; EMPNO ENAME DNO SALARY AGE MANAGER JOB ------------------------- -------------------- -------- --------------E12 E15 E16 Anna Ram Raj D-507 8000 D-505 5000 D-503 3000 35 55 45

EMPNO ENAME DNO SALARY AGE MANAGER JOB ------------------------- -------------------- -------- --------------E17 E18 E20 Leela Kuttu Biju D-504 6500 40 42 30

D-501 4000 D-504 9500

EMPNO ENAME DNO SALARY AGE MANAGER JOB ------------------------- -------------------- -------- --------------E25 E26 Siju Giri D-509 1000 D-506 2500 25 20

8 rows selected.

M.Sc Computer Science Assumption College, Changanacherry.

81

SQL> update emptable SET Manager='Biju',job='Ast.Mngr' where Empno='E12'; 1 row updated. SQL> update emptable SET Manager='Anil',job='Sales' where Empno='E15'; 1 row updated. SQL> update emptable SET Manager='Rajan',job='Executive' where Empno='E16'; 1 row updated. SQL> update emptable SET Manager='Biju',job='Clerk' where Empno='E17'; 1 row updated. SQL> update emptable SET Manager='Rani',job='Accounter' where Empno='E18'; 1 row updated. SQL> update emptable SET Manager='Null',job='Manager' where Empno='E20'; 1 row updated. SQL> update emptable SET Manager='Nila',job='Sweeper' where Empno='E25'; 1 row updated. SQL> update emptable SET Manager='Geetha',job='Sweeper' where Empno='E26'; 1 row updated. SQL> select* from emptable; EMPNO ENAME DNO SALARY AGE MANAGER JOB ------------------------- -------------------- -------- --------------------E12 Anna D-507 8000 35 Biju Ast.Mngr

M.Sc Computer Science Assumption College, Changanacherry.

82

E15 E16

Ram Raj

D-505 5000 D-503 3000

55 45

Anil Rajan

Sales Executive

EMPNO ENAME DNO SALARY AGE MANAGER JOB ------------------------- -------------------- -------- ----------------------E17 E18 E20 Leela Kuttu Biju D-504 6500 40 42 30 Biju Rani Null Clerk Accounter Manager

D-501 4000 D-504 9500

EMPNO ENAME DNO SALARY AGE MANAGER JOB ------------------------- -------------------- -------- ---------------------E25 E26 Siju Giri D-509 D-506 1000 2500 25 20 Nila Geetha Sweeper Sweeper

8 rows selected.

q9 :
Dispaly the Empno,Ename and job of employee who are not Clerk and Whose salary is less than the salary of Clerk.

Ans :

SQL> select Empno,Ename,job from emptable where salary<any(select salary from emptable where job='Clerk') and job!='Clerk'; EMPNO ENAME JOB ---------- -------------------- ---------E15 Ram Sales E16 Raj Executive E18 Kuttu Accounter E25 Siju Sweeper E26 Giri Sweeper

M.Sc Computer Science Assumption College, Changanacherry.

83

q10 :
Select the name of employee and department of all employees from emptable,depttable.

Ans :
SQL> select Ename,Dname from emptable,depttable where emptable.Dno=depttable.Dno; ENAME DNAME -------------- --------------Anna Toy Ram Clothes Raj Shoe Leela Sweets Kuttu Toy Biju Sweets Siju Jewels Giri Books

M.Sc Computer Science Assumption College, Changanacherry.

84

Project II
q1 :

Student Database
Create Studtable: Studtable :-Name,Rollno,Date of Birth,Sub1,Sub2,Sub3,Sub4,Sub5

Ans :
SQL> create table Studtable(name varchar(10),rollno numeric(5)PRIMARY KEY,DOB date,sub1 numeric(5),sub2 numeric(5), sub3 numeric(5),sub4 numeric(5),sub5 numeric(5)); Table created.

q2 :
Insert 5 row values and display 5 rows.

Ans :
SQL> insert into Studtable values('amith',110,TO_DATE('12-121989','DD-MM-YY'),89,75,99,95,87); 1 row created. SQL> insert into Studtable values('aruna',118,TO_DATE('15-7-1988','DD-MMYY'),88,56,75,65,50); 1 row created. SQL> insert into Studtable values('sunitha',225,TO_DATE('4-8-1990','DD-MMYY'),75,65,88,77,90); 1 row created. SQL> insert into Studtable values('krishna',544,TO_DATE('17-4-1995','DD-MMYY'),88,70,80,78,65);

M.Sc Computer Science Assumption College, Changanacherry.

85

1 row created. SQL> insert into Studtable values('leela',780,TO_DATE('8-8-1985','DD-MMYY'),70,70,50,60,65); 1 row created. SQL> select * from Studtable; NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 ---------- ---------- --------- ---------- ---------- ---------- --------------------amith 110 12-DEC-89 89 75 99 95 87 aruna 118 15-JUL-88 04-AUG-90 88 75 56 65 75 88 65 77 50 90

sunitha 225

NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 ---------- ---------- --------- ---------- ---------- ---------- --------------------krishna 544 leela 780 17-APR-95 08-AUG-85 88 70 70 70 80 50 78 60 65 65

q3 : Ans :
Modify the studtable to include 2 fields total and percent.

SQL> alter table Studtable ADD(total numeric(10),percent numeric(10)); Table altered. SQL> select * from Studtable; NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PERCENT ---------- ---------- --------- ---------- ---------- ---------- ------------------------------amith 110 12-DEC-89 aruna 118 15-JUL-88 89 88 75 56 99 75 95 65 87 50

M.Sc Computer Science Assumption College, Changanacherry.

86

sunitha 225 04-AUG-90 75

65

88

77

90

NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PERCENT ---------- ---------- --------- ---------- ---------- ---------- ------------------------------krishna 544 17-APR-95 88 leela 780 08-AUG-85 70 70 70 80 50 78 60 65 65

q4 : Ans :
Calculate Total and Percent and dispaly.

SQL> update Studtable SET total=sub1+sub2+sub3+sub4+sub5; 5 rows updated. SQL> update Studtable SET percent=total/500*100; 5 rows updated. SQL> select * from Studtable; NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PERCENT ---------- ---------- --------- ---------- ---------- ---------- ------------------------------amith 110 12-DEC-89 89 aruna 118 15-JUL-88 88 sunitha 225 04-AUG-90 75 75 56 65 99 75 88 95 65 77 87 50 90 445 334 395 89 67 79

NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PERCENT ---------- ---------- --------- ---------- ---------- ---------- ------------------------------krishna 544 17-APR-95 88 leela 780 08-AUG-85 70 70 70 80 50 78 65 381 315 76 63

60 65

M.Sc Computer Science Assumption College, Changanacherry.

87

q5 :
Select the details of student whose name starts with a having 5 letters.

Ans:
SQL> select * from Studtable where name like('a____'); NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PERCENT ---------- ---------- --------- ---------- ---------- ---------- ------------------------------amith 110 12-DEC-89 89 aruna 118 15-JUL-88 88 75 56 99 75 95 65 87 50 445 334 89 67

q6 : Ans :
Give additional 5 marks to those student whose % is above 80.

SQL> select name,rollno,DOB,sub1,sub2,sub3,sub4,sub5,total+5 as total,(total+5)/5 as percent from Studtable where percent > 80; NAME ROLLNO DOB SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL PERCENT ---------- ---------- --------- ---------- ---------- ---------- ------------------------------amith 110 12-DEC-89 89 75 99 95 87 450 90

q7 :
Display number of rows in the table;

Ans :
SQL> select count(*) from Studtable; COUNT(*) ---------5
M.Sc Computer Science Assumption College, Changanacherry.

88

q8 :
Select the name of student with highest mark;

Ans :
SQL> select name from Studtable where (total=(Select max(total) from Studtable)); NAME ---------Amith

q9 :
Select name and age whose age is greater than the others.

Ans :
SQL> select name,(TO_CHAR(sysdate,'YYYY')-TO_CHAR(DOB,'YYYY')) as age from Studtable where DOB= (Select min(DOB) from Studtable); NAME AGE ---------- ---------leela 26

M.Sc Computer Science Assumption College, Changanacherry.

89

Project III
q1 :

Item Database
Create item table. Item:- Itemno, Itemname, Itemprice,Qtyonhand

Ans:
SQL> create table item(itemno numeric(5)PRIMARY KEY,itemname varchar(10),itemprice numeric(3,2),qtyonhand numeric(3)); Table created.

q2 :
Insert values And display all details of item.

Ans :
SQL> insert into item values(1,'Screw',2.25,50); 1 row created. SQL> insert into item values(2,'Nut',5.00,110); 1 row created. SQL> insert into item values(3,'Bolt',3.99,75); 1 row created. SQL> insert into item values(4,'Hammer',9.99,125); 1 row created. SQL> insert into item values(5,'Washer',1.99,100); 1 row created.
M.Sc Computer Science Assumption College, Changanacherry.

90

SQL> insert into item values(6,'Nail',0.99,300); 1 row created. SQL> select * From item; ITEMNO ITEMNAME ITEMPRICE QTYONHAND ---------- ------------------ ----------------- -------------1 Screw 2.25 50 2 Nut 5 110 3 Bolt 3.99 75 4 Hammer 9.99 125 5 Washer 1.99 100 6 Nail .99 300 6 rows selected.

q3:
Find the total value of each item based on qtyonhand.

Ans :
SQL> select itemname,itemprice*qtyonhand "Total value" from item; ITEMNAME Total value ---------- -------------------Screw 112.5 Nut 550 Bolt 299.25 Hammer 1248.75 Washer 199 Nail 297 6 rows selected.

q4: Ans:
Display item with unit price of atleast 5

SQL> select UPPER(itemname) as item,itemprice from item where itemprice>=5;


M.Sc Computer Science Assumption College, Changanacherry.

91

ITEM ITEMPRICE ------------ -----------NUT 5 HAMMER 9.99

q5:
Find item with a unit price between 2 and 5.

Ans:
SQL> select * from item where itemprice between 2 and 5; ITEMNO ITEMNAME ITEMPRICE QTYONHAND ------------- ---------- ---------- --------------------------1 Screw 2.25 50 2 Nut 5 110 3 Bolt 3.99 75

q6 :
Find the total,average,highest and lowest unit prices

Ans:
SQL> select sum(itemprice),avg(itemprice),max(itemprice), min(itemprice) from item; SUM(ITEMPRICE) AVG(ITEMPRICE) MAX(ITEMPRICE) MIN(ITEMPRICE) ---------------------- ----------------------- ----------------------- ---------------------24.21 4.035 9.99 .99

q7:
Display item price rounded to nearest price.

Ans :
SQL> select itemname,ROUND(itemprice,0) from item;

M.Sc Computer Science Assumption College, Changanacherry.

92

ITEMNAME ROUND(ITEMPRICE,0) ----------------- ---------------------------Screw 2 Nut 5 Bolt 4 Hammer 10 Washer 2 Nail 1 6 rows selected.

q8:
Delete records in which itemprice lessthan 2 .

Ans :
SQL> delete from item where itemprice<2; 2 rows deleted.

q9 : Ans :
Display after deletion.

SQL> select * from item; ITEMNO ITEMNAME ITEMPRICE QTYONHAND --------------- -------------- ---------- -----------------------1 Screw 2.25 50 2 Nut 5 110 3 Bolt 3.99 75 4 Hammer 9.99 125

M.Sc Computer Science Assumption College, Changanacherry.

You might also like