Académique Documents
Professionnel Documents
Culture Documents
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; } }}
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(); } } { { {
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) { {
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)
}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
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; } { { } }
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; }
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");
OUTPUT:
Enter the four digit number|||| 4567 Encrypt the number 3412 decrype number 4567
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
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");
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
lcount=search.length();
Output:
to be or to be
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:
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
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);}
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 :");
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
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 { { } }
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
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) { }
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);} }
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
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{ { }}
{ 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) {
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
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);
} }
OutPut:
C:\Documents and Settings\Sankar Rao\Desktop\Lab-progrms>java Lab14 Student Name : Sankar Rao Student Age : 22 Student Reg.No: 07MCA135
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()
{ {
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() {
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,"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 { } { } } }
serverSocket.close();
} }
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:
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