You are on page 1of 18

Indian Institute of Information Technology, Pune

Page: 1

Department Electronics and communication engineering


Name: Agrawal Himanshu Enrolment No: 112016001
Sushil
Year: Second Semester: Fourth
Data of Submission 14th Feb, 2022 Subject Name Java

Published Date 08-02-2022


Lab Assignment – 3
Q1 WAJP to make a prototype of checking keyboard keys.

Ans-1 Code: -
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
boolean f=true;
System.out.println("Enter the Key or type in 123 to exit: \n");
while(f)
{
String s=br.readLine();
if(s.compareTo("123")==0)
break;
char c=s.charAt(0);
System.out.println(c+" Character is typed ");
System.out.println("Keyboard working fine enter 1 else enter
2: ");
int i=Integer.parseInt(br.readLine());
if(i==2)
f=false;
}
if(f)
System.out.println("\nKeyboard working fine");
else
System.out.println("\nKeyboard not working fine");
}
}
Indian Institute of Information Technology, Pune

Page: 2
Output: -

Q2 WAJP to read a text and make operations to find and


replace operation.
Ans-2 Code: -
import java.util.*;
public
class Main
{
public
static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the message: ");
String message = sc.nextLine();
System.out.println();
System.out.print("Enter the string to find: ");
String find_str = sc.nextLine();
System.out.println();
int index = message.indexOf(find_str);
if (index == -1)
{
System.out.println("Not found in message, Please try
again!!");
return;
}
System.out.print("Enter the string to replace with: ");
String replace_str = sc.nextLine();
System.out.println();
Indian Institute of Information Technology, Pune

Page: 3
int l = find_str.length();
String ans = "";
for (int i = 0; i < index; i++)
{
ans += message.charAt(i);
}
ans += replace_str;
for (int i = index + l; i < message.length(); i++)
{
ans += message.charAt(i);
}
System.out.println(ans);
}
}

Output: -

Q3 WAJP to find factorial of N number using recursion


technique.
Ans-3 Code: -
import java.util.*;
public
class Main
{
public
static int factorial(int number)
{
if (number == 1)
{
Indian Institute of Information Technology, Pune

Page: 4
return 1;
}
return number * factorial(number - 1);
}
public
static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = sc.nextInt();
System.out.println();
int ans = factorial(num);
System.out.print("The factorial of the entered number is: " +
ans);
}
}

Output: -

Q4 WAJP to read any 10 Names of Student do following


Operations in Menu:
1. To Upper String
2. To Lower String
3. To Calculate Length of String
4. To Display in reverse pattern
5. Quit
Indian Institute of Information Technology, Pune

Page: 5

Ans-4 Code: -
import java.util.*;
public
class Main
{
public
static String reverse(String s)
{
return new StringBuffer(s).reverse().toString();
}
public
static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int tc = 10;
String names[] = new String[10];
for (int i = 0; i < tc; i++)
{
System.out.print("Enter the name of student : ");
String name = sc.nextLine();
names[i] = name;
}
System.out.println("Here are the names you entered: ");
for (int i = 0; i < tc; i++)
{
System.out.println((i + 1) + " " + names[i]);
}
while (true)
{

System.out.println("*****************************************
*********");
System.out.println("\t\tMENU");
System.out.println("1) Convert to upper-string");
System.out.println("2) Convert to lower-string");
System.out.println("3) Length of string");
System.out.println("4) Reverse of string");
System.out.println();

System.out.println("*****************************************
*********");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
Indian Institute of Information Technology, Pune

Page: 6
if (choice > 4)
{
System.out.println("End of program");
break;
}
System.out.print("Enter the number of name, which you wish to
update");
int name_choice = sc.nextInt();
System.out.println();
if (choice == 1)
{
System.out.println("Current name is: " +
names[name_choice - 1]);
names[name_choice - 1] = names[name_choice -
1].toUpperCase();
System.out.println("New name is: " + names[name_choice -
1]);
}
else if (choice == 2)
{
System.out.println("Current name is: " +
names[name_choice - 1]);
names[name_choice - 1] = names[name_choice -
1].toLowerCase();
System.out.println("New name is: " + names[name_choice -
1]);
}
else if (choice == 3)
{
System.out.println("Length of name is: " +
names[name_choice - 1].length());
}
else if (choice == 4)
{
System.out.println("Current name is: " +
names[name_choice - 1]);
System.out.println("New name is: " +
reverse(names[name_choice - 1]));
}
else
{
System.out.println("End of program");
break;
}
}
Indian Institute of Information Technology, Pune

Page: 7
}
}

Output: -
Indian Institute of Information Technology, Pune

Page: 8
Indian Institute of Information Technology, Pune

Page: 9

Q5 WAJP a make Menu to do Matrix Operation


1. Add Matrix.
2. Subtract Matrix
3. Multiplication Matrix.
4. Invers Matrix
5. Quit
Indian Institute of Information Technology, Pune

Page: 10
Ans-5 Code: -
import java.util.*;
public
class Main
{
public
static int N;
public
static int determinant(int[][] arr, int m)
{
int D = 0;
if (m == 1)
return arr[0][0];
int[][] tempArray = new int[N][N];
int sign = 1;
for (int f = 0; f < m; f++)
{
cofactors(arr, tempArray, 0, f, m);
D += sign * arr[0][f] * determinant(tempArray, m - 1);
sign = -sign;
}
return D;
}
public
static void adjoint_matrix(int[][] arr, int[][] adjacent_arr)
{
if (N == 1)
{
adjacent_arr[0][0] = 1;
return;
}
int sign = 1;
int[][] tmp = new int[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cofactors(arr, tmp, i, j, N);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adjacent_arr[j][i] = (sign) * (determinant(tmp, N -
1));
}
}
}
public
Indian Institute of Information Technology, Pune

Page: 11
static void cofactors(int[][] arr, int[][] arr1, int x, int y, int z)
{
int i = 0, j = 0;
for (int row = 0; row < z; row++)
{
for (int col = 0; col < z; col++)
{
if (row != x && col != y)
{
arr1[i][j++] = arr[row][col];
if (j == z - 1)
{
j = 0;
i++;
}
}
}
}
}
public
static boolean inverse(int[][] arr, float[][] inv)
{
int det = determinant(arr, N);
if (det == 0)
{
return false;
}
int[][] adj = new int[N][N];
adjoint_matrix(arr, adj);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
inv[i][j] = adj[i][j] / (float)det;
return true;
}
public
static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter order of matrix: ");
int n = sc.nextInt();
System.out.println();
int[][] matrix1 = new int[n][n];
int[][] matrix2 = new int[n][n];

System.out.println("Enter matrix 1: ");


Indian Institute of Information Technology, Pune

Page: 12
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print("Enter element at" + i + "," + j + " ->
");
matrix1[i][j] = sc.nextInt();
}
}
System.out.println();
System.out.println("Enter matrix is: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(matrix1[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
System.out.println("Enter matrix 2: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print("Enter element at" + i + "," + j + " ->
");
matrix2[i][j] = sc.nextInt();
}
}
System.out.println();
System.out.println("Enter matrix is: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(matrix2[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
while (true)
{
Indian Institute of Information Technology, Pune

Page: 13
System.out.println("*****************************************
***************");
System.out.println("\t\tMENU");
System.out.println("1) Addition of matrix");
System.out.println("2) Subtraction of matrix");
System.out.println("3) Product of matrix");
System.out.println("4) Inverse of matrix");

System.out.println("*****************************************
***************");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
System.out.println();
if (choice == 1)
{
int[][] result = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("Sum matrix is: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(result[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
else if (choice == 2)
{
int[][] result = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
System.out.println("Diffrence matrix is: ");
Indian Institute of Information Technology, Pune

Page: 14
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(result[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
else if (choice == 3)
{
int[][] result = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
result[i][j] += matrix1[i][k] *
matrix2[k][j];
}
}
}
System.out.println("Diffrence matrix is: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(result[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
else if (choice == 4)
{
System.out.println("Matrix 1 :-\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(matrix1[i][j]);
System.out.print(" ");
}
Indian Institute of Information Technology, Pune

Page: 15
System.out.println();
}
N = n;
float[][] inversedMatrix = new float[N][N];
if (inverse(matrix1, inversedMatrix))
{
System.out.println("Inverse of Matrix 1 is: ");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
System.out.print(inversedMatrix[i][j] + "");
}
System.out.println();
}
else
{
System.out.println("Matrix is singular");
}
}
else
{
System.out.println("Program over..");
break;
}
}
}
}
Indian Institute of Information Technology, Pune

Page: 16

Output: -
Indian Institute of Information Technology, Pune

Page: 17
Indian Institute of Information Technology, Pune

Page: 18

You might also like