Top Banner

of 30

CS2309 Java Lab Manual CSE

Oct 29, 2015

Download

Documents

Anna University Java Lab Manual Reg 2008
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • EX. NO: Rational number class in Java

    DATE :

    AIM:

    ALGORITHM:

  • PROGRAM: /** Rational number class */ import java.io.*; import java.util.*; public class Rational { int n,d; Rational(int x,int y) { n=x; d=y; System.out.println("\n Numerator Value:"+n+"\n Denominator Value:"+d); System.out.println("\n Before Simplification:"+n+"/"+d); } public void reducedform() { if(n
  • public static void main(String a[]) { Scanner s=new Scanner(System.in); System.out.println("Enter the Numerator Value:"); int c=s.nextInt(); System.out.println("\n Enter the Denominator Value:"); int b=s.nextInt(); Rational r=new Rational(c,b); r.reducedform(); r.display(); } }

    OUTPUT: H:\CS2309>java Rational Enter the Numerator Value: 500 Enter the Denominator Value: 1000 Numerator Value: 500 Denominator Value: 1000 Before Simplification: 500/1000 Reduced Form= 1/2 H:\CS2309>java Rational Enter the Numerator Value: 100 Enter the Denominator Value: 0 Numerator Value: 100 Denominator Value: 0 Before Simplification: 100/0 In determinant Form

    RESULT:

  • EX. NO : Date class in Java

    DATE :

    AIM:

    ALGORITHM:

  • PROGRAM: /** Date class in java */ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class BasicDate { public static void main (String a[ ]) throws Exception { Date today=Calendar.getInstance( ).getTime( ); DateFormat shortf = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT); DateFormat longf =SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG); DateFormat medf = SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM); System.out.println(shortf.format(today)); System.out.println(longf.format(today)); System.out.println(mediumf.format(today)); String DateAsText=shortf.format(today); Date TextAsDate=shortf.parse(DateAsText); System.out.println(today); } }

    OUTPUT:

    H:\CS2309>java BasicDate 3/9/12 3 September, 2012 3 Sep, 2012 Mon Sep 03 21:48:24 IST 2012 RESULT:

  • EX. NO : Lisp-like list in Java

    DATE :

    AIM:

    ALGORITHM:

  • PROGRAM:

    /** Lisp-like List */ import java.util.*; public class Lisp { public int car(List l) { Object ob=l.get(0); String st=ob.toString(); System.out.println("Using a L.car( ) function\n"); return Integer.parseInt(st); } public List cons(List l) { Object ob=l.add(7); Object obj[ ]=l.toArray(); System.out.println("Using a L.cons( ) function\n"); List list=Arrays.asList(obj); return list; } public List cdr(List l) { Object ob=l.remove(0); Object obj[ ]=l.toArray( ); System.out.println("Using a L.cdr( ) function \n"); List list=Arrays.asList(obj); return list; } public static void main(String a[]) { Listl=new ArrayList( ); l.add(3); l.add(0); l.add(2); l.add(5); Lisp b=new Lisp(); int val=b.car(l); System.out.println(""+val+"\n"); List list1=b.cons(l); System.out.println(""+list1+"\n"); List list=b.cdr(l); System.out.println(list); } }

  • OUTPUT: H:\CS2309>java Lisp Using a L.car( ) function 3 Using a L.cons( ) function [3, 0, 2, 5, 7] Using a L.cdr( ) function [0, 2, 5, 7]

    RESULT:

  • EX. NO : Design a Java interface for ADT Stack

    DATE :

    AIM:

    ALGORITHM:

  • PROGRAM: import java.util.*; import java.lang.*; interface StackDemo { public void push(int v); public void pop(); } class Link implements StackDemo { LinkedList list=new LinkedList(); public void push(int v) { list.addFirst(v); System.out.println(list); } public void pop() { list.removeFirst(); System.out.println(list); } } class Link1 implements StackDemo { static int n=0; ArrayList list=new ArrayList(); public void push(int v) { list.add(v); n++; System.out.println(list); } public void pop() { list.remove(--n); System.out.println(list); } } class StackAll { public static void main(String args[]) { int ch,lk,ak; System.out.println("Stack Implementation Using Array and Linked List"); System.out.println("Enter your choice"); System.out.println("1.Stack Implementation Using Linked List"); System.out .println("2.Stack Implementation Using Array"); Scanner s=new Scanner(System.in); ch=s.nextInt();

  • switch(ch) { case 1: System.out.println("1.Stack Implementation Using Linked List"); Link l =new Link(); Scanner t=new Scanner(System.in); do { System.out.println("Enter your choice 1.Push 2.Pop"); lk=t.nextInt(); switch(lk) { case 1: System.out.println("Enter elements"); int n=t.nextInt(); l.push(n); break; case 2: try { l.pop(); System.out.println(l); } catch(NoSuchElementException k) { System.out.println("Empty Stack:"+k); } break; default: System.out.println("Invalid choice"); break; } } while(lk
  • default:System.out.println("Invalid choice"); break; } } while(akjava StackAll Stack Implementation Using Array and Linked List Enter your choice 1.Stack Implementation Using Linked List 2.Stack Implementation Using Array 1 1.Stack Implementation Using Linked List Enter your choice 1.Push 2.Pop 1 Enter elements 12 [12] Enter your choice 1.Push 2.Pop 1 Enter elements 14 [14, 12] Enter your choice 1.Push 2.Pop 1 Enter elements 25 [25, 14, 12] Enter your choice 1.Push 2.Pop 2 [14, 12] Link@10b30a7 Enter your choice 1.Push 2.Pop 2 [12] Link@10b30a7 Enter your choice 1.Push 2.Pop 2 [] Link@10b30a7 Enter your choice 1.Push 2.Pop 2 Empty Stack:java.util.NoSuchElementException Enter your choice 1.Push 2.Pop 3 Invalid choice

  • 1.Stack Implementation Using Array Enter your choice 1.Push 2.Pop 1 Enter elements 51 [51] Enter your choice 1.Push 2.Pop 1 Enter elements 52 [51, 52] Enter your choice 1.Push 2.Pop 1 Enter elements 53 [51, 52, 53] Enter your choice 1.Push 2.Pop 2 [51, 52] Enter your choice 1.Push 2.Pop 2 [51] Enter your choice 1.Push 2.Pop 2 [] Enter your choice 1.Push 2.Pop 2 Empty stack java.lang.ArrayIndexOutOfBoundsException: -1 Enter your choice 1.Push 2.Pop 3 Invalid choice Invaild choice

    RESULT:

  • EX. NO: Vehicle class hierarchy design in Java

    DATE :

    AIM:

    ALGORITHM:

  • PROGRAM: import java.io.*; class Vehicle { String regno; int model; Vehicle(String r, int m) { regno=r; model=m; } void display() { System.out.println("Registration no: "+regno); System.out.println("Model no: "+model); } } class Twowheeler extends Vehicle { int noofwheel; Twowheeler(String r,int m,int n) { super(r,m); noofwheel=n; } void display() { System.out.println("Two wheeler Tvs"); super.display(); System.out.println("No. of Wheels: " +noofwheel); } } class Threewheeler extends Vehicle { int noofleaf; Threewheeler(String r,int m,int n) { super(r,m); noofleaf=n; } void display() { System.out.println("Three wheeler Auto"); super.display(); System.out.println("No. of Wheels:" +noofleaf); } } class Fourwheeler extends Vehicle { int noofleaf; Fourwheeler(String r,int m,int n) { super(r,m); noofleaf=n; }

  • void display() { System.out.println("Four wheeler Car"); super.display(); System.out.println("No. of Wheels:" +noofleaf); } } public class Vehicledemo { public static void main(String arg[]) { Twowheeler t1; Threewheeler th1; Fourwheeler f1; t1=new Twowheeler("TN74 12345", 1,2); th1=new Threewheeler("TN74 54321", 4,3); f1=new Fourwheeler("TN34 45677",5,4); t1.display(); th1.display(); f1.display(); } } OUTPUT: H:\CS2309>java Vehicledemo Two wheeler Tvs Registration no: TN74 12345 Model no: 1 No. of Wheels: 2 Three wheeler Auto Registration no: TN74 54321 Model no: 4 No. of Wheels:3 Four wheeler Car Registration no: TN34 45677 Model no: 5 No. of Wheels:4

    RESULT:

  • EX. NO: Random Generation of objects

    DATE:

    AIM:

    ALGORITHM:

  • PROGRAM: /* StoreCurrency.java */ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.util.Random; import java.io.Serializable; public class StoreCurrency { public static void main(String args[])throws FileNotFoundException,IOException { Currency currency=null; ObjectOutputStream out; out=new ObjectOutputStream(new FileOutputStream(new File("currency.txt"))); Random random=new Random(); for( int i=0;i
  • public Double getValue() { return this.money; } public String getPrintableValue() { String str="Objectname::Rupee\nINR::RS"+getValue()+"\n------\n"; return str; } } class Dollar extends Currency { public Dollar(Double amount) { super(amount); } public Double getValue() { return(this.money*D_R_E); } public String getPrintableValue() { String str="Objectname::Dollar\nUSD::$"+this.money+"\nINR::Rs"+getValue()+"\n---\n"; return str; } }

    /* ReadCurrency.java */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class ReadCurrency { public static void main(String args[])throws IOException, ClassNotFoundException { Currency currency=null; ObjectInputStream in; in =new ObjectInputStream(new FileInputStream(new File("currency.txt"))); while((currency=(Currency)in.readObject())!=null) { System.out.println(currency.getPrintableValue()); } in.close(); } }

  • OUTPUT: H:\CS2309>javac StoreCurrency.java H:\CS2309>java StoreCurrency H:\CS2309>javac ReadCurrency.java H:\CS2309>java ReadCurrency Objectname::Rupee INR::RS3.539596175905678 ------ Objectname::Rupee INR::RS7.23774299663749 ------ Objectname::Rupee INR::RS5.628136075750075 ------ Objectname::Dollar USD::$7.113509356001621 INR::Rs316.15992332749204 ------ Objectname::Dollar USD::$8.277365791228362 INR::Rs367.88752259114455 ------ Objectname::Dollar USD::$9.676135168723924 INR::Rs430.05582757393483 ------ Objectname::Rupee INR::RS7.52459928209551 ------ Objectname::Rupee INR::RS5.344043955456231 ------ Objectname::Rupee INR::RS6.022195153063526 ------ Objectname::Rupee INR::RS9.281613788590379 ------

    RESULT:

  • EX. NO: Scientific Calculator in Java DATE : AIM:

    ALGORITHM:

  • PROGRAM: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.lang.*; public class Scicalc { public static void main(String [] a) { CalculatorFrame frame=new CalculatorFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("CALCULATOR"); CalculatorPanel pan=new CalculatorPanel(); add(pan); 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("ME",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!",insert); 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 l) { JButton button=new JButton(label); button.addActionListener(l); 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")) { Double m=(1/Double.parseDouble(display.getText())); display.setText(" " +m); } 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 if(input.equals("n!")) { double nValue=1.0; for(double i=1.0;i

  • public void Calculator(double x) { if(lastCommand.equals("+")) result+=x; if(lastCommand.equals("-")) result-=x; if(lastCommand.equals("*")) result*=x; if(lastCommand.equals("/")) result/=x; if(lastCommand.equals("=")) result=x; if(lastCommand.equals("CE")) result=0.0; if(lastCommand.equals("M+")) result=x; if(lastCommand.equals("M-")) result=0.0; else if(lastCommand.equals("pow")) { double powwal=1.0; for(double i=0.0;i
  • OUTPUT: H:\CS2309> java Scicalc

    RESULT:

  • EX. NO: Multithreading in Java DATE : AIM:

    ALGORITHM:

  • PROGRAM:

    interface maxlimit { public static final int MAX_LIMIT=10000; } class prime extends Thread implements maxlimit { int num[ ]=new int[MAX_LIMIT]; prime(String n) { super(n); for(int i=0;i

  • public void run() { int f1=-1,f2=1,f3=0,k=0; while(f3
  • OUTPUT: H:\CS2309>java primefibo Prime numbers in fibonacci series 2 3 5 13 89 233 RESULT: