You are on page 1of 16

IMPLEMENTATION OF OBJECT SERILIZATION

PROGRAM: import java.util.*; import java.io.ObjectOutput; import java.io.FileOutputStream; import java.io.ObjectOutputStream; class Rupee { public Rupee() { try { Object object=new Object(); object="45"; ObjectOutput out=new ObjectOutputStream(new FileOutputStream("Rupees.dat")); out.writeObject(object); out.close(); } catch(Exception e) { e.printStackTrace(); } } } class Dollar { public Dollar() { try { Object object=new Object(); object="45"; ObjectOutput out=new ObjectOutputStream(new FileOutputStream("Dollar.dat")); out.writeObject(object); out.close(); } catch(Exception e) { e.printStackTrace(); } } } public class Currency { public static void main(String args[])

{ new Rupee(); new Dollar(); } } //CURRENCY TEST import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.*; public class CurrencyTest { public static void main(String[] args) { System.out.println("Select Input Type"); System.out.println("\n1.Dollar\n2.Rupee\n\n"); Scanner input=new Scanner(System.in); int choice=input.nextInt(); if(choice==1) { System.out.println("Enter No of Dollar:"); int noDollar=input.nextInt(); String value=""; String filename="Dollar.dat"; FileInputStream fis=null; ObjectInputStream in=null; try { fis=new FileInputStream(filename); in=new ObjectInputStream(fis); value=(String)in.readObject(); in.close(); } catch(IOException ex) { ex.printStackTrace(); } catch(ClassNotFoundException ex) { ex.printStackTrace(); } System.out.println("The Equal Rupee is:"+noDollar*(Integer.parseInt(value))); System.out.println(); }

else if(choice==2) { System.out.println("Enter Rupee:"); int noRupee=input.nextInt(); System.out.println("The Rupee is:"+noRupee); System.out.println(); } } }

Output:

IMPLEMENTATION OF SCENTIFIC CALCULATOR USING EVENT DRIVEN PROGRAMMING


PROGRAM: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; public class Calculator { public static void main(String[] args) { CalculatorFrame frame = new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Calculator"); CalculatorPanel panel = new CalculatorPanel(); add(panel); pack(); } } class CalculatorPanel extends JPanel { public CalculatorPanel() { setLayout(new BorderLayout()); result = 0; lastCommand = "="; start = true; display = new JButton("0"); display.setEnabled(false); add(display, BorderLayout.NORTH); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); panel = new JPanel(); panel.setLayout(new GridLayout(6,5)); addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command);

addButton("CE", command); addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("m+", command); addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("m-", command); addButton("0", insert); addButton(".", insert); addButton("+/-", command); addButton("+", command); addButton("n!", command); addButton("pow", command); addButton("1/x", insert); addButton("SQRT", insert); addButton("log", insert); addButton("%", command); addButton("sin", insert); addButton("cos", insert); addButton("tan", insert); addButton("x2", insert); addButton("=", command); add(panel, BorderLayout.CENTER); } private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.addActionListener(listener); panel.add(button); } private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start==true) { display.setText(""); start = false; } if(input.equals("1/x")) display.setText(""+1/Double.parseDouble(display.getText()));

else if(input.equals("SQRT")) display.setText(""+Math.sqrt(Double.parseDouble(display.getText()))); else if(input.equals("log")) display.setText(""+Math.log(Double.parseDouble(display.getText()))); else if(input.equals("x2")) display.setText(""+Double.parseDouble(display.getText())* Double.parseDouble(display.getText())); else if(input.equals("sin")) { Double angle=Double.parseDouble(display.getText())*2.0*Math.PI/360.0; display.setText(""+Math.sin(angle)); } else if(input.equals("cos")) { Double angle=Double.parseDouble(display.getText())*2.0*Math.PI/360.0; display.setText(""+Math.cos(angle)); } else if(input.equals("tan")) { Double angle=Double.parseDouble(display.getText())*2.0*Math.PI/360.0; display.setText(""+Math.tan(angle)); } else display.setText(display.getText() + input); } } private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (start==true) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; }

else { calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } public void calculate(double x) { if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("=")) result = x; else if (lastCommand.equals("CE")) result = 0.0; else if (lastCommand.equals("m+")) result = result; else if (lastCommand.equals("m-")) result = 0.0; else if (lastCommand.equals("pow")) { double powval=1.0; for(double i=0.0;i<x;i++) powval*=result; result=powval; } display.setText(""+ result); } private JButton display; private JPanel panel; private double result; private String lastCommand; private boolean start; }

Output:

IMPLEMENTATION OF MULTI THREADED PROGRAM


PROGRAM: import java.util.*; import java.io.*; class Fibonacci extends Thread { private PipedWriter out=new PipedWriter(); public PipedWriter getPipedWriter() { return out; } public void run() { Thread t=Thread.currentThread(); t.setName("Fibonacci:"); System.out.println(t.getName()+"Thread stored......"); int fibo1=0,fibo2=1,fibo=0; while(true) { try { fibo=fibo1+fibo2; if(fibo>100000) { out.close(); break; } out.write(fibo); sleep(1000); } catch(Exception e) { System.out.println("Fibonacci:"+e); } fibo1=fibo2; fibo2=fibo; } System.out.println(t.getName()+"Thread exiting."); } } class Prime extends Thread { private PipedWriter out1=new PipedWriter(); public PipedWriter getPipedWriter() {

return out1; } public void run() { Thread t=Thread.currentThread(); t.setName("Prime:"); System.out.println(t.getName()+"Thread stored......."); int prime=1; while(true) { try { if(prime>100000) { out1.close(); break; } if(isPrime(prime)) out1.write(prime); prime++; sleep(0); } catch(Exception e) { System.out.println(t.getName()+"Thread exiting."); System.exit(0); } } } public boolean isPrime(int n) { int m=(int)Math.round(Math.sqrt(n)); if(n==1||n==2) return true; for(int i=2;i<=m;i++) if(n%i==0) return false; return true; } } public class PipedIO { public static void main(String[] args)throws Exception { Thread t=Thread.currentThread(); t.setName("Main:");

System.out.println(t.getName()+"Thread sorted......"); Fibonacci fibonacci=new Fibonacci(); Prime prime=new Prime(); PipedReader fpr=new PipedReader(fibonacci.getPipedWriter()); PipedReader ppr=new PipedReader(prime.getPipedWriter()); fibonacci.start(); prime.start(); int fib=fpr.read(),prm=ppr.read(); System.out.println("The Numbers Common To PRIME and FIBONACCI:"); while ((fib!=-1)&&(prm!=-1)) { while(prm<=fib) { if(fib==prm) System.out.println(prm); prm=ppr.read(); } fib=fpr.read(); } System.out.println(t.getName()+"Thread exiting."); } }

Output:

MULTI-THREADED ECHO SERVER AND A CORRESPONDING GUI CLIENT


PROGRAM 1: import java.io.*; import java.net.*; public class EchoServer { public EchoServer(int portnum) { try { server = new ServerSocket(portnum); } catch (Exception err) { System.out.println(err); } } public void serve() { try { while (true) { Socket client = server.accept(); BufferedReader r = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter w = new PrintWriter(client.getOutputStream(), true); w.println("Welcome to the Java EchoServer. Type 'bye' to close."); String line; do { line = r.readLine(); if ( line != null ) w.println("Got: "+ line); } while ( !line.trim().equals("bye") ); client.close(); } } catch (Exception err) { System.err.println(err); } } public static void main(String[] args)

{ EchoServer s = new EchoServer(9999); s.serve(); } private ServerSocket server; } PROGRAM 2: import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 9999); BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter w = new PrintWriter(s.getOutputStream(), true); BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); String line; do { line = r.readLine(); if ( line != null ) System.out.println(line); line = con.readLine(); w.println(line); } while ( !line.trim().equals("bye") ); } catch (Exception err) { System.err.println(err); } } }

Output:
Server:

Client: