Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview: Write a Java program for basic matrix operations. Your program should

ID: 3743833 • Letter: O

Question

Overview: Write a Java program for basic matrix operations. Your program should read in two matrices as inputs from two different text files and employ a singly/doubly linked list representation to store the two matrices internally and perform the following operations on them. Add Subtract Multiply Transpose Technical Requirement of Solution: You are required to create your own singly or doubly linked list data structures from scratch. That means: Your solution cannot use any existing collection framework or other library methods in Java for matrix based operations. . You should not use existing variants of Array, List, ArrayList, Vector for storing integers and two-diiensional arrays are strictly prohibited You are allowed to read the input file EXACTLY ONCE for all operations. . For determinant operation, you may augment your inked ist node to retain row column id and emp ov recursion to direct v implement the sta method for computing determinant of a matrix ou are encouraged to design your own node representation (e-g, each node element has two pointers ne to its next right and another to its next bottom element that facilitate both horizontal and vertical traversals like one gets in a 2d array . You will need to complete this assignment in Java

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

class UserMatrix {

private int row;

public int getRow() {

return row;

}

private int column;

public int getColumn() {

return column;

}

public LinkedList root[];

public UserMatrix(int row, int column) {

this.row = row;

this.column = column;

root = new LinkedList[row];

}

}

class LinkedList {

int data;

LinkedList next;

public LinkedList() {

// TODO Auto-generated constructor stub

}

public LinkedList(int data) {

// TODO Auto-generated constructor stub

this.data = data;

this.next = null;

}

}

class Operation {

public UserMatrix add(UserMatrix A, UserMatrix B) {

UserMatrix c = new UserMatrix(A.getRow(), A.getColumn());

int rowC = A.getRow();

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

LinkedList C = null, obj1 = null, obj2 = A.root[i], obj3 = B.root[i];

while (obj2 != null && obj3 != null) {

if (C == null) {

obj1 = new LinkedList(obj2.data + obj3.data);

C = obj1;

obj2 = obj2.next;

obj3 = obj3.next;

} else {

obj1.next = new LinkedList(obj2.data + obj3.data);

obj1 = obj1.next;

obj2 = obj2.next;

obj3 = obj3.next;

}

}

c.root[i] = C;

}

return c;

}

public UserMatrix subtract(UserMatrix A, UserMatrix B) {

UserMatrix c = new UserMatrix(A.getRow(), A.getColumn());

int rowC = A.getRow();

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

LinkedList C = null, obj1 = null, obj2 = A.root[i], obj3 = B.root[i];

while (obj2 != null && obj3 != null) {

if (C == null) {

obj1 = new LinkedList(obj2.data - obj3.data);

C = obj1;

obj2 = obj2.next;

obj3 = obj3.next;

} else {

obj1.next = new LinkedList(obj2.data - obj3.data);

obj1 = obj1.next;

obj2 = obj2.next;

obj3 = obj3.next;

}

}

c.root[i] = C;

}

return c;

}

public int getItem(UserMatrix A , int i,int j) {

LinkedList obj = A.root[i];

int k=0;

while(obj!=null ) {

if(k==j) {

return obj.data;

}

k++;

obj=obj.next;

}

return -1 ;

}

public UserMatrix transpose(UserMatrix A) {

UserMatrix c = new UserMatrix(A.getColumn(), A.getRow());

int rowC = c.getRow();

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

LinkedList C = null, obj1 = null;

for (int j = 0; j < c.getColumn(); j++) {

if (j == 0) {

int temp = getItem(A, j, i);

obj1 = new LinkedList(temp);

C = obj1;

} else {

int temp = getItem(A, j, i);

obj1.next = new LinkedList(temp);

obj1 = obj1.next;

}

}

c.root[i] = C;

}

return c;

}

public UserMatrix multiply(UserMatrix A, UserMatrix B) {

if(A.getColumn()!=B.getRow()) {

System.out.println("dimension mismatch ");

return null;

}

UserMatrix c = new UserMatrix(A.getRow(), B.getColumn());

int rowC = c.getRow();

B=transpose(B);

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

LinkedList C = null, obj1 = null;

for (int j = 0; j < c.getColumn(); j++) {

if (j == 0) {

int temp = getcal(A.root[i],B.root[j]);

obj1 = new LinkedList(temp);

C = obj1;

} else {

int temp = getcal(A.root[i],B.root[j]);

obj1.next = new LinkedList(temp);

obj1 = obj1.next;

}

}

c.root[i] = C;

}

return c;

}

private int getcal(LinkedList A, LinkedList B) {

// TODO Auto-generated method stub

int k=0;

while(A!=null ) {

k += (A.data * B.data);

A = A.next;

B=B.next;

}

return k;

}

}

public class TestClass {

public static void main(String[] args) throws FileNotFoundException {

String str1 = "D:\java ecllipse projects\Sample\src\Afile.txt";

File file = new File(str1);

Scanner sc1 = new Scanner(file);

int rowA = sc1.nextInt();

int columnA = sc1.nextInt();

UserMatrix obj = new UserMatrix(rowA, columnA);

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

LinkedList A = null, obj1 = null;

for (int j = 0; j < columnA; j++) {

if (j == 0) {

obj1 = new LinkedList(sc1.nextInt());

A = obj1;

} else {

obj1.next = new LinkedList(sc1.nextInt());

obj1 = obj1.next;

}

}

obj.root[i] = A;

}

str1 = "D:\java ecllipse projects\Sample\src\Bfile.txt";

File file1 = new File(str1);

Scanner sc2 = new Scanner(file1);

int rowB = sc2.nextInt();

int columnB = sc2.nextInt();

UserMatrix obj1 = new UserMatrix(rowB, columnB);

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

LinkedList B = null, objA1 = null;

for (int j = 0; j < columnB; j++) {

if (j == 0) {

objA1 = new LinkedList(sc2.nextInt());

B = objA1;

} else {

objA1.next = new LinkedList(sc2.nextInt());

objA1 = objA1.next;

}

}

obj1.root[i] = B;

}

str1 = "D:\java ecllipse projects\Sample\src\Cfile.txt";

File file2 = new File(str1);

Scanner sc3 = new Scanner(file2);

int rowC = sc3.nextInt();

int columnC = sc3.nextInt();

UserMatrix obj2 = new UserMatrix(rowC, columnC);

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

LinkedList C = null, objC1 = null;

for (int j = 0; j < columnC; j++) {

if (j == 0) {

objC1 = new LinkedList(sc3.nextInt());

C = objC1;

} else {

objC1.next = new LinkedList(sc3.nextInt());

objC1 = objC1.next;

}

}

obj2.root[i] = C;

}

Operation op = new Operation();

UserMatrix C = op.add(obj1, obj);

C=op.subtract(obj1, obj);

C=op.transpose(obj);

C=op.multiply(obj1, C);

}

}

/*

A.txt

2 3
1 2 3
4 5 6

first row contains number of rows and column respectively

acc. to example rows=2 column=3

and others are element of matrix.

" file path are not cmd line input you should copy and paste acc. to main function"

*/