Vous êtes sur la page 1sur 62

LAB CYCLE NO:1:AirLine Reservations

import java.io.*; import java.util.Scanner; public class Airlines{ public static void main(String args[]) { int i=0; flights ob=new flights(); Scanner in=new Scanner(System.in); while(i<10) int ch=0; { i++;

System.out.println("\nPress 1 to for reservations "); ch=in.nextInt(); if(ch==1) { ob.checkclass(); } class res{ String name; private int seatno; String classps; public void setdataf(int fcount) { try{ BufferedReader planes=new BufferedReader(new InputStreamReader(System.in)); System.out.println("\nEnter Your name"); name=planes.readLine(); seatno=fcount+1; } }}

classps="First class"; display(); }catch(IOException e) e.printStackTrace(); { } }

public void setdatae(int ecount) { try{ BufferedReader planes=new BufferedReader(new InputStreamReader(System.in)); System.out.println("\nEnter Your name"); name=planes.readLine(); seatno=ecount+1; classps="Second class"; display(); }catch(IOException e) e.printStackTrace(); public void display() { int n; System.out.println("\nSeating chart"); for(int i=0;i<10;i++) n=i+1; System.out.print(n+"."+flights.chart[i]+" "); System.out.println("Passanger name= "+name); System.out.println("Passanger class= "+ classps); System.out.println("Passanger Seatno= "+ seatno); System.out.println("Next Flight in three hours "); }} } { { } }

class flights { private int classp; static boolean chart[]; int ecount; int fcount; res rs; public flights() classp=0; ecount=5; fcount=0; chart=new boolean[10]; for(int i=0;i<10;i++) chart[i]=false; rs=new res(); } } { { {

public void checkclass() try {

BufferedReader planes=new BufferedReader(new InputStreamReader(System.in)); System.out.println("\nEnter the class to travel \n 1.First class \n 2.Economy"); classp=Integer.parseInt(planes.readLine()); if(classp==2) { {

if(ecount>=10) int ch;

System.out.println("\nEconomy class is full in this flight \n Press 1 if want in first class"); ch=Integer.parseInt(planes.readLine()); if(ch==1)

chart[fcount]=true; rs.setdataf(fcount); fcount++; else { }

chart[ecount]=true; rs.setdatae(ecount); ecount++; else { } }

chart[fcount]=true; rs.setdataf(fcount); fcount++; }

}catch(IOException e) { e.printStackTrace(); } }}

OutPut:
Press 1 to for reservations 1 Enter the class to travel 1.First class 2.Economy 1 Enter Your name Sankar Rao Seating chart 1.true 2.false 3.false 4.false 5.false 6.false 7.false 8.false 9.false 10.false Passanger name= Sankar Rao

Passanger class= First class Passanger Seatno= 1 Next Flight in three hours Press 1 to for reservations 1 Enter the class to travel 1.First class 2.Economy 2 Enter Your name Talari Seating chart 1.true 2.false 3.false 4.false 5.false 6.true 7.false 8.false 9.false 0.false Passanger name= Talari Passanger class= Second class Passanger Seatno= 6 Next Flight in three hours Press 1 to for reservations

LAB CYCLE:2 Data Encryption and Data Decryption


import java.io.*; class trans{ int n; void encry(int e) {

int a[]=new int[4]; int i=0; while(e>0) {

int r=e%10; r+=7; r=r%10; a[i++]=r; e=e/10; int t=a[0]; a[0]=a[1]; a[1]=t; t=a[2]; a[2]=a[3]; a[3]=t; for(int j=0;j<4;j++) e=e*10+a[j]; n=e; } { { } }

void decry(int e) int i=0;

int a[]=new int[4];

while(e>0) int r=e%10; a[i++]=r; e=e/10; int t=a[0]; a[0]=a[1]; a[1]=t; t=a[2]; a[2]=a[3]; a[3]=t; for(int j=0;j<4;j++) a[j]+=3; a[j]=a[j]%10; e=e*10+a[j]; n=e; }

public static void main(String[] args) try { trans t=new trans();

DataInputStream ds=new DataInputStream(System.in); System.out.println("Enter the four digit number||||"); t.n=Integer.parseInt(ds.readLine()); System.out.println("Encrypt the number"); t.encry(t.n); System.out.println(t.n); System.out.println("decrype number");

t.decry(t.n); System.out.println(t.n); }catch(Exception e) { System.out.println(e); } } }

OUTPUT:
Enter the four digit number|||| 4567 Encrypt the number 3412 decrype number 4567

Lab Cycle:3 Credit Balance


import java.io.*; import java.util.*; class lab3 { static final int limit=4000; public static void main(String[] args) { String name[]=new String[10]; int acc[]=new int[10]; int bbal[]=new int[10]; int totalc[]=new int[10]; int totalch[]=new int[10]; int newbal[]=new int[10]; int i=0; try{ DataInputStream br=new DataInputStream(System.in); System.out.println("enter the number of customer u want !"); int n=Integer.parseInt(br.readLine()); for(i=0;i<n;i++){ System.out.println("enter the customer name"); name[i]=br.readLine(); System.out.println("enter the customer account number"); acc[i]=Integer.parseInt(br.readLine());

System.out.println("enter the customer begining balence"); bbal[i]=Integer.parseInt(br.readLine()); System.out.println("enter the customer total credits"); totalc[i]=Integer.parseInt(br.readLine()); System.out.println("enter the customer total charges"); totalch[i]=Integer.parseInt(br.readLine()); newbal[i]=bbal[i]+totalch[i]-totalc[i]; } System.out.println("****The Credit Balence Details*****"); for(i=0;i<n;i++) { if(newbal[i]>limit) {

System.out.println("The New Balence is "+newbal[i]); System.out.println("sorry ur credit limit is exceed Mr:"+name[i]); } else { System.out.println("The New Balence is "+newbal[i]); System.out.println("Thank You! Mr:"+name[i]); } } }catch(Exception e){} } }

Output:
enter the number of customer u want ! 2 enter the customer name Sankar Rao enter the customer account number 135 enter the customer begining balence 2000

enter the customer total credits 900 enter the customer total charges 89 enter the customer name Talari enter the customer account number 900 enter the customer begining balence 8900 enter the customer total credits 900 enter the customer total charges 98 ****The Credit Balence Details***** The New Balence is 1189 Thank You! Mr:Sankar Rao The New Balence is 8098 sorry ur credit limit is exceed Mr:Talari

Lab Cycle:4 Employee Salary System Using Inheritance


import java.io.*; class Emp { int salary; void getData()throws IOException { String st; int id; System.out.println("enter empid and empname"); DataInputStream k=new DataInputStream(System.in); id=Integer.parseInt(k.readLine()); st=k.readLine(); } } class Semp extends Emp { int x; void getDetails()throws IOException { getData(); System.out.println("enter salary of the employee"); DataInputStream k2=new DataInputStream(System.in); x=Integer.parseInt(k2.readLine()); int cal()throws IOException { salary= x; return salary; class Hemp extends Emp { int x1,x2; void getDetails()throws IOException {getData(); } } }

System.out.println("enter no of hrs he worked N payrate of the Emp"); DataInputStream k2=new DataInputStream(System.in); x1=Integer.parseInt(k2.readLine()); x2=Integer.parseInt(k2.readLine()); int cal() { if(x1>30) System.out.println("Base Salary is :"+ (x1*x2)); int t=x1-30; t=t*(x2+10); System.out.println("OverTime Payment is:"+t); x1=(x1*x2)+t; return(x1); }} }

class Cemp extends Emp { int x; void getDetails()throws IOException { getData(); System.out.println("enter sales of the employee"); DataInputStream k2=new DataInputStream(System.in); x=Integer.parseInt(k2.readLine()); int cal()throws IOException { salary=(x*10)/100; return salary; } } class Scemp extends Cemp { int x1,x3; int cal()throws IOException }

{ getDetails(); System.out.println("enter salary of the employee"); DataInputStream k2=new DataInputStream(System.in); x1=Integer.parseInt(k2.readLine()); x3=super.cal(); x3=x3+x1; x3=x3+x3*10/100; return(x3); } } class lab4 { public static void main(String arg[])throws IOException { int q; Semp s=new Semp(); Hemp h=new Hemp(); Cemp c=new Cemp(); Scemp sc=new Scemp(); int ch,sal; System.out.println("1.salaried employee"); System.out.println("2.hourly employee"); System.out.println("3.commisioned employee"); System.out.println("4.salaried comissioned employee");

System.out.println("Select the type of the employee"); DataInputStream t=new DataInputStream(System.in); ch=Integer.parseInt(t.readLine());

if(ch==1) { s.getDetails(); q=s.cal(); System.out.println("The Salary Of The Employee Is: "+q); } if(ch==2) { h.getDetails(); q=h.cal(); System.out.println("The Salary Of The Employee Is: "+q); if(ch==3) { }

c.getDetails(); q=c.cal(); System.out.println("The Salary Of The Employee Is: "+q); if(ch==4) q=sc.cal(); System.out.println("The Salary Of The Employee Is: "+q); } }} { }

Output:
1.salaried employee 2.hourly employee 3.commisioned employee 4.salaried comissioned employee Select the type of the employee 1

enter empid and empname 100 Sankar Rao enter salary of the employee 9000 The Salary Of The Employee Is: 9000

1.salaried employee 2.hourly employee 3.commisioned employee 4.salaried comissioned employee Select the type of the employee 4 enter empid and empname 135 Talari enter sales of the employee 8900 enter salary of the employee 345000 The Salary Of The Employee Is: 380479

Lab Cycle:5 String Manipulation


import java.io.*; class lb5 { public static void main(String args[])throws IOException { String str,s[],t[],search; int slen,i,j,count,tcount=0,flag; int lcount=0; DataInputStream in=new DataInputStream(System.in); str=in.readLine(); s=str.split(" "); slen=s.length+1; t=new String[slen+1]; for(i=0;i<slen-1;i++) { count=0; search=s[i]; for(j=0;j<slen-1;j++) {

if(search.equalsIgnoreCase(s[j])) count++; flag=0; for(int k=0;k<slen;k++) { }

if(search.equalsIgnoreCase(t[k])) flag=1; if(flag!=1) } {

lcount=search.length();

tcount++; t[tcount]=search; System.out.println("\n"+lcount+" letter Word="+t[tcount]+" has occoured "+count+" times"); } } } }

Output:
to be or to be

2 letter Word=to has occoured 2 times

2 letter Word=be has occoured 2 times

2 letter Word=or has occoured 1 times

Lab Cycle:6 Displaying Circles and Lines Using Threads


import java.awt.*; import java.util.*; public class Circle extends Canvas implements Runnable { Thread t; int px1,py1; int px2,py2; int px3,py3; public Circle() {t=new Thread(this); t.start(); setBackground(new Color(128,128,0));} public void run(){ px1=(int)((500-300+1) * java.lang.Math.random() +1); py1=(int)((500-300+1) * java.lang.Math.random() +1); px2=(int)((500-300+1) * java.lang.Math.random() +1); py2=(int)((500-300+1) * java.lang.Math.random() +1); px3=(int)((500-300+1) * java.lang.Math.random() +1); py3=(int)((500-300+1) * java.lang.Math.random() +1); repaint();} public void paint(Graphics g){ g.setColor(new Color(64,0,0));

g.drawOval(px1,py1,100,100); try{ t.sleep(1000);} catch(InterruptedException ee){ System.out.println(ee + " ");} g.drawOval(px2,py2,100,100); try{ t.sleep(1000);} catch(InterruptedException ee){ System.out.println(ee + " ");} t.stop(); t=new Thread(this); t.start();}}

//Line import java.awt.*; import java.util.*; public class Line extends Canvas implements Runnable { Thread t; int px1,py1; int px2,py2; int px3,py3; public Line() {t=new Thread(this); t.start();

setBackground(new Color(128,128,0));} public void run(){ px1=(int)((300-1+1) * java.lang.Math.random() +1); py1=(int)((300-1+1) * java.lang.Math.random() +1); px2=(int)((300-1+1) * java.lang.Math.random() +1); py2=(int)((300-1+1) * java.lang.Math.random() +1); px3=(int)((300-1+1) * java.lang.Math.random() +1); py3=(int)((300-1+1) * java.lang.Math.random() +1); repaint();} public void paint(Graphics g){ g.setColor(new Color(128,0,128)); g.drawOval(px1,py1,px1 + 100,py1 +100); try{ t.sleep(1000);} catch(InterruptedException ee){ System.out.println(ee + " ");} g.drawLine(px2,py2,px2+ 100,py2 +100); try{ t.sleep(1000);} catch(InterruptedException ee){ System.out.println(ee + " ");} g.drawLine(px3,py3,px3 +100,py3 +100); try{ t.sleep(1000);} catch(InterruptedException ee){

System.out.println(ee + " ");} t.stop(); t=new Thread(this); t.start();} } //Shapes import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Shapes extends JFrame{ Line line; Circle circle; Panel p; Container c; Shapes(){ c=getContentPane(); p=new Panel(); p.setLayout(new FlowLayout()); line=new Line(); circle=new Circle(); line.setVisible(true); line.setBounds(1,1,250,250); circle.setVisible(true); circle.setBounds(270,270,250,250); c.add("North",line);

c.add("Center",circle); addWindowListener(new WH()); setTitle("Line vs. Circle"); repaint();} public void paint(Graphics g){} public class WH extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);} } public static void main(String args[]){ Shapes ss=new Shapes(); ss.setVisible(true); ss.setBounds(0,0,700,700);} }

Output:

Lab Cycle:7 User Defined Exceptions


