Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

SYIT46

Practical 1A

Write a Java program to print table of entered number.

Program:
Output:
Practical 1B

Write a Java program to print a pattern.

Program:

Output:
Practical 1C

Write a Java program to print area and circumference of a circle.

Program:

Output:
Practical 2A

Write a Java program for binary addition

Program:
Output:
Practical 2B

Write a program to convert binary number to decimal and decimal to binary.

Program:

Output:
Practical 2C

Write a program to reverse a string.

Program:

Output:
Practical 3A

Write a program to count number of digits, spaces and special characters in a string

Program:
Output:
Practical 3B

Write a program to add the elements of an array of string by converting them in long

Program:

Output:
Practical 3C

Write a java program to print the smallest and largest number in an array

Program:
Output:

Practical 4A

Write a Java program to make an array ascending and descending order

Program:
Output:
Practical 4B

Write a program to use constructors and destructors.

Program:

Output:
Practical 4C

Write a program to use abstarct classes in java

Program:
Output:
Practical 5A

Write a program to implement inheritance in java.

Program:

Output:
Practical 5B

Write a program in java to implement method overriding

Program:

Output:
Practical 5c:

Write a program to execute interfaces:

Program:

Interface S {

Public void show();

Interface T extends S{

Public void display();

Class Prac5C implements T {

@Override

Public void show() {

System.out.println(“From interface S”);

@Override

Public void display() {

System.out.println(“From interface T”);

Public static void main(String[] argz) {

Prac5C ob = new Prac5C();

Ob.show();

Ob.display();
}

Output:
Practical 6B

Write a program to add two matrix

Program:

Import java.util.Scanner;

Class Prac6B {

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 scanner = new Scanner(System.in);

System.out.println(“Enter Matrix 1 Elements: “);

For(i=0; i<2; i++)

For(j=0; j<2; j++)

Mat1[i][j] = scanner.nextInt();

System.out.println(“\nEnter Matrix 2 Elements: “);

For(i=0; i<2; i++)

For(j=0; j<2; j++)

Mat2[i][j] = scanner.nextInt();

System.out.println(“\nAdding both the matrices..”);


For(i=0; i<2; i++)

For(j=0; j<2; j++)

Mat3[i][j] = mat1[i][j] + mat2[i][j];

System.out.println(“\nThe Two Matrices were added successfully..”);

System.out.println(“\nThe new Matrix will be: “);

For(i=0; i<2; i++) {

For(j=0; j<2; j++) {

System.out.print(mat3[i][j] + “ “);

System.out.println();

}
Output:
Practical 6c

Program to execute matrix multiplication

Program:

Import java.util.Scanner;

Class Main {

Public static void main(String[] argz) {

Int base;

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter the base of the squared matrices: “);

Base = scanner.nextInt();

Int[][] mat1 = new int[base][base];

Int[][] mat2 = new int[base][base];

Int[][] mat3 = new int[base][base];

System.out.println(“\nEnter the elements of 1st matrix Row wise: “);

For(int i=0; i<base; i++)

For(int j=0; j<base; j++)

Mat1[i][j] = scanner.nextInt();

System.out.println(“\nEnter the elements of 2nd matrix Row wise: “);

For(int i=0; i<base; i++)

For(int j=0; j<base; j++)

Mat2[i][j] = scanner.nextInt();
System.out.println(“\nMultiplying the matrices..”);

For(int i=0; i<base; i++)

For(int j=0; j<base; j++)

For(int k=0; k<base; k++)

Mat3[i][j] += mat1[i][k] * mat2[k][j];

System.out.println(“\nThe product is: “);

For(int i=0; i<base; i++) {

For(int j=0; j<base; j++) {

System.out.print(mat3[i][j] + “ “);

System.out.println();

}
Output:
Practical 7b:

Write Java program to execute threads

Program:

Import java.util.Vector;

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();

}
Output:
Practical 7c:
Write a program to execute threads
Program:
Import java.io.*;

Class A extends Thread

Public void run()

System.out.println(“Start A”);

For(int i=1;i<=5;i++)

System.out.println(“Thread A i:”+i);

System.out.println(“Exit A”);

Class B extends Thread

{
Public void run()

System.out.println(“Start B”);

For(int j=1;j<=5;j++)

System.out.println(“Thread B j:”+j);

System.out.println(“Exit B”);
}

Class C extends Thread

Public void run()

System.out.println(“Start C”);
For(int k=1;k<=5;k++)

System.out.println(“Thread C k:”+k);

System.out.println(“Exit C”);

Class D extends Thread

Public void run()

System.out.println(“Start D”);

For(int m=1;m<=5;m++)

System.out.println(“Thread D m:”+m);

System.out.println(“Exit D”);

}
}

Class Pract7c

Public static void main(String args[])

New A().start();

New B().start();

New C().start();

New D().start();

}
Output:
Practical 8a
Write a program to read a file
Program:
Import java.io.*;

Public class Main {

Public static void main(String[] args) throws FileNotFoundException, IOException {

InputStream is=new FileInputStream(“text1.txt”);

DataInputStream dis=new DataInputStream(is);

Int count= is.available();

Byte a[]=new byte[count];

Is.read(a);

For (byte b : a) {

Char k=(char)b;

System.out.print(k+”-“);

Output:
Text1.t
Practical 8b:
Write a Java lrogram to copy contents of one text file to another
Program:

Import java.io.*;
Class Main {

Public static void main(String[] args) throws FileNotFoundException, IOException {

FileInputStream fis=null;

FileOutputStream fos=null;

Try {

File inF=new File(“text1.txt”);

File outF=new File(“text2.txt”);

Fis=new FileInputStream(inF);

Fos=new FileOutputStream(outF);

Byte buff[]=new byte[1024];

Int length;

While((length=fis.read(buff))>0)

Fos.write(buff,0,length);

Fis.close();

Fos.close();

System.out.println(“Contents Copied…”);

} catch (Exception e) {

e.printStackTrace();

}}}
text2.txt before execution:

text2.txt after execution:


Practical 8c:
Write a Java program to create a text file with the entered name phone number and address
Program:
Import java.io.*;
Import java.util.Scanner;

Public class Main {

Public static void main(String[] args) throws FileNotFoundException, IOException {

String s1,s2,s3;

Scanner sc=new Scanner(System.in);

System.out.println(“Enter name: “);

S1= sc.nextLine();

System.out.println(“Enter phone number”);

S2= sc.nextLine();

System.out.println(“Enter address: “);

S3= sc.nextLine();

OutputStream fos=new FileOutputStream(“text1.txt”);

Byte b1[]=s1.getBytes();

Fos.write(b1);

Byte b2[]=s2.getBytes();

Fos.write(b2);

Byte b3[]=s3.getBytes();

Fos.write(b3);

Fos.close();

System.out.println(“File Created”);

}
Output:

Text file:
Practical 9c:
Write a Java program to execute exception handling
Program:

Class Pract9c

Public static void main(String args[])

Int a[]={5,10};

Int b=5;

Try

Int x=a[2]/(b-a[0]);
}

Catch(ArithmeticException e)

System.out.println(“Divison By Zero Not Possible”);

Catch(ArrayIndexOutOfBoundsException e)

System.out.println(“Array Index Error”);

Finally

Int y=a[1]/a[0];

System.out.println(“Result:-“+y);

}
}

}
Output:

You might also like