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
MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 1: The Fibonacci sequence is defined by the following rule: The first two values in the sequence are 1 and 1. Java Coding class febi { public stAtic void main(String arg[]) { int a=1,b=1,s=0,n=25; while(s<=n) { System.out.println(s); s=a+b; a=b; b=s; } } } -1- MECH-213 Mechatronics (3rd Sem) OUTPUT: -2- Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 2: WAP in Java that prints all real solutions to the Quadratic eqyation ax2 + bx + c = 0. Read in a, b, c and use the quadratic formula. Java Coding import java.util.Scanner; class quardatic { public static void main(String[] args) { int a,b,c; double x,y; Scanner s=new Scanner(System.in); System.out.println("Enter the values of a,b, and c"); a=s.nextInt(); b=s.nextInt(); c=s.nextInt(); int k=(b*b)-4*a*c; if(k<0) { System.out.println("No real roots"); } else { double l=Math.sqrt(k); x=(-b-l)/2*a; y=(-b+l)/2*a; System.out.println("Roots of given equation:"+x+" "+y); } } } -3- MECH-213 Mechatronics (3rd Sem) OUTPUT: -4- Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 3: WAP in Java to implement the sum of the digits of a number. Java Coding import java.util.Scanner; class sumofdigit { public static void main(String arg[]) { int n,s=0; Scanner ss=new Scanner(System.in); System.out.println("Enter the number"); n=ss.nextInt(); while(n>0) { s=s+n%10; n=n/10; } System.out.println("The sum of digit is = "+s); } } -5- MECH-213 Mechatronics (3rd Sem) OUTPUT: -6- Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 4: WAP in Java to check whether the number is prime or not. Java Coding import java.util.Scanner; class prime { public static void main(String arg[]) { int n,i; Scanner ss=new Scanner(System.in); System.out.println("Enter the number"); n=ss.nextInt(); for(i=2;i<n;i++) { if(n%i==0) { System.out.println("Not Prime"); break; } } if(i==n) System.out.println("Prime"); } } -7- MECH-213 Mechatronics (3rd Sem) OUTPUT: -8- Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 5: WAP in Java that implements Factorial of a Given number. Java Coding import java.util.Scanner; class fact { public static void main(String arg[]) { int number; int fact=1; Scanner ss=new Scanner(System.in); System.out.println("enter the number"); number=ss.nextInt(); for(int i=(number-1);i>1;i--) { fact=fact*i; } System.out.println("fact of no. is="+fact); } } -9- MECH-213 Mechatronics (3rd Sem) OUTPUT: - 10 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 6: WAP in Java that creates three threads. First thread displays “Good Morning” every one second, the second thread displays “Hello” every two seconds and the third thread displays “Welcome” every three seconds. Java Coding class t1 extends Thread { public void run() { try { Thread.sleep (1000); System.out.println("Good Morning"); } catch(Exception l) { System.out.println(l); } } } class t2 extends Thread { public void run() { try { Thread.sleep (2000); System.out.println("hello"); } catch(Exception l) { System.out.println(l); } } } class t3 extends Thread { public void run() { try { Thread.sleep (3000); - 11 - MECH-213 Mechatronics (3rd Sem) System.out.println("WelCome"); } catch(Exception l) { System.out.println(l); } } } class hello11 { public static void main(String arg[]) { t1 o=new t1(); t2 o1= new t2(); t3 o2= new t3(); o.start(); o1.start(); o2.start(); } } - 12 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) OUTPUT: - 13 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 7: WAP in Java that implements the method of Interface. Java Coding import java.util.Random; interface SharedConstants { int NO = 0; int YES = 1; int MAYBE = 2; int LATER = 3; int SOON = 4; int NEVER = 5; } class Question implements SharedConstants { Random rand = new Random(); int ask() { int prob = (int) (100 * rand.nextDouble()); if (prob < 30) return NO; // 30% else if (prob < 60) return YES; // 30% else if (prob < 75) return LATER; // 15% else if (prob < 98) return SOON; // 13% else return NEVER; // 2% } } class AskMe implements SharedConstants { static void answer(int result) { switch(result) { case NO: System.out.println("No"); break; case YES: System.out.println("Yes"); break; case MAYBE: System.out.println("Maybe"); break; case LATER: System.out.println("Later"); break; case SOON: - 14 - MECH-213 Mechatronics (3rd Sem) System.out.println("Soon"); break; case NEVER: System.out.println("Never"); break; } } public static void main(String args[]) { Question q = new Question(); answer(q.ask()); answer(q.ask()); answer(q.ask()); answer(q.ask()); } } - 15 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) OUTPUT: - 16 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 8: WAP in Java that check whether a given string is a Palindrom or not. Java Coding class Palindrom { public static void main(String arg[]) { String str = "abba"; StringBuffer str1 = new StringBuffer(str).reverse(); String str2= str1. toString(); if(str. equalsIgnoreCase(str2)) System.out.println("No. is Palindrom"); else System.out.println("No. is not Palindrom"); } } - 17 - MECH-213 Mechatronics (3rd Sem) OUTPUT: - 18 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 9: WAP in Java that works as simple calculator. Using a grid layout to arrange buttons for the Digits and for the +,-,*,% operation. Java Coding import java.awt.*; import java.awt.event.*; import java.applet.*; public class myevent extends Applet implements ActionListener { Label l1,l2,l3; Button b1,b2,b3,b4,b5; TextField t1,t2,t3; public void init() { setLayout(null); l1 = new Label("Enter the first value"); l2 = new Label("Enter the second value"); l3 = new Label("Result"); t1 = new TextField(30); t2 = new TextField(30); t3 = new TextField(30); b1 = new Button("ADDITION"); b2 = new Button("MULTIPLICATION"); b3 = new Button("SUBTRACTION"); b4 = new Button("DivISION"); b5 = new Button("CLEAR"); add(t1); add(t2); add(t3); add(l1); add(l3); add(l2); add(b1); add(b2); - 19 - MECH-213 Mechatronics (3rd Sem) add(b3); add(b4); add(b5); l1.setBounds(50,100,150,30); l2.setBounds(50,150,150,30); l3.setBounds(50,200,150,30); t1.setBounds(250,100,150,30); t2.setBounds(250,150,150,30); t3.setBounds(250,200,150,30); //t3.setVisible(false); b1.setBounds(230,240,100,30); b2.setBounds(330,240,100,30); b3.setBounds(430,240,100,30); b4.setBounds(530,240,100,30); b5.setBounds(630,240,100,30); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="ADDITION") { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); int c = a+b; t3.setText(String.valueOf(c)); } if(e.getActionCommand()=="MULTIPLICATION") { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); int c = a*b; t3.setText(String.valueOf(c)); } if(e.getActionCommand()=="SUBTRACTION") { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); - 20 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) int c = a-b; t3.setText(String.valueOf(c)); } if(e.getActionCommand()=="DivISION") { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); int c = a/b; t3.setText(String.valueOf(c)); } if(e.getActionCommand()=="CLEAR") { t1.setText(""); t2.setText(""); t3.setText(""); } } } /*<applet code="myevent" width =1000 height =1000></applet>*/ - 21 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) OUTPUT: - 22 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Program 10: WAP in JAVA for handling Mouse events. Java Coding import java.awt.*; import java.awt.event.*; import java.applet.*; public class Mouse11 extends Applet implements MouseListener { public void init() { addMouseListener(this); } public void mouseClicked(MouseEvent e) { showStatus("MouseClicked"); setBackground(Color.red); } public void mousePressed(MouseEvent e) { showStatus("MousePressed"); setBackground(Color.green); } public void mouseEntered(MouseEvent e) { showStatus("MouseEntered"); setBackground(Color.blue); } public void mouseExited(MouseEvent e) { showStatus("MouseExit"); setBackground(Color.pink); } public void mouseReleased(MouseEvent e) { showStatus("MouseReleased"); setBackground(Color.yellow); } } /*<applet code="Mouse11" height="500" width="700"> </applet>*/ - 23 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 OUTPUT: Mouse Entered Mouse Exit Mouse Pressed - 24 - MECH-213 Mechatronics (3rd Sem) Program 11: WAP in JAVA for handling key event. Java Coding import java.awt.*; import java.awt.event.*; import java.applet.*; public class Key11 extends Applet implements KeyListener { public void init() { addKeyListener(this); } public void keyPressed(KeyEvent e) { showStatus("KeyPressed"); setBackground(Color.green); } public void keyReleased(KeyEvent e) { showStatus("KeyReleased"); setBackground(Color.blue); } public void keyTyped(KeyEvent e) { showStatus("KeyTyped"); setBackground(Color.pink); } } /*<applet code="Mouse11" height="500" width="700"> </applet>*/ - 25 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Output: - 26 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 12: Develop an applet that receives an integer in one text field, and computes its factorial value and return it in another text field, when the button named “Compute” is clicked. Java Coding import java.awt.*; import java.awt.event.*; import java.applet.*; public class myevent11 extends Applet implements ActionListener { Label l1,l2; Button b1; TextField t1,t2; public void init() { setLayout(null); l1 = new Label("Enter the number"); l2 = new Label("Result"); t1 = new TextField(30); t2 = new TextField(30); b1 = new Button("Compute"); add(t1); add(t2); add(l1); add(l2); add(b1); l1.setBounds(50,100,150,30); l2.setBounds(50,150,150,30); - 27 - MECH-213 Mechatronics (3rd Sem) t1.setBounds(250,100,150,30); t2.setBounds(250,150,150,30); b1.setBounds(230,240,100,30); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="Clicked") { int a = Integer.parseInt(t1.getText()); int fact =1; for (int i= 1; i<=a; i++) { fact=fact*i; } t2.setText(String.valueOf(fact)); } } } /*<applet code="myevent11" width =1000 height =1000></applet>*/ - 28 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) OUTPUT: - 29 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 13: Develop an applet that displays a simple Message. Java Coding import java.awt.*; import java.awt.event.*; import java.applet.*; public class myevent111 extends Applet { public void paint (Graphics g) { setBackground( Color.red ); g.drawString("What is your name?", 90, 70); g.drawString("My name is Pankaj Dhir", 90, 90); } } /*<applet code="myevent111" width =1000 height =1000></applet>*/ - 30 - MECH-213 Mechatronics (3rd Sem) OUTPUT: - 31 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Roll No.11115504 Program 14: WAP in JAVA that implements a simple Client / Server application. Java Coding of Simple Client program import java.net.*; import java.io.*; public class Clint1 { public static void main(String arg[]) { byte byteread; try { Socket ss = new Socket("localhost",1234); InputStream in = ss.getInputStream(); do { byteread= (byte)in.read(); System.out.print((char)byteread); } while(byteread!=-1); in.close(); } catch(Exception e) { System.out.println(e); } } } - 32 - MECH-213 Mechatronics (3rd Sem) Java Coding of Server Program import java.io.*; import java.io.*; public class Server1 { public static void main(String arg[]) { try { ServerSocket ss = new ServerSocket(1234,6); Socket s = ss.accept(); System.out.println("Server is connected and wait for clint request"); FileInputStream fis = new FileInputStream("server1.java"); OutputStream os = s.getOutputStream(); PrintWriter pr = new PrintWriter(os); while(true) { char c =(char)(fis.read()); if(c==-1) break; os.write(c); } pr.close(); os.close(); fis.close(); s.close(); ss.close(); } catch(Exception e) { System.out.println(e); } } } - 33 - Roll No.11115504 MECH-213 Mechatronics (3rd Sem) Output: - 34 - Roll No.11115504