import java.io.*; class triangle extends Exception { double area; float s; int a,b,c; public String toString() { String s1=" i am sorry a+b<c wt shuld i do?"; return s1; triangle(){ } }

triangle(int x,int y,int z) { try a=x; b=y; c=z; if(a+b<c){ throw new triangle();} else { float s=(a+b+c)/2; area=s*(s-a)*(s-b)*(s-c); s=(float)Math.sqrt(area); System.out.println(s);} }catch(triangle e){ System.out.println("caught"+e);} } } class myexception { public static void main(String[] args) { {

try{ DataInputStream ds=new DataInputStream(System.in); System.out.println("enter the a value"); int a=Integer.parseInt(ds.readLine()); System.out.println("enter the b value"); int b=Integer.parseInt(ds.readLine()); System.out.println("enter the c value"); int c=Integer.parseInt(ds.readLine()); triangle t=new triangle(a,b,c); } catch(Exception e){ System.out.println(e); } } }

Output:
enter the a value 9 enter the b value 8 enter the c value 7 26.832815

Lab Cycle:8 Rational Manipulation-Using packages


package mypackage; import java.io.*; public class rational { private int nr,dr; public rational() { nr=0;dr=0; } public rational(int a,int b){ nr=a;dr=b; int max=a; if(a<b) max=b; for(int i=2;i<max;i++) if(a%i==0 && b%i==0 nr=a/i;dr=b/i; } }} public void reducedForm(int a,int b){ nr=a;dr=b; int max=a; if(a<b) max=b; for(int i=2;i<max;i++) if(a%i==0 && b%i==0) nr=a/i;dr=b/i; } }} { { { {

public void add(rational a,rational b) {

if(a.dr==b.dr) { nr=a.nr+b.nr; dr=a.dr; } else if(a.dr!=b.dr && b.dr%a.dr!=0) { dr=a.dr*b.dr; nr=a.nr*b.dr+b.nr*a.dr; } reducedForm(nr,dr);} public void sub(rational a,rational b){ if(a.dr==b.dr){ nr=a.nr-b.nr; dr=a.dr; } else if(a.dr!=b.dr && b.dr%a.dr!=0) { dr=a.dr*b.dr; nr=a.nr*b.dr-b.nr*a.dr; } if(nr==0) dr=0; reducedForm(nr,dr);} public void mul(rational a,rational b){ nr=a.nr*b.nr; dr=a.dr*b.dr; reducedForm(nr,dr);} public void div(rational a,rational b){ rational t=new rational(); t.nr=b.dr; t.dr=b.nr; mul(a,t);}

public void display() { System.out.println(nr+ "/"+dr);} }

import mypackage.rational; import java.io.*; public class package2 { public static void main(String arg[]) throws IOException { int a,b,c,d; DataInputStream in=new DataInputStream(System.in); System.out.println("enter the first fractional number :"); a=Integer.parseInt(in.readLine()); b=Integer.parseInt(in.readLine()); System.out.println("enter the first fractional number :"); c=Integer.parseInt(in.readLine()); d=Integer.parseInt(in.readLine()); rational r1=new rational(4,6); rational r2=new rational(3,4); rational r3=new rational(); r3.add(r1,r2); r1.display(); r2.display(); System.out.println("addition :"); r3.display(); r3.sub(r1,r2); System.out.println("subtraction :");

r3.display(); System.out.println("multiplication :"); r3.mul(r1,r2); r3.display(); System.out.println("division :"); r3.div(r1,r2); r3.display();}}

Output:
enter the first fractional number : 2 4 enter the first fractional number : 5 6 2/3 3/4 addition : 17/12 subtraction : -1/12 multiplication : 1/2 division : 8/9

Lab Cycle:9 Implementing Interfaces


import java.io.*; interface shape{ void area()throws Exception; void getname(); void getvol()throws Exception; } abstract class point implements shape{ float pie; point() { pie=3.14f; }} {

class circle extends point public void getname() {

System.out.println("This is circle Shape"); } public void area()throws Exception int r; DataInputStream ds=new DataInputStream(System.in); System.out.println("enter the radius of the circle"); r=Integer.parseInt(ds.readLine()); float area=pie*r*r; System.out.println("Area Of The Circle Is public void getvol()throws Exception class cylinder extends circle public void getname() { System.out.println("This is Cylinder Shape"); } public void getvol()throws Exception int r; { { {} } "+area); } {

DataInputStream ds=new DataInputStream(System.in); System.out.println("enter the radius of the cylinder"); r=Integer.parseInt(ds.readLine()); float vol=(4/3)*(pie*r*r*r); System.out.println("The Volume of the Cylinder Shape is "+vol); class driver { { } }

public static void main(String[] args) try{ int i=0;

shape s[]=new shape[2]; s[0]=new circle(); s[1]=new cylinder(); for( i=0;i<1;i++) s[i].getname(); for(i=1;i<2;i++) { s[i].getname(); s[i].getvol(); }catch(Exception e1){}}} } { s[i].area(); }

Output:
This is circle Shape enter the radius of the circle 2 Area Of The Circle Is This is Cylinder Shape enter the radius of the cylinder 3 The Volume of the Cylinder Shape is 84.78 12.56

Lab Cycle:10 Developing Employee Applet


/*<html> <title> Applet</title> <body bgcolor="orange"> <applet code="Lab10" width=300 height=300> <param name=name value=Rao> <param name=eid value=100> <param name=des value=HR> <param name="basicsal" value="50000.0"> </applet> </body> </html>*/ import java.awt.*; import java.io.*; import java.applet.*; public class Lab10 extends Applet { Double basic; Double da,hra,pf,lic,esi,netpay; String name,eid,desti; public void init() {

basic=Double.parseDouble(getParameter("basicsal")); name=getParameter("name"); eid=getParameter("eid"); desti=getParameter("desti");

da=(0.21)*basic; hra=(0.15)*basic; pf=(0.17)*basic; lic=(0.23)*basic; esi=(0.10)*basic; netpay=basic+da+hra-pf-lic-esi; setBackground(Color.BLUE); public void paint(Graphics g) { }

g.drawString("Ename is :"+name,40,50); g.drawString("Eid is :"+eid,55,65); g.drawString("Desgination is :"+desti,70,80); g.drawString("NET SALARY IS :"+netpay,115,125); }}

Lab Cycle:11Developing Calculator Using AWT Tools


import java.io.*; import java.awt.*; import java.awt.event.*; import static java.lang.Math.sin; import static java.lang.Math.cos; import static java.lang.Math.tan; class LabAwt extends Frame implements ActionListener,MouseListener{ int mouseX,mouseY,movX,movY; Panel keypad=new Panel(); TextField text=new TextField(50); Button one=new Button("1"); Button two=new Button("2"); Button three=new Button("3"); Button four=new Button("4"); Button five=new Button("5"); Button six=new Button("6"); Button seven=new Button("7"); Button eight=new Button("8"); Button nine=new Button("9"); Button zero=new Button("0"); Button add=new Button("+"); Button sub=new Button("-"); Button mul=new Button("X"); Button div=new Button("/");

Button dec=new Button("."); Button equal=new Button("="); Button sin=new Button("sin"); Button cos=new Button("cos"); Button tan=new Button("tan"); Button reset=new Button("RESET"); float temp; double temp1; String prevOptr; public LabAwt() { text.setEditable(false); text.setBounds(10,10,380,40); keypad.setLayout(new GridLayout(4,5)); keypad.add(one); keypad.add(two); keypad.add(three); keypad.add(four); keypad.add(five); keypad.add(six); keypad.add(seven); keypad.add(eight); keypad.add(nine); keypad.add(zero); keypad.add(add); keypad.add(sub);

keypad.add(mul); keypad.add(div); keypad.add(sin); keypad.add(cos); keypad.add(tan); keypad.add(dec); keypad.add(equal); keypad.add(reset); add(text,BorderLayout.NORTH); add(keypad,BorderLayout.CENTER); one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this); six.addActionListener(this); seven.addActionListener(this); eight.addActionListener(this); nine.addActionListener(this); zero.addActionListener(this); add.addActionListener(this); sub.addActionListener(this); mul.addActionListener(this); div.addActionListener(this); sin.addActionListener(this);

cos.addActionListener(this); tan.addActionListener(this); dec.addActionListener(this); equal.addActionListener(this); reset.addActionListener(this); addMouseListener(this); addWindowListener(new MyWindowAdapter()); setFont(new Font("SansSerif",Font.BOLD,12));} public void actionPerformed(ActionEvent ae){ if(ae.getSource()==one||ae.getSource()==two||ae.getSource()==three||ae.getSource()==four||ae.getS ource()==five||ae.getSource()==six||ae.getSource()==seven||ae.getSource()==eight||ae.getSource()== nine||ae.getSource()==zero||ae.getSource()==dec){ try{ text.setText((text.getText()).concat(ae.getActionCommand()));} catch(Exception e){} } if(ae.getSource()==add||ae.getSource()==sub||ae.getSource()==mul||ae.getSource()==div){ try{ temp=Float.valueOf(text.getText()).floatValue(); prevOptr=ae.getActionCommand(); clear();} catch(Exception e){}} if(ae.getSource()==sin||ae.getSource()==cos||ae.getSource()==tan){ try{ temp1=Math.toRadians(Double.valueOf(text.getText()).doubleValue()); text.setText(""); if(ae.getSource()==sin)

text.setText(Double.toString(sin(temp1))); else if(ae.getSource()==cos) text.setText(Double.toString(cos(temp1))); else if(ae.getSource()==tan) text.setText(Double.toString(tan(temp1)));} catch(Exception e){} } if(ae.getSource()==reset){ try{ text.setText("");} catch(Exception e){} } if(ae.getSource()==equal) { try{ if(prevOptr.equals("+")) text.setText(Float.toString(temp+(Float.valueOf(text.getText()).floatValue()))); if(prevOptr.equals("-")) text.setText(Float.toString(temp-(Float.valueOf(text.getText()).floatValue()))); if(prevOptr.equals("X")) text.setText(Float.toString(temp*(Float.valueOf(text.getText()).floatValue()))); if(prevOptr.equals("/")) text.setText(Float.toString(temp/(Float.valueOf(text.getText()).floatValue()))); } catch(Exception e){} } } public void clear(){ text.setText("");} public void mouseExited(MouseEvent evtObj){ mouseX=10;

mouseY=54; repaint(); } public void mouseEntered(MouseEvent evtObj){ mouseX=10; mouseY=54; repaint();} public void mouseReleased(MouseEvent me){ mouseX=me.getX(); mouseY=me.getY(); repaint();} public void mousePressed(MouseEvent me){ mouseX=me.getX(); mouseY=me.getY(); repaint();} public void mouseClicked(MouseEvent me){ } public void mouseMoved(MouseEvent me){ movX=me.getX(); movY=me.getY(); repaint(0,0,100,60);} public void mouseDragged(MouseEvent me){ mouseX=me.getX(); mouseY=me.getY(); movX=me.getX(); movY=me.getY(); repaint();}

public void paint(Graphics g){} public static void main(String args[]){ LabAwt la=new LabAwt(); la.setSize(new Dimension(250,250)); la.setVisible(true);} } class MyWindowAdapter extends WindowAdapter{ public void windowClosing(WindowEvent we){ System.exit(0);} }

Lab Cycle :12 Employee DataBase Using JDBC


import java.sql.*; import java.io.*; class Lab12 { public static void main (String args []) int c=0; try { DataInputStream br=new DataInputStream(System.in); Connection con=DriverManager.getConnection("jdbc:odbc:access1"); PreparedStatement pstmt=null; Statement stmt = con.createStatement (); do{ System.out.println("1.Create Table(only one time)"); System.out.println("2.Insert Data"); System.out.println("3.Delete Records"); System.out.println("4.Update Records"); System.out.println("5.Condition Based"); System.out.println("6.Exit"); System.out.println("Enter the Choice"); int ch=Integer.parseInt(br.readLine()); switch(ch) case 1: pstmt=con.prepareStatement("create table Employee1(eno number,ename text,eaddress text,esal number)"); pstmt.executeUpdate(); { throws SQLException {

break; case 2: pstmt=con.prepareStatement("insert into Employee1 values(?,?,?,?)"); System.out.println("Enter the Employee number"); pstmt.setInt(1,Integer.parseInt(br.readLine())); System.out.println("Enter the Employee Name"); pstmt.setString(2,br.readLine()); System.out.println("Enter the Employee Address"); pstmt.setString(3,br.readLine()); System.out.println("Enter The Employee Salary"); pstmt.setInt(4,Integer.parseInt(br.readLine())); pstmt.executeUpdate(); break; case 3: pstmt=con.prepareStatement("delete *from Employee1 where ename=?"); System.out.println("Enter the Employee name U want to Delete"); pstmt.setString(1,br.readLine()); int count=pstmt.executeUpdate(); System.out.println("The " +count+ " Records Are Deleted"); break; case 4: pstmt=con.prepareStatement("update Employee1 set esal=45000 where eno=?"); System.out.println("Enter the Empolyee id u want to update his salary"); pstmt.setInt(1,Integer.parseInt(br.readLine())); int count1=pstmt.executeUpdate();

System.out.println("The " +count1+ " Records Are Updated"); break; case 5: ResultSet rset = stmt.executeQuery("select * from Employee1 where esal>2000"); System.out.println("Empno"+"\t"+"EmpName"+"\t"+"Empaddress"+"\t"+"Empsalary") System.out.println("----------------------------------------"); while (rset.next()){ System.out.println (rset.getInt(1)+"\t"+rset.getString (2)+"\t"+rset.getString(3)+"\t"+rset.getInt(4)); } break; case 6: System.exit(0); break; } System.out.println("Do u Want Continue ...press 1"); c=Integer.parseInt(br.readLine()); }while(c==1); }catch(Exception e) { System.out.println(e); e.printStackTrace(); } } }

Output:
1.Create Table(only one time) 2.Insert Data 3.Delete Records 4.Update Records 5.Condition Based 6.Exit Enter the Choice 2 Enter the Employee number 100 Enter the Employee Name Sankar Rao Enter the Employee Address Hyderabad Enter The Employee Salary 50000 Do u Want Continue ...press 1 2

Lab Cycle:13 Working Files and Streams


a) import java.io.*; import java.util.*; class filecopy { public static void main(String[] args) String data=" "; try{ FileReader fr=new FileReader("sample.txt"); int c=fr.read(); while(c!=-1) char ch=(char)c; data=data+ch; c=fr.read();} fr.close(); }catch(Exception e){ System.out.println(e); } try{ FileWriter fw=new FileWriter("Sankar Rao.txt"); char ch1[]=data.toCharArray(); fw.write(ch1); fw.close(); }catch(Exception e1){ System.out.println(e1); }} { {

B). import java.io.*; class filename implements FilenameFilter { String ext=null; filename(String pext) { this.ext=pext; } {

public boolean accept(File dir,String pext) return pext.endsWith(ext); class driver1 { public static void main(String[] args) try{ { }}

File f1=new File("E:/Sankar Rao-java/class-work"); if(f1.isDirectory()) {

FilenameFilter fo=new filename(".java"); String []listed=f1.list(fo); for(int i=0;listed[i]!=null;i++){ System.out.println(listed[i]); } }catch(Exception e) } } }}

