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

Experiment 5

Aim: Write a java program to multiply two matrices.


Introduction: Matrix multiplication is a fundamental operation in linear algebra, extensively used in
mathematics, computer science, physics, engineering, and many other fields. It involves multiplying two
matrices to obtain a new matrix that represents the composition of linear transformations.
IDE used: Eclipse
Source Code:
import java.util.Scanner;

public class MatrixMultiplication {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter rows and columns for first matrix:");


int rowsFirst = scanner.nextInt();
int columnsFirst = scanner.nextInt();
int[][] firstMatrix = new int[rowsFirst][columnsFirst];
System.out.println("Enter the elements of the first matrix:");
for (int i = 0; i < rowsFirst; i++) {
for (int j = 0; j < columnsFirst; j++) {
firstMatrix[i][j] = scanner.nextInt();
}
}

System.out.println("Enter rows and columns for second matrix:");


int rowsSecond = scanner.nextInt();
int columnsSecond = scanner.nextInt();
if (columnsFirst != rowsSecond) {
System.out.println("Matrix multiplication is not possible. The number of columns in the first
matrix must be equal to the number of rows in the second matrix.");
return;
}

int[][] secondMatrix = new int[rowsSecond][columnsSecond];


System.out.println("Enter the elements of the second matrix:");
for (int i = 0; i < rowsSecond; i++) {
for (int j = 0; j < columnsSecond; j++) {
secondMatrix[i][j] = scanner.nextInt();
}
}
int[][] product = multiplyMatrices(firstMatrix, secondMatrix, rowsFirst, columnsFirst,
columnsSecond);
System.out.println("Product of the two matrices is:");
for (int[] row : product) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}

public static int[][] multiplyMatrices(int[][] firstMatrix, int[][] secondMatrix, int rowsFirst, int
columnsFirst, int columnsSecond) {
int[][] product = new int[rowsFirst][columnsSecond];

for (int i = 0; i < rowsFirst; i++) {


for (int j = 0; j < columnsSecond; j++) {
for (int k = 0; k < columnsFirst; k++) {
product[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}

return product;
}
}

Output:
Experiment 6

Aim:
Write a program in java to demonstrate the new keyword and dot operator using addition and subtraction
method.

Introduction: The new keyword in Java is used to create an instance of a class, also known as an object.
It is used to allocate memory for the object on the heap, the memory space where objects are stored. It
calls the constructor of a class to initialize the object's state.

The dot operator (“.”) is used frequently to access a field or to call a method on an object.

IDE used: Eclipse


Source Code:
public class Calculator {

public int add(int num1, int num2) { return


num1 + num2;
}
public int subtract(int num1, int num2) { return
num1 - num2;
}
public static void main(String[] args) { Calculator
calculator = new Calculator(); int
sum=calculator.add(5, 3); System.out.println("Sum:
" + sum); int difference =calculator.subtract(10, 4);
System.out.println("Difference: " + difference); }
}

Output:
Experiment 7

Aim:
Write a program in java to demonstrate the default constructor and parameterized constructor.

Introduction: In Java, a constructor is a block of codes similar to the method. It is called when an
instance of the class is created. It is a special type of method which is used to initialize the object.
Default constructor is one which doesn’t consist of any parameters and is created automatically in case
no constructors are defined by the programmer.
Parameterized constructors are used to create objects with differing values or of same values too. They
are differentiated by the compiler by the number of parameters in the list and their types.

IDE used: Eclipse


Source Code:
public class abc { int
num;

public abc() { //default constructor


num =0;
System.out.println("Default constructor called. Value of num: "+num); }

public abc(int num) { // parameterized constructor


this.num=num;
System.out.println("Parameterized constructor called. Value of num: "+num); }

public static void main(String[] args) { abc obj1 = new abc();


//default constructor called abc obj2 = new abc(10);
//parameterized constructor called
}
}

Output:
Experiment 8

Aim:
Write a program in java to demonstrate the constructor overloading.

Introduction: Constructor overloading in Java involves creating multiple constructors in a class with
different parameter lists so that every constructor can perform a different task. They are differentiated by
the compiler by the number of parameters in the list and their types.

IDE used: Eclipse


Source Code:
public class Over {
int num; public
Over() { num = 0;
System.out.println("Default constructor called. Value of num: " + num); }

public Over(int num) {


System.out.println("1 arg" + num); }

public Over(int num1, int num2) { this.num =


num1 + num2;
System.out.println("2 args" + num); }

public static void main(String[] args) {


Over obj1 = new Over();
Over obj2 = new Over(10);
Over obj3 = new Over(5, 7);
}
}

Output:
Experiment 9

Aim:
Write a program in java to demonstrate the method overloading using addition method.

Introduction: Method overloading in Java involves creating multiple methods with the same name but
with different parameter lists and different types.

Source Code:
public class Example{

public int add(int num1, int num2) {


return num1 + num2;
}

public int add(int num1, int num2, int num3) {


return num1 + num2 + num3;
}

public double add(double num1, double num2) {


return num1 + num2;
}
public static void main(String[] args) {
Example obj = new Example();
System.out.println("Sum of 5 and 3: " + obj.add(5, 3));
System.out.println("Sum of 5, 3, and 2: " + obj.add(5, 3, 2));
System.out.println("Sum of 5.5 and 3.7: " + obj.add(5.5, 3.7));
}
}

Output:
Experiment 10

Aim:
Write a program in java to demonstrate the static keyword.

Introduction: The static keyword is used to create variables and methods that belong to the class itself
rather than to instances of the class. The static keyword is used for memory management mainly. Static
variables have the property of preserving their value even after they are out of their scope. Static methods
can be called directly without having to create an instance of the class.

IDE used: Eclipse Source


Code:
class Counter{ static
int count=0;

Counter(){ count++;
}

static void print(){


System.out.println(count);
}

public static void main(String args[]){


Counter c1=new Counter(); Counter
c2=new
Counter(); Counter c3=new
Counter(); Counter.print();
}
}

Output:

You might also like