0% found this document useful (0 votes)
7 views14 pages

LABFILE

The document contains Java code for various programming assignments, including matrix operations (addition, subtraction, multiplication), calculating factorial using BigInteger, computing a function involving π and e^x using BigDecimal, and implementing a student class with instance counting. Additionally, it includes a singleton class that restricts instantiation to one object. Each section provides code snippets along with sample outputs demonstrating the functionality of the programs.

Uploaded by

adnanansari7565
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

LABFILE

The document contains Java code for various programming assignments, including matrix operations (addition, subtraction, multiplication), calculating factorial using BigInteger, computing a function involving π and e^x using BigDecimal, and implementing a student class with instance counting. Additionally, it includes a singleton class that restricts instantiation to one object. Each section provides code snippets along with sample outputs demonstrating the functionality of the programs.

Uploaded by

adnanansari7565
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

15/11/2024, 17:44 Matrix.

java

javaassignments/src/Matrix.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:1


2
3 Question 1: Java program for matrix operation take two matrices as input of
similar dimension, populate the value randomly and perform sum, difference,
product of the Matrix.
4
5 import java.util.*;
6 import java.util.Random;
7
8 public class Matrix {
9 private int[][] data;
10 private int rows;
11 private int cols;
12
13 public Matrix(int rows, int cols) {
14 this.rows = rows;
15 this.cols = cols;
16 this.data = new int[rows][cols];
17 populateMatrix();
18 }
19
20 // Method to populate the matrix with random values
21 private void populateMatrix() {
22 Random random = new Random();
23 for (int i = 0; i < rows; i++) {
24 for (int j = 0; j < cols; j++) {
25 data[i][j] = random.nextInt(10); // Random values between 0 and 9
26 }
27 }
28 }
29
30 // Method to print the matrix
31 public void printMatrix() {
32 for (int[] row : data) {
33 for (int value : row) {
34 System.out.print(value + " ");
35 }
36 System.out.println();
37 }
38 }
39
40 // Method to add two matrices
41 public Matrix add(Matrix other) {
42 Matrix result = new Matrix(rows, cols);
43 for (int i = 0; i < rows; i++) {
44 for (int j = 0; j < cols; j++) {
45 result.data[i][j] = this.data[i][j] + other.data[i][j];
46 }
47 }
48 return result;
49 }
50
51 // Method to subtract two matrices
52 public Matrix subtract(Matrix other) {
53 Matrix result = new Matrix(rows, cols);
54 for (int i = 0; i < rows; i++) {
55 for (int j = 0; j < cols; j++) {
56 result.data[i][j] = this.data[i][j] - other.data[i][j];
localhost:51647/4d909c30-5f11-40d6-9a50-06cca3fe1cd6/ 1/2
15/11/2024, 17:44 Matrix.java
57
58
59 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:2
60
61
62
63 }
64 }
65
66 return result;
67 }
68
69 // Method to multiply two matrices
70 public Matrix multiply(Matrix other) {
71 Matrix result = new Matrix(rows, other.cols);
72 for (int i = 0; i < rows; i++) {
73 for (int j = 0; j < other.cols; j++) {
74 result.data[i][j] = 0; // Initialize to 0
75 for (int k = 0; k < cols; k++) {
76 result.data[i][j] += this.data[i][k] * other.data[k][j];
77 }
78 }
79 }
80 return result;
81 }
82 public static void main(String[] args) {
83 Scanner scanner = new Scanner(System.in);
84
85 System.out.print("Enter the number of rows: ");
86 int rows = scanner.nextInt();
87 System.out.print("Enter the number of columns: ");
88 int cols = scanner.nextInt();
89
90 // Creating matrix objects
91 Matrix matrix1 = new Matrix(rows, cols);
92 Matrix matrix2 = new Matrix(rows, cols);
93
94 // Displaying the matrices
95 System.out.println("Matrix 1:");
96 matrix1.printMatrix();
97 System.out.println("Matrix 2:");
98 matrix2.printMatrix();
99
100 // Performing operations
101 Matrix sumMatrix = matrix1.add(matrix2);
102 Matrix diffMatrix = matrix1.subtract(matrix2);
103 Matrix productMatrix = matrix1.multiply(matrix2);
104
105
106 // Displaying results
107 System.out.println("Sum of Matrices:");
108 sumMatrix.printMatrix();
109 System.out.println("Difference of Matrices:");
110 diffMatrix.printMatrix();
111 System.out.println("Product of Matrices:");
112 productMatrix.printMatrix();
113
114
115 }
116 }

localhost:51647/4d909c30-5f11-40d6-9a50-06cca3fe1cd6/ 2/2
MALIK OWAIS MUSHTAQ:202404466 Page no:3
Enter the number of rows: 5
Enter the number of columns: 3
Matrix 1:
635
040
043
499
166
Matrix 2:
793
863
754
709
762
Sum of Matrices:
13 12 8
8 10 3
797
11 9 18
8 12 8
Di erence of Matrices:
-1 -6 2
-8 -2 -3
-7 -1 -1
-3 9 0
-6 0 4
Product of Matrices:
101 97 47
32 24 12
53 39 24
163 135 75
97 75 45

Enter the number of rows: 3


Enter the number of columns: 3
Matrix 1:
440
798
006
Matrix 2:
117
808
424
Sum of Matrices:
557
15 9 16
4 2 10
Di erence of Matrices:
3 3 -7
-1 9 0
-4 -2 2
Product of Matrices:
36 4 60
111 23 153
24 12 24
ff
ff
15/11/2024, 17:49 factorial.java

javaassignments/src/factorial.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:4


2
3 Question no:2 In the factorial program assume that you are taking input as a Big
Integer and you are computing factorial as a Big Integer.
4
5 import java.math.BigInteger;
6 import java.util.Scanner;
7
8 public class factorial {
9 public static void main(String[] args) {
10 int n;
11 Scanner s=new Scanner(System.in);
12 System.out.print("enter the number: ");
13 n=s.nextInt();
14 if(n<0){
15 System.out.println("not compatible for negative numbers");
16 }else{
17 BigInteger res=fact(BigInteger.valueOf(n));
18 System.out.println("the factorial of "+n+ " is "+res);
19 }
20 }
21 //recursive function to calculate factorial of the big integer
22 static BigInteger fact(BigInteger n){
23 if(n.equals(BigInteger.ZERO)){
24 return BigInteger.ONE;
25 }else {
26 return n.multiply(fact(n.subtract( BigInteger.ONE)));
27 }
28 }
29 }
30

localhost:51647/1e1cf99b-e866-467b-9831-dc9e114d52e1/ 1/1
MALIK OWAIS MUSHTAQ:202404466 Page no:5

enter the number: 5


the factorial of 5 is 120

enter the number: 0


the factorial of 0 is 1

enter the number: -9


not compatible for negative numbers

enter the number: 13


the factorial of 13 is 6227020800
15/11/2024, 17:55 biginteger.java

javaassignments/src/biginteger.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:6


2
3 Question no 3: write a java programme to vomute the following function, f = π *
e^x,make use of BigDecimal.
4
5
6 import java.math.BigDecimal;
7
8 class biginteger {
9
10 // Method to compute f = π * e^x using Math.exp() and Math.PI
11 public static BigDecimal computePiExp(BigDecimal x) {
12 // Return the result in one statement
13 return BigDecimal.valueOf(Math.PI) // Convert Math.PI to BigDecimal
14 .multiply(BigDecimal.valueOf(Math.exp(x.doubleValue()))); // Use
Math.exp() to calculate e^x
15 }
16
17 public static void main(String[] args) {
18 // Input value for x
19 BigDecimal x = new BigDecimal("-9");
20
21 // Calculate f = π * e^x
22 BigDecimal result = computePiExp(x);
23 System.out.println("f = πe^x for x = " + x + " is: " + result);
24 }
25 }
26
27

localhost:51647/61330d8f-f8f0-4a59-a67c-63ecfd459d96/ 1/1
MALIK OWAIS MUSHTAQ:202404466 Page no:7

f = πe^x for x = 2.0 is: 23.21340435736338476027105485545

f = πe^x for x = 0 is: 3.1415926535897930

f = πe^x for x = 6 is: 1267.4089338833702774356810128343

f = πe^x for x = -9 is: 0.00038770333389966811944296767773108


15/11/2024, 18:03 student.java

javaassignments/src/student.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:8


2
3 Question no 4)4.1: Define a class student which has necessary data member and
member function, this class also have a data member called instance count which
store the total number of instances of student class that has been made and it has
a get method to print instance count.
4
5
6 class student{
7 //static variable instance keeps count of no of instances created
8 static int instance=0;
9 private String name,address;
10 double pincode;
11 student(){
12 instance++;
13 this.name=name;
14 this.address=address;
15 this.pincode=pincode;
16 }
17 void getmarks(double marks){
18 System.out.println("your marks are"+marks);
19 }
20 static int setinstance(){
21 return instance;
22 }
23 static void getinstance(){
24 System.out.println(setinstance());
25 }
26 public static void main(String[] args){
27 student user1=new student();
28 student user2=new student();
29 student user3=new student();
30 student user4=new student();
31 System.out.println("no of instances");
32 student.getinstance();
33 }
34
35 }
36
37
38 Output:
39
40 no of instances 4

localhost:51647/1e6bca0c-8a1b-405a-bd63-39d6e979c610/ 1/1
15/11/2024, 18:04 singleton.java

javaassignments/src/singleton.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:9


2
3 Question:4.2: Define a class called singleton class, the advantage of singleton
class is that it will allow to create only one object at a time.
4 Write a tester program to demonstrate working of singleton class
5
6 class singleton{
7 static singleton instance;
8 private String name,address;
9 double pincode;
10 //intialise a private constructor so instances created in main class will not
be able to access it
11 private singleton(String name,String address,double pincode){
12 this.name=name;
13 this.address=address;
14 this.pincode=pincode;
15 }
16 //static function to create new instance if none is present and return same
created instance when user tries to create second or more instances
17 static singleton restrict(String name,String address,double pincode){
18 if(instance==null){
19 instance=new singleton(name,address,pincode);
20 }
21
22 return instance;
23
24 }
25 //compares instances
26 static boolean comparison(singleton user1,singleton user2){
27 if(user1.equals(user2)){
28 System.out.println("two variables are pointing to same object");
29 return true;
30 }
31 System.out.println("two variables are not pointing to same object");
32 return false;
33 }
34
35
36 }
37

localhost:51647/698835f5-642f-4570-8dbd-9bf60e02e2e5/ 1/1
15/11/2024, 18:09 Main.java

javaassignments/src/Main.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:10


2
3
4 class Main {
5 public static void main(String[] args) {
6 singleton user1=singleton.restrict("owais","kashmir",193201);
7 singleton user2=singleton.restrict("rahul","delhi",191101);
8 singleton.comparison(user1,user2);
9
10 }
11 }
12
13
14 Output:
15
16 two variables are pointing to same object

localhost:51647/604eac20-a373-490e-a18c-01f68530b1dc/ 1/1
15/11/2024, 18:14 BankAccount.java

BankAccount/src/assignment/BankAccount.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:11


2
3 Question no 5: Define a class called bank account that represent the bank account
of a person that compromises the several related data such as account
number(unique), first name, last name, address, city, pin code, Aadhar(optional),
PAN (optional), and balance. Introduced necessary constructor for initializing of
data further this class has following followings methods: Boolean Deposited (double
amount), Boolean Withdraw (double amount), Boolean Check Balance (), Boolean
Transfer (Bank Account ac, double amount). Apply your own strategy to automatically
unique account number, now test your Bank Account Class by writing a menu driven
class.
4
5 package assignment;
6 import java.lang.*;
7 import java.util.Scanner;
8 class BankAccount {
9 String first_name,last_name,addres,pin_code,aadhar,pan;
10 double acc_no,balance,ammount;
11 private static double lastaccno=1000;
12
13 //intialise the bank account constructor
14 BankAccount(double balance){
15 this.balance=balance;
16 this.acc_no=generateaccno();
17 }
18 //method for depositing ammount in your account
19 void deposit(double ammount){
20 balance=balance+ammount;
21 System.out.println("your new balance is "+balance);
22 }
23
24 //method to withdraw ammount from your account
25 boolean withdraw(double ammount){
26 if(balance-ammount>0){
27 balance=balance-ammount;
28 System.out.println("withdraw succesfull,your balance is "+balance);
29 return true;
30 }else{
31 System.out.println("balance insufficient");
32 }
33 return false;
34 }
35 //method for checking balance in your account
36 void checkbalance(){
37 System.out.println("your balance is "+balance);
38 }
39 //generate account number
40 private static double generateaccno(){
41 lastaccno++;
42 return lastaccno;
43 }
44 double getaccno(){
45 return acc_no;
46 }
47
48 //method for transfering ammount from your account
49 boolean transfer(BankAccount targetacc_no, double ammount){
50 if(withdraw(ammount)) {
51 targetacc_no.deposit(ammount);

localhost:51647/ba0c7f0f-24f9-4f72-bf33-c5cb3f4228dd/ 1/2
15/11/2024, 18:14 BankAccount.java
52
53 MALIK OWAIS MUSHTAQ:202404466 PAGE NO:12
54
55 System.out.println("tranfer succesfull to acc no" +
targetacc_no.getaccno());
56 return true;
57 }
58 else{
59 System.out.println("transfer not succesfull");
60 }
61 return false;
62
63 }
64
65 }
66

localhost:51647/ba0c7f0f-24f9-4f72-bf33-c5cb3f4228dd/ 2/2
15/11/2024, 18:16 Main.java

BankAccount/src/assignment/Main.java

1 MALIK OWAIS MUSHTAQ:202404466 PAGE NO :13


2 package assignment;
3
4 import java.util.Scanner;
5
6 class Main{
7 public static void main(String[] args) {
8
9 BankAccount user=new BankAccount(5000);
10 BankAccount targetacc=new BankAccount(500);
11 double accno= user.getaccno();
12 boolean b = true;
13 while(b){
14 System.out.println("Menu");
15 System.out.println("1.balance");
16 System.out.println("2.deposit");
17 System.out.println("3.withdraw");
18 System.out.println("4.transfer");
19 Scanner s=new Scanner(System.in);
20 System.out.println("enter the option ");
21 int option=s.nextInt();
22
23 //switch case to form menu type interface
24 switch (option){
25 case 1:
26 user.checkbalance();
27 break;
28 case 2:
29 System.out.println("enter the ammount");
30 double ammount=s.nextDouble();
31 user.deposit(ammount);
32 break;
33 case 3:
34 System.out.println("enter the ammount");
35 double withdraw=s.nextDouble();
36 user.withdraw(withdraw);
37 break;
38 case 4:
39 System.out.println("enter ammount");
40 double transfer=s.nextDouble();
41 user.transfer(targetacc,transfer);
42 break;
43 default:
44 System.out.println("invalid option");
45
46 break;
47
48 }
49 }
50 }
51 }

localhost:51647/6d9f457d-a279-4927-8116-ee8f03a4d66b/ 1/1
MALIK OWAIS MUSHTAQ:202404466 PAGE NO:14

Menu
1.balance
2.deposit
3.withdraw
4.transfer
enter the option
1

your balance is 5000.0

Menu
1.balance
2.deposit
3.withdraw
4.transfer
enter the option
2

enter the ammount:


400

your new balance is 5400.0

Menu
1.balance
2.deposit
3.withdraw
4.transfer
enter the option
3

enter the ammount:


500
withdraw succesfull,your balance is 4900.0

Menu
1.balance
2.deposit
3.withdraw
4.transfer
enter the option
4

enter ammount:
566

withdraw succesfull,your balance is 4334.0


transfer succesfull to acc no:1002.0

You might also like