{ System.out.println(e);

Output:
E:\Sankar Rao-java\class-work>java driver1 A.java Ab.java Abc.java applet2.java c.java cattest.java

C). import java.io.*; class Lab13c { public static void main(String[] args) {

File f=new File("e:/Sankar Rao-java/demo.java");

System.out.println("exits "+f.exists()); System.out.println("Reader"+f.canRead()); System.out.println("Writer"+f.canWrite()); System.out.println(" path"+f.getPath()); System.out.println("absoulte path"+f.getAbsolutePath()); System.out.println("filesize"+f.length()); System.out.println("parent"+f.getParent()); System.out.println("isdir"+f.isDirectory()); }}

Output:
exits true Readertrue Writertrue pathe:\Sankar Rao-java\demo.java absoulte pathe:\Sankar Rao-java\demo.java filesize4900 parente:\Sankar Rao-java isdirfalse

Lab Cycle:14 Implementing Serialization


import java.io.*; class student implements Serializable { String sname="Sankar Rao "; int sage=24; String sregno="07MCA135"; } class Lab14 { public static void main(String[] args) try { {

FileOutputStream fos=new FileOutputStream("nitha1.txt",true); ObjectOutputStream oos=new ObjectOutputStream(fos); student a=new student(); oos.writeObject(a); fos.close(); }catch(Exception e){ System.out.println(e); } try { FileInputStream fis=new FileInputStream("nitha1.txt"); ObjectInputStream ois=new ObjectInputStream(fis); Object o=ois.readObject(); student a1=null; a1=(student)o; System.out.println("Student Name : " +a1.sname); System.out.println("Student Age : "+a1.sage); System.out.println("Student Reg.No: "+a1.sregno);

fis.close(); }catch(Exception e){ System.out.println(e); }

} }

