Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
INDEX S. No Title 1. WAP to find the average and sum of n numbers using command line arguments Demonstrate Explicit and implicit Type casting Find the number of arguments provided at run time Test the prime number Calculate Simple Interest on Principal and Rate of Interest Create a shape class to find out the area and perimeter of rectangle and box using Super and This keyword. Find the GCD and LCM of two number Design a class account using the inheritance and static that show all functions of Bank. Find the factorial of a giver number using recursion Design a class using abstract methods and classes Deisng a string class that perform the string methods Handle the exception using the try and multiple catch blocks Implement the Nested Try statements Create a package that access the member of external class as well as the same package. Import the user defined package and access the user member variable of classes contained by that package Handle the user defined exception using the throw keyword Create a thread that implements the Runnable interface Implement interthread communication Create a class component that shows control and event handling on those controls Draw the line rectangle, Text Box, Text using the Graphics method. Create a menu using the frame Implement the Flow Layout and Border Layout. Implement the Grid Layout and Card Layout Demonstrate the System Clock. Demonstrate the AWT controls. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. Page No. 1 2 3 5 6 7 9 11 13 16 19 20 23 24 26 27 28 30 31 34 37 38 39 42 46 Remarks /* wap for to find the average of command line arguments */ import java.lang.*; class a1{ public static void main(String a[]) {int l=a.length; if(l==0) {System.out.println("NO. cmmd line argument exist"); } else{ int sum=0; for(int i=0;i<l;i++) {sum=sum+Integer.parseInt(a[i]); } float avg=(float)sum/l; System.out.println("average of cmmd is "+avg); } } } /* wap for type casting */ import java.lang.*; class a2{ public static void main(String a[]) { int i=10,j; float x=10.5F,y; y=i; System.out.println("implicit type casting from int to float"+y); j=(int)x; System.out.println("explicit type casting from float to int"+j); } } /* wap to find run time arguments */ import java.lang.*; class a3{ public static void main(String a[]) { int l=a.length; if(l==0) { System.ou.println("NO. cmmd line argument exist"); } else { for(int i=0;i<l;i++) System.out.println((i+1)+"cmmd argument is "+a[i]); } } } /* wap to test prime no. */ class a4{ public static void main(String a[]) { int n=2,flag=0; for(int i=2;i<n;i++) { if(n%i==0) { flag=1; break; } } if(flag==0) System.out.println(n+" is a prime no."); else System.out.println(n+" is not a prime no."); } } /* find the simple interst*/ import java.io.*; class a5{ public static void main(String a[]) { int p,t,r; double si; DataInputStream in1 = new DataInputStream(System.in); try { System.out.println("Enter amount"); p=Integer.parseInt(in1.readLine()); System.out.println("Enter time"); t=Integer.parseInt(in1.readLine()); System.out.println("Enter rate"); r=Integer.parseInt(in1.readLine()); si=p*r*t/100.0; System.out.println("Simple interst is"+si); } catch(Exception e) { System.out.println("Error in Wraper classess"); } } } /* use of super keyword */ class area{ int l,b; area(int l1,int b1) { l=l1; b=b1; } void display() { System.out.println("Area of rect. is"+(l*b)); } } class shape extends area{ int l,b; shape(int l1,int b1) { super(l1,b1); l=l1; b=b1; } void display() { super.display(); System.out.println("perameter of rect. is"+2*(l+b)); } } class a6{ public static void main(String args[]) { shape s=new shape(10,10); s.display(); } } /* to find the gcd and hcf of 2 nos enterd */ import java.lang.*; import java.io.*; class a7{ public static void main(String ar[]) { int a,b,c,x,y,h; DataInputStream in1=new DataInputStream(System.in); try { System.out.println("Enter first no."); a=Integer.parseInt(in1.readLine()); System.out.println("Enter second no."); b=Integer.parseInt(in1.readLine()); if(a<b) c=a; a=b; b=c; } { x=a; y=b; while(%b!=0) { c=b; b=a%b; a=c; } System.out.println("Gcd of no. is"+b); h=x*y/b; System.out.println("hcf of no. is"+h); } catch(Exception e) { System.out.println("Error in wrapper classess"); } } } /* wap for bank */ class bank{ int ac_no,amount; bank(int ac1,int amo) { ac_no=ac1; amount=amo; } void draw(int damnt) { System.out.println("Amount before drwn"+amount); amount=amount-damnt; System.out.println("Amount after drwn"+amount); } void deposit(int damnt) { System.out.println("Amount before deposit"+amount); amount=amount+damnt; System.out.println("Amount after deposit"+amount); } } class bbank extends bank{ int ac_no,amount; static int rate=5; bbank(int ac1,int amo) { super(ac1,amo); ac_no=ac1; amount=amo; } void loan(int lamnt) { amount=amount-lamnt; System.out.println("loaned amount"+amount); } void interest(int time) { float si; si=(float)amount*time*rate/100; System.out.println("Amount of interest"+si); } } class a8{ public static void main(String ar[]) { bbank b = new bbank(1,1000); b.draw(500); b.deposit(1500); b.interest(3); b.loan(10000); } } /* factorial using recursion */ class Calculation{ int fact(int n) { int result; if(n==1) return 1; result = fact(n-1) * n; return result; } } class a9{ public static void main(String args[]) { Calculation obj_one = new Calculation(); int a = obj_one.fact(4); System.out.println("The factorial of the number is : " + a); } } /* wap for showing the concept of abstract class */ abstract class abs{ abstract void f1() { System.out.println("hello in abstract class"); } } class a10{ public static void main(String ar[]); { abs a=new abs(); a.f1(); } } /* wap for string handling */ import java.lang.*; class string1{ String str=new String(); string1(String str1) { /* for(int i=0;i<str1.length;i++) str[i]=str1[i]; */ str=str1; } void equal(string1 s) { if(str.equals(s.str)) System.out.println("Equal"); else System.out.println("No. equal"); } void rev() { char ch[]=str.toCharArray(); int l=str.length()-1; for(int i=0;i<l/2;i++) { char x=ch[i]; ch[i]=ch[l-i]; ch[l-i]=x; } String s2=new String(ch); System.out.println("Reverse of string is "+s2); } void change() { System.out.println("after changing the case value is"+str.toUpperCase()); } } class a11 { public static void main(String ar[]) { String str1=new String("mca"); string1 s1 = new string1(str1); string1 s2 = new string1(str1); s1.equal(s2); s1.rev(); s1.change(); } } /* exception handling in java */ import java.lang.*; class a12{ public static void main(String ar[]) { try { int a=10/0; } catch(ArithmeticException e) { System.out.println("Divide by zero"); } } } /* exception handling in java with multiple catch blocks and nested try*/ import java.lang.*; class a13{ public static void main(String ar[]) { try { int b=110/0; try { int a=10/0; } catch(ArithmeticException e) { System.out.println("Divide by zero"); } } catch(Exception e) { System.out.println("Arithmetic exception"); } finally { System.out.println("Error in program"); } } } import d15.*; class exte{ void f1() { System.out.println("External class of program"); } } class a14{ public static void main(String a[]) { exte d1=new exte(); d1.f1(); p14 p=new p14(); p.f1(); } } import d15.*; class a15{ public static void main(String a[]) { p15 p=new p15(); p.f1(); } } /* use of throw keyword */ import java.lang.Exception; class myexception extends Exception{ myexception(String e) { System.out.println(" "+e); } } class a17{ public static void main(String ar[]) { int rno=-1; try { if(rno<=0) throw new myexception("Roll NO cannt be negative"); } catch(myexception e) // super(e); } { } } /* thread using runnable interface */ class t implements Runnable{ int i; public void run() { for(i=0;i<7;i++) { System.out.println("Thread using runnable interface"); } } } class a18{ public static void main(String ar[]) { t t1 =new t(); Thread t2=new Thread(t1); t2.start(); } } class Chat { boolean flag = false; public synchronized void Question(String msg) { if (flag) { try { wait(); } catch (Exception e) {} } System.out.println(msg); flag = true; notify(); } public synchronized void Answer(String msg) { if (!flag) { try { wait(); } catch (Exception e) {} } System.out.println(msg); flag = false; notify(); } } class T1 implements Runnable{ Chat m; String[] s1 = { "Hi", "How are you ?", "I am also doing fine!" }; public T1(Chat m1) { this.m = m1; new Thread(this, "Question").start(); } public void run() { for (int i = 0; i < s1.length; i++) m.Question(s1[i]); } } class T2 implements Runnable { Chat m; String[] s2 = { "Hi", "I am good, what about you?", "Great!" }; public T2(Chat m2) { this.m = m2; new Thread(this, "Answer").start(); } public void run() { for (int i = 0; i < s2.length; i++) m.Answer(s2[i]); } } public class a19{ public static void main(String[] args) { Chat m = new Chat(); new T1(m); new T2(m); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; class winlis implements WindowListener{ public void windowDeactivated(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowClosing(WindowEvent e) { Window w = e.getWindow(); w.setVisible(false); w.dispose(); System.exit(0); } } class comp extends Frame{ comp() { setLayout(new FlowLayout()); winlis l=new winlis(); Label lname=new Label("Name"); TextField tname=new TextField("Deep"); add(lname); addWindowListener(l); add(tname); } } class a20{ public static void main(String a[]) { comp frame=new comp(); frame.setTitle("mca"); frame.setSize(1500,700); frame.setVisible(true); } } /* wap to create rectangle oval circle */ import java.awt.*; import java.applet.*; public class a21 extends Applet{ public void paint(Graphics g) { g.drawRect(10,10,200,100); g.drawLine(10,150,200,150); g.drawArc(500,30,100,100,120,90); g.drawOval(500,100,200,300); } } /* wap to create rectangle oval circle */ import java.awt.*; import java.applet.*; public class a21 extends Applet{ public void paint(Graphics g) { g.drawRect(10,10,200,100); g.drawLine(10,150,200,150); g.drawArc(500,30,100,100,120,90); g.drawOval(500,100,200,300); } } /* wap to create rectangle oval circle */ import java.awt.*; import java.applet.*; public class a21 extends Applet{ public void paint(Graphics g) { g.drawRect(10,10,200,100); g.drawLine(10,150,200,150); g.drawArc(500,30,100,100,120,90); g.drawOval(500,100,200,300); } } /* wap to create rectangle oval circle */ import java.awt.*; import java.applet.*; public class a21 extends Applet{ public void paint(Graphics g) { g.drawRect(10,10,200,100); g.drawLine(10,150,200,150); g.drawArc(500,30,100,100,120,90); g.drawOval(500,100,200,300); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; class windowad implements WindowListener{ public void windowClosing(WindowEvent e) { Window w = e.getWindow(); w.setVisible(false); w.dispose(); System.exit(0); } public void windowDeactivated(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified(WindowEvent e) { } } class comp extends Frame{ comp() { MenuBar mbar=new MenuBar(); Menu menunew = new Menu("New"); Menu menuedit=new Menu("edit"); MenuItem icopy = new MenuItem("Copy"); MenuItem ipaste=new MenuItem("paste"); setMenuBar(mbar); mbar.add(menunew); mbar.add(menuedit); menunew.add(icopy); menuedit.add(ipaste); addWindowListener(new windowad()); } } class a22{ public static void main(String a[]) { comp frame=new comp(); frame.setTitle("First Awt program"); frame.setSize(1500,700); frame.setVisible(true); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; class winlis extends WindowAdapter implements WindowListener{ public void windowClosing(WindowEvent e) { System.exit(0); } } s class myframe extends Frame{ myframe(String str1) { super(str1); setLayout(new BorderLayout()); add(new Button("north"),BorderLayout.NORTH); add(new Button("east"),BorderLayout.EAST); add(new Button("west"),BorderLayout.WEST); add(new Button("south"),BorderLayout.SOUTH); add(new Button("Center"),BorderLayout.CENTER); addWindowListener(new winlis()); } } class a24b{ public static void main(String ar[]) { myframe f1=new myframe("DeEP"); f1.setSize(1500,700); f1.setVisible(true); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; class winlis extends WindowAdapte implements WindowListener{ public void windowClosing(WindowEvent e) { System.exit(0); } } class myframe extends Frame{ myframe(String str1) { super(str1); setLayout(new GridLayout(4,3)); for(int i=0;i<8;i++) { String str="Button"; str+=Integer.toString(i); add(new Button(str)); } addWindowListener(new winlis()); } } class a25{ public static void main(String ar[]) { myframe f1=new myframe("DeEP"); f1.setSize(1500,700); f1.setVisible(true); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; class winlis extends WindowAdapter implements WindowListener{ public void windowClosing(WindowEvent e) { System.exit(0); } } class myframe extends Frame implements ActionListener{ CardLayout card=new CardLayout(20,20); myframe(String str1) { super(str1); setLayout(card); Button b1=new Button("b1"); Button b2=new Button("b2"); Button b3=new Button("b3"); add(b1,"Card1"); add(b2,"Card2"); add(b3,"Card3"); addWindowListener(new winlis()); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent e) { card.next(this); } } class a25b{ public static void main(String ar[]) { myframe f1=new myframe("DeEP"); f1.setSize(1500,700); f1.setVisible(true); } } import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class a26 { public static void main(String[] args) { DateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss"); //get current date time with Date() Date date = new Date(); System.out.println(date); //get current date time with Calendar() Calendar cal = Calendar.getInstance(); System.out.println(dateFormat.format(cal.getTime())); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; class winlis extends WindowAdapter implements WindowListener { public void windowClosing(WindowEvent e) { System.exit(0); } } class myframe extends Frame implements ActionListener { myframe(String str) { super(str); Button left=new Button("left"); Button right=new Button("right"); Button center=new Button("center"); Label l1=new Label("Deep Kumar"); add(l1); add(left); add(center); add(right); addWindowListener(new winlis()); left.addActionListener(this); right.addActionListener(this); center.addActionListener(this); } public void actionPerformed(ActionEvent a) { if(a.getSource()="left"); { System.out.println("Left is pressed"); } if(a.getSource()="right"); { System.out.println("right is pressed"); } if(a.getSource()="cenetr"); { System.out.println("cenetr is pressed"); } } } class a27 { public static void main(String ar[]) { myframe f1=new myframe("Deep kUmAr"); f1.setSize(1500,700); f1.setVisible(true); } }