Posts

Showing posts with the label Java

8 A. Design a AWT program to print the factorial for an input value.

Program:   import java.awt.*; import java.awt.event.*; class Factorial extends Frame implements ActionListener { TextField tf; Button b; Label n, l, r; Factorial() {   n = new Label("AWT Factorial Program"); l = new Label("Enter number"); r = new Label(); tf = new TextField(); b = new Button("Factorial");   n.setBounds(30, 40, 200, 20); l.setBounds(30, 70, 150, 20); r.setBounds(30, 170, 200, 20); tf.setBounds(30, 90, 190, 30); b.setBounds(30, 130, 190, 30);   add(n); add(l); add(r); add(tf); add(b); setSize(250,210); setLayout(null);//no layout manager   setVisible(true);//now frame will be visible, by default not visible   b.addActionListener(this); addWindowListener(new WindowAdapter() {               public void windowClosing(WindowEvent e)     ...

7 C. Write a java program to implement multithreading.

  Program : class MultiThreading  extends  Thread { public void run() { try{ for(int i=1;i<11;i++) { System.out.println(i); Thread.sleep(500); } } catch(Exception e) {} } public static void main(String args[]) { MultiThreading t1=new MultiThreading(); MultiThreading t2=new MultiThreading(); try{ t1.setName("My first thread"); t1.start(); t1.join(); t2.start(); } catch(Exception e) {} } }

7 B. Write a java program to implement thread life cycle.

 Program : class Pract7b { public static void main(String args[]) { System.out.println(Thread.currentThread().getName()); for(int i=0;i<10;i++) { new Thread(""+i) { public void run() { System.out.println("Thread:"+getName()+"running"); } }.start(); } } }

7 A. Write a java program to implement the vectors.

 Program : import java.util.Vector; class Pract7a { public static void main(String args[]) { Vector<String> v=new Vector<String>(); v.add("Red"); v.add("Green"); v.add("Blue"); System.out.println("Vector Elements are:-"+v); v.add(2,"Yellow"); System.out.println("After Adding Element at second position:-"+v); System.out.println("Element at third position:-"+v.get(3)); System.out.println("First Element:-"+v.firstElement()); System.out.println("Last Element:-"+v.lastElement()); System.out.println("Is this vector empty?"+v.isEmpty()); } }

6 C. Write a java program for multiplying two matrices and print the result for the same.

 Program : import java.util.Scanner; public class MatAdd { public static void main(String args[]) { int i, j; int mat1[][] = new int[2][2]; int mat2[][] = new int[2][2]; int mat3[][] = new int[2][2]; Scanner sc = new Scanner(System.in); System.out.print("Enter Matrix 1 Elements : "); for(i=0; i<2; i++) { for(j=0; j<2; j++) { mat1[i][j] = sc.nextInt(); } } System.out.print("Enter Matrix 2 Elements : "); for(i=0; i<2; i++) { for(j=0; j<2; j++) { mat2[i][j] = sc.nextInt(); } } System.out.print("Adding both Matrix to form the Third Matrix...\n"); System.out.print("The Two Matrix Added Successfully..!!\n"); System.out.print("The New Matrix will be :\n"); for(i=0; i<2; i++) { for(j=0; j<2; j++) { mat3[i][j] = mat1[i][j] * mat2[i][j]; System.out.print(mat3[i][j]+ " "); } System.out.println(""); } } }

6 B. Write a java program to add two matrices and print the resultant matrix.

 Program : import java.util.Scanner; public class MatAdd { public static void main(String args[]) { int i, j; int mat1[][] = new int[2][2]; int mat2[][] = new int[2][2]; int mat3[][] = new int[2][2]; Scanner sc = new Scanner(System.in); System.out.print("Enter Matrix 1 Elements : "); for(i=0; i<2; i++) { for(j=0; j<2; j++) { mat1[i][j] = sc.nextInt(); } } System.out.print("Enter Matrix 2 Elements : "); for(i=0; i<2; i++) { for(j=0; j<2; j++) { mat2[i][j] = sc.nextInt(); } } System.out.print("Adding both Matrix to form the Third Matrix...\n"); System.out.print("The Two Matrix Added Successfully..!!\n"); System.out.print("The New Matrix will be :\n"); for(i=0; i<2; i++) { for(j=0; j<2; j++) { mat3[i][j] = mat1[i][j] + mat2[i][j]; System.out.print(mat3[i][j]+ " "); } System.out.println(""); } } }