OutPut:
C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>java Lab14 Student Name : Sankar Rao Student Age : 22 Student Reg.No: 07MCA135

Lab Cycle:15 Chatting Using Networking


import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class Chat extends JFrame{ JLabel lblServerIP = new JLabel("Server IP:"); JTextField serverIP = new JTextField(15); JLabel lblPort = new JLabel("Port Num:"); JTextField port = new JTextField(15); JLabel lblName = new JLabel("NickName:"); JTextField name = new JTextField(15); JRadioButton serverBTN = new JRadioButton("Server (Host)"); JRadioButton clientBTN = new JRadioButton("Client (Guest)"); JButton connect = new JButton("Connect"); JButton disconnect = new JButton("Disconnect"); JTextArea messages = new JTextArea("",10,21); JTextField mssg = new JTextField(15); JButton send = new JButton("Send"); JLabel lbl = new JLabel("MCA Chat"); Font btn = new Font("times new roman",Font.BOLD,20); Font lblFont = new Font("times new roman",Font.BOLD+Font.HANGING_BASELINE,45); Socket socket = null;

Socket socket2 = null; ServerSocket serverSocket = null; PrintWriter out = null; BufferedReader in = null; PrintWriter out2 = null; BufferedReader in2 = null; boolean server = true; Color purple = new Color(108,111,206); Color yellow = new Color(173,115,5); Color blue = new Color(15,11,102); public Chat() { super("Chat@MCA"); setSize(505,250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); Container contentArea = getContentPane(); GridLayout flowManager = new GridLayout(1,2); contentArea.setLayout(flowManager); Cursor cursor = new Cursor(Cursor.HAND_CURSOR); setCursor(cursor); JPanel pnl = new JPanel(); pnl.add(lbl); lbl.setFont(lblFont); lbl.setForeground(Color.darkGray); pnl.add(lblServerIP);

lblServerIP.setForeground(purple); pnl.add(serverIP); pnl.add(lblPort); lblPort.setForeground(purple); pnl.add(port); pnl.add(lblName); lblName.setForeground(purple); pnl.add(name); ButtonGroup type = new ButtonGroup(); type.add(serverBTN); serverBTN.setForeground(yellow); serverBTN.addActionListener( new ActionListener() { {

public void actionPerformed(ActionEvent event) server = true; type.add(clientBTN); clientBTN.setForeground(yellow); clientBTN.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { server = false; pnl.add(serverBTN); pnl.add(clientBTN); pnl.add(connect); connect.addActionListener( } } ); } } );

new ActionListener()

{ {

public void actionPerformed(ActionEvent event) connect(); } } );

connect.setFont(btn); connect.setForeground(blue); pnl.add(disconnect); disconnect.setEnabled(false); disconnect.addActionListener( new ActionListener() { {

public void actionPerformed(ActionEvent event) disconnect(); } } );

disconnect.setFont(btn); disconnect.setForeground(blue); contentArea.add(pnl); JPanel pnl2 = new JPanel(); pnl2.add(new JScrollPane(messages, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)); messages.setEditable(false); messages.setCursor(new Cursor(Cursor.TEXT_CURSOR)); pnl2.add(mssg); pnl2.add(send); send.setEnabled(false); send.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent event) send(); } } );

contentArea.add(pnl2); contentArea.setBackground(Color.white); setContentPane(contentArea); } public void connect() { serverBTN.setEnabled(false); clientBTN.setEnabled(false); disconnect.setEnabled(true); connect.setEnabled(false); send.setEnabled(true); try { if(!server) try { {

socket = new Socket(serverIP.getText(), Integer.parseInt(port.getText())); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); messages.append(in.readLine()); catch (Exception hostEx) { }

JOptionPane.showMessageDialog(null,"Error with port number or IP "+ "Address.","Error", JOptionPane.ERROR_MESSAGE); else try { { } } }

serverSocket = new ServerSocket(Integer.parseInt(port.getText()));

catch (Exception portInUseEx)

JOptionPane.showMessageDialog(null,"Port in use. Select a different "+ "port.","Port in Use", JOptionPane.ERROR_MESSAGE); PrintWriter out2 = new PrintWriter( socket2.getOutputStream(), true); BufferedReader in2 = new BufferedReader( new InputStreamReader( socket2.getInputStream())); messages.append(in2.readLine()); catch (Exception exception) { } } public void disconnect() { serverBTN.setEnabled(true); clientBTN.setEnabled(true); disconnect.setEnabled(false); connect.setEnabled(true); send.setEnabled(false); try { if(!server) out.close(); in.close(); socket.close(); else { } { } } }

out2.close(); in2.close(); socket2.close();

serverSocket.close();

} }

catch(Exception closeEx) { } } public void send() { try { if(!server) {

messages.append(name.getText() + ": " + mssg.getText() + "\n"); out.println(name.getText() + ": " + mssg.getText()); mssg.setText(" else { "); }

messages.append(name.getText() + ": " + mssg.getText() + "\n"); out2.println(name.getText() + ": " + mssg.getText());mssg.setText(" } catch(Exception e3) { } } public static void main (String [] args) { new Chat(); }} "); }

Output:

Lab Cylce:16 Chatting Using RMI


//ChatClient import java.rmi.*; public interface ChatClient extends Remote{ void receive (String s) throws RemoteException; } //ChatSever import java.rmi.*; public interface ChatServer extends Remote { void register(ChatClient c) throws RemoteException; void broadcast(String s) throws RemoteException; } //ChatClientImpl import java.util.*; import java.io.*; import java.util.Scanner; import java.net.*; import java.rmi.*; import java.rmi.server.*; public class ChatClientImpl extends UnicastRemoteObject implements ChatClent, Runnable { private ChatServer mycs; public ChatClientImpl(ChatServer cs) throws RemoteException { mycs=cs; mycs.register(this);} public synchronized void receive (String s) throws RemoteException{ System.out.println("Message: "+s);} public void Run (){

Scanner in=new Scanner(System.in); String msg; while(true){ try{ msg=in.readLine(); mycs.broadcast(msg);} catch(Exception e){ System.err.println("Problem.");} } } public static void main (String[] args){ String url = "rmi://localhost/ChatServer"; try{ ChatServer cs= (ChatServer) Naming.lookup(url); new Thread(new ChatClientImpl(cs)).start();} catch (Exception e){ System.err.println("Problem.") ;} } } //ChatServerImpl import java.rmi.*; import java.rmi.server.*; import java.net.*; import java.util.*; public class ChatServerImpl extends UnicastRemoteObject implements ChatServer { private LinkedList myclients;

public ChatServerImpl() throws RemoteException { myclients = new LinkedList(); } public synchronized void register (ChatClient c) throws RemoteException{ myclients.add(c); } public synchronized void broadcast(String s) throws RemoteException { for(int i=0;i< myclients.size();i++){ myclients.get(i).receive(s); } public static void main (String[] args){ try{ Naming.rebind("ChatServer", new ChatServerImpl());} catch(Exception e){ System.err.println("Problem.") ;} } }

Output:
C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>javac ChatClient.java C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>javac ChatServer.java C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>javac ChatClientImpl.java C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>javac ChatServerImpl.java C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>start rmiregistry C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>java ChatServerImpl C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>java ChatClientImpl Client:hi Server:hi

Vous aimerez peut-être aussi