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

Java Matrices: Write a program that reads in two matrices from two different tex

ID: 3746365 • Letter: J

Question

Java Matrices:

Write a program that reads in two matrices from two different text files. Should use a singly or double linked list to store the two matrices and perform the following operations on them:

- Add, Mulitply, and Determinant

Requirements:

Use singly or doubly linked list date structures only:

- No other library methods or existing collection framework.

- No use of Array, ArrayList, List, or Vectors for storing.

- No two-dimensional arrays.

- Input files can only be read Exactly Once for all operations.

For the determinant operation, you may augment your linked list node to retain row/column id and employ recursion to directly implement the standard method for computing determinant of a matrix. You are encouraged to design your own node representation (e.g., each node element has two pointers: one 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)

Inputs:

The program takes 3 or 4 arguments as input. Assuming input matrix are in space deliminated files a.txt and b.txt, an few examples of format operation query will be:

- Addition: add   a.txt   b.txt   output.txt

- Multiplication: mul a.txt   b.txt   output.txt

- Determinant: det   a.txt   output.txt

The input file can contains only integer (e.g. -3). However your output should be in floating point format with 1 decimal place (e.g. -3.0).

Example Input:

Input A

11 17 20 13 1
8 6 7 20 1
10 18 15 10 6
12 11 3 10 6
7 20 8 8 11

Input B

12 3 5 19 13
11 4 15 6 17
14 8 17 11 18
4 4 16 12 19
5 12 12 14 1

Explanation / Answer

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.LineNumberReader;

import java.io.PrintWriter;

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 countLines(String filename) throws IOException

{

LineNumberReader reader = new LineNumberReader(new FileReader(filename));

int cnt = 0;

String lineRead = "";

while ((lineRead = reader.readLine()) != null) {}

cnt = reader.getLineNumber();

reader.close();

return cnt;

}

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 "+ A.getColumn()+" "+B.getRow());

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 Solution {

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

String str1 ,str2,str3;

str2="";

int operation = -1;

if (args.length <= 0)

{

System.out.println("No cmd arguments");

return;

}

else {

if (args[0].equalsIgnoreCase("add"))

operation = 0;

else if (args[0].equalsIgnoreCase("sub"))

operation = 1;

else if (args[0].equalsIgnoreCase("mul"))

operation = 2;

else if (args[0].equalsIgnoreCase("Tra"))

operation = 3;

}

if(operation==3)

{

str1=args[1];

str3=args[2];

}

else

{

str1=args[1];

str2=args[2];

str3=args[3];

}

File file = new File(str1);

Scanner sc1 = new Scanner(file);

Operation op = new Operation();

int rowA = op.countLines(str1);

UserMatrix obj=null ;

BufferedReader br = new BufferedReader(new FileReader(new File(str1)));

String line;

int j0=0;

int temp=0;

while((line = br.readLine())!= null){

// split the line you read after the

String [] r = line.split(" ");

if(temp==0)

{

  

obj = new UserMatrix(rowA, r.length);

  

}

  

// iterate through these values

LinkedList A = null, obj1 = null;

for(int i = 0; i < r.length; i++)

{

int val = Integer.parseInt(r[i]);

if (i == 0) {

obj1 = new LinkedList(val);

A = obj1;

} else {

obj1.next = new LinkedList(val);

obj1 = obj1.next;

}

}

obj.root[j0] = A;

j0++;

temp++;

}

br.close();

UserMatrix obj1 = null;

if(operation!=3)

{

File file1 = new File(str2);

Scanner sc2 = new Scanner(file1);

int rowB =op.countLines(str2);

int temp1=0;

BufferedReader br1 = new BufferedReader(new FileReader(new File(str2)));

String line1;

int j1=0;

while((line1 = br1.readLine())!= null)

{

// split the line you read after the

String [] r1 = line1.split(" ");

if(temp1==0)

{

obj1 = new UserMatrix(rowB, r1.length);

}

LinkedList B = null, objA1 = null;

for(int i = 0; i < r1.length; i++)

{

int val = Integer.parseInt(r1[i]);

if (i == 0) {

objA1 = new LinkedList(val);

B = objA1;

} else {

objA1.next = new LinkedList(val);

objA1 = objA1.next;

}

}

obj1.root[j1] = B;

j1++;

temp1++;

}

br1.close();

}

UserMatrix C = null ;

switch(operation)

{

case 0: C= op.add(obj1, obj);

break;

case 1: C=op.subtract(obj1, obj);

break;

case 3:C=op.transpose(obj);

break;

case 2:C=op.multiply(obj1, obj);

break;

}

PrintWriter writer = new PrintWriter(str3, "UTF-8");

for (int i = 0; i < C.getRow(); i++)

{

LinkedList objout = C.root[i];

for (int j = 0; j < C.getColumn() && objout != null; j++)

{

float valueofelement=(float)objout.data;

writer.print(valueofelement);

writer.print(" ");

objout = objout.next;

}

writer.println("");

}

writer.close();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote