Question: I am trying to form a matrix class in java that perform addition, subtraction, dot product, and hadamard product. The program cannot use any loops.
I am trying to form a matrix class in java that perform addition, subtraction, dot product, and hadamard product. The program cannot use any loops. I need to print options for choosing the methods in the main class. I've wrote the code for the program but it still doesn't work. Please help me with the main method and debugging the code. Also I need to write method for hadamard product and method to convert the matrix into string which should return and object of type string and override the inherited toString method from the Object class. The code I wrote doesn't work. Could you please check the errors and provide a separate main class to run the program.
package com.company; import java.util.Arrays; import java.util.Random; import java.util.stream.IntStream; import java.util.Map; import java.util.Scanner; public class Matrix { private int rows; private int columns; private int [] elements; Scanner scan= new Scanner(System.in); int printOptions() { System.out.println("Select an option"); System.out.println("1:Add 2.Sub 3:Dot product 4: Product"); int result = scan.nextInt(); Matrix A= Matrix.getMatrix("first"); System.out.println(A.toString()); Matrix B= Matrix.getMatrix("Second"); System.out.println(B.toString()); Matrix C= Matrix.Calculate (A,B); System.out.println(result); System.out.println(C.toString()); return result; } public void main (){ Scanner scan =new Scanner(System.in); Matrix A=Matrix.getMatrix ("first"); System.out.println(A.toString()); Matrix B= Matrix.getMatrix ("Second"); System.out.println(B.toString()); Matrix C=Matrix.Calculate (A,B); System.out.println("result"); System.out.println(C.toString()); } public Matrix(){ this (2,2); } public Matrix (int rows,int columns){ this.rows=rows; this.columns=columns; this.elements = IntStream.range(0,rows.mapToObject(x->new Random().ints().limit(columns).toArray()).toArray(int[][]:: new)); } public Matrix (int [][] matrixArray){ this.rows=matrixArray.length; this.columns=matrixArray.length; this.elements= Arrays.stream(matrixArray).mapToObj(x->Arrays.clone(x).toArray(int [][]::new)); } public Matrix add (Matrix otherMatrix){ return new Matrix (IntStream.range(0,rows).mapToObj(x->IntStream.range(0,columns).map(y->this.elements[x][y]+ otherMatrix.elements[x][y]).toArray()).toArray(int[][]::new)); } public Matrix subtract (Matrix otherMatrix){ return new Matrix (IntStream.range(0,this.rows).mapToObj(x->IntStream.range(0,this.columns).map(y->(this.elements[x][y]- elements[x][y]).toArray()).toArray(int[][]::new))); } public Matrix dotProduct (Matrix otherMatrix){ return new Matrix (IntStream.range (0, this.rows).mapToObj(i->IntStream(0,(otherMatrix.columns).mapToInt()->(j->IntStream(0,(this.columns).map(k->this.elements [i][k] * otherMatrix.elements[k][j]).reduce (0,(a,b)->a+b).toArray()).toArray (int[][]::new)); } public Matrix HadamardProduct(Matrix otherMatrix){ } static boolean validateSubAddHad (Matrix A, Matrix B) { return (A.rows==B.rows && A.columns==B.columns); } static boolean validateDot(Matrix A, Matrix B) { return (A.columns == B.rows); } Matrix Calculate (Matrix A, Matrix B){ boolean isValid=false; do{ int selection =Matrix.doOperation(); if(selection==1||selection==2||selection==4){ isValid=Matrix.validateSubAddHad(A,B); } else if (selection==3){ isValid=Matrix.validateDot(A,B); } else isValid=false; }while (!isValid); return new Matrix (A,B); } Matrix Calculate (Matrix A, Matrix B){ boolean isValid=false; do { int selection = DoOperations(); if (!isValid) { Matrix A = Matrix.getMatrix("First"); System.out.println(B.toString()); } return Matrix.performOperation (A,B, selection); }while (!isValid); } public Matrix getMatrix (String name){ System.out.println ("give the parameters for the" +name + "matrix"); System.out.println("Enter the rows:"); int rows= scan.nextInt(); System.out.println (); System.out.println ( "Enter the number of columns" ); int columns = scan.nextInt (); return new Matrix (rows,columns); } static Matrix performOperation(Matrix A,Matrix B, int selection){ switch (selection) { case 1: return A.add(B); break; case 2: return A.subtract(B); break; case 3: return A.dotProduct(B); break; default: return A.HadamardProduct(B); } } @Override public String toString(){ return name; } @Override public boolean equals (Object obj){ if(obj instanceof Matrix){ Matrix otherMatrix =(Matrix) obj; return ArrayDeep.equals (this.elements, otherMatrix.elements); } else return false; } @Override public Object Clone(){ return (Object) new Matrix (this.elements); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
