Java program help. I need help re-factorying my main class, I cannot include all
ID: 3586087 • Letter: J
Question
Java program help.
I need help re-factorying my main class, I cannot include all of the user interface in one main method, rather call on other methods which perform a specific task. Also I need to control the user input so that the methods are invoked with valid inputs such as a boolean method, this method must be in the matrix class. Thanks.
import java.util.*;
public class Test
{
//Main method
public static void main(String args[])
{
int row1;
int col1;
int row2;
int col2;
Scanner sc= new Scanner(System.in);
//Reading the first matrix
System.out.print(" Input Matrix 1 dimensions (ROWS space COLUMNS): ");
row1 = sc.nextInt();
col1 = sc.nextInt();
//Reading the second matrix
System.out.print(" Input Matrix 2 dimensions (ROWS space COLUMNS): ");
row2 = sc.nextInt();
col2 = sc.nextInt();
//Checking if dimensions match
if (row1 != row2 || col1 != col2)
System.out.print(" Error, Rows or Columns do not match! Please try again ");
else {
int operation;
//Reading the type of operation
System.out.print(" Select the operation to executed: 1. Add 2. Subtract 3. Multiply > ");
operation = sc.nextInt();
Matrix result;
//Matrix class objects
Matrix m1 = new Matrix(row1, col1);
Matrix m2 = new Matrix(row2, col2);
//Calling appropriate function
switch(operation)
{
case 1:
//Adding
result = m1.add(m2);
System.out.println(" First Matrix: " + m1.getPrintableMatrix());
System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
System.out.println(" Result Matrix: " + result.getPrintableMatrix());
break;
case 2:
//Subtracting
result = m1.subtract(m2);
System.out.println(" First Matrix: " + m1.getPrintableMatrix());
System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
System.out.println(" Result Matrix: " + result.getPrintableMatrix());
break;
case 3:
//Multiplying
result = m1.dotProduct(m2);
System.out.println(" First Matrix: " + m1.getPrintableMatrix());
System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
System.out.println(" Result Matrix: " + result.getPrintableMatrix());
break;
default: System.out.println(" Operation is not valid! "); break;
}
}
System.out.print(" ");
}
}
//Matrix class
import java.util.*;
class Matrix
{
//Storing data in row and column
private int row, column;
//Storing data in array
private double[][] matrixElements;
public Matrix(int rows, int columns)
{
//Updating dimensions
this.row = rows;
this.column = columns;
//Allocating memory
matrixElements = new double[row][column];
//Generating array between -100 and 100
populatematrix(-100, 100);
}
public Matrix(double [][] matrixArray)
{
//Updating dimensions
this.row = matrixArray.length;
this.column = (matrixArray[0]).length;
//Allocating memory
matrixElements = new double[row][column];
//Copying array elements
for(int i=0; i {
for(int j=0; j {
matrixElements[i][j] = matrixArray[i][j];
}
}
}
//Method that that generates random numbers
private void populatematrix(int min, int max)
{
Random randnum = new Random();
//Creating random object
Random rand = new Random();
//Generating random value and placing it in array
for (int i=0; i {
for (int j=0; j {
//Updating matrix
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
//Method that adds
public Matrix add(Matrix otherMatrix)
{
//To store result array
double[][] resultMatrixArray = new double[this.row][this.column];
//Adding matrix elements
for (int i=0; i {
for (int j=0; j {
resultMatrixArray[i][j] = this.matrixElements[i][j] + otherMatrix.matrixElements[i][j];
}
}
//Return resultant matrix
return new Matrix(resultMatrixArray);
}
//Method that subtracts
public Matrix subtract(Matrix otherMatrix)
{
//To store result array
double[][] resultMatrixArray = new double[row][column];
//Subtracting matrix elements
for (int i=0; i {
for (int j=0; j {
resultMatrixArray[i][j] = this.matrixElements[i][j] - otherMatrix.matrixElements[i][j];
}
}
//Returning matrix elements
return new Matrix(resultMatrixArray);
}
//Method that performs matrix multiplication
public Matrix dotProduct(Matrix otherMatrix)
{
double[][] resultMatrixArray = new double[row][column];
double sum=0;
//Performing multiplication operation
for ( int c = 0 ; c < this.row ; c++ )
{
for ( int d = 0 ; d < otherMatrix.column ; d++ )
{
for ( int k = 0 ; k < otherMatrix.row ; k++ )
{
sum = sum + ((this.matrixElements[c][k]) * (otherMatrix.matrixElements[k][d]));
}
resultMatrixArray[c][d] = sum;
sum = 0;
}
}
//Returning result array
return new Matrix(resultMatrixArray);
}
//Creating a matrix to a String
public String getPrintableMatrix()
{
String result = "";
for (double[] row : matrixElements)
{
for (double j : row)
{
result += " " + j + " ";
}
result += " ";
}
return result;
}
}
Explanation / Answer
Note could u plz check this and tell me if u need any modifications.so that I will do for sure..thank you
_____________
Matrix.java
import java.util.*;
class Matrix {
// Storing data in row and column
private int row, column;
// Storing data in array
private double[][] matrixElements;
public Matrix(int rows, int columns) {
// Updating dimensions
this.row = rows;
this.column = columns;
// Allocating memory
matrixElements = new double[row][column];
// Generating array between -100 and 100
populatematrix(-100, 100);
}
public static boolean isvalid(int row1, int col1, int row2, int col2) {
// Checking if dimensions match
if (row1 != row2 || col1 != col2)
return false;
else
return true;
}
public Matrix(double[][] matrixArray) {
// Updating dimensions
this.row = matrixArray.length;
this.column = (matrixArray[0]).length;
// Allocating memory
matrixElements = new double[row][column];
// Copying array elements
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matrixElements[i][j] = matrixArray[i][j];
}
}
}
// Method that that generates random numbers
private void populatematrix(int min, int max) {
Random randnum = new Random();
// Creating random object
Random rand = new Random();
// Generating random value and placing it in array
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
// Updating matrix
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
}
}
}
// Method that adds
public Matrix add(Matrix otherMatrix) {
// To store result array
double[][] resultMatrixArray = new double[this.row][this.column];
// Adding matrix elements
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
resultMatrixArray[i][j] = this.matrixElements[i][j]
+ otherMatrix.matrixElements[i][j];
}
}
// Return resultant matrix
return new Matrix(resultMatrixArray);
}
// Method that subtracts
public Matrix subtract(Matrix otherMatrix) {
// To store result array
double[][] resultMatrixArray = new double[row][column];
// Subtracting matrix elements
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
resultMatrixArray[i][j] = this.matrixElements[i][j]
- otherMatrix.matrixElements[i][j];
}
}
// Returning matrix elements
return new Matrix(resultMatrixArray);
}
// Method that performs matrix multiplication
public Matrix dotProduct(Matrix otherMatrix) {
double[][] resultMatrixArray = new double[row][column];
double sum = 0;
// Performing multiplication operation
for (int c = 0; c < this.row; c++) {
for (int d = 0; d < otherMatrix.column; d++) {
for (int k = 0; k < otherMatrix.row; k++) {
sum = sum
+ ((this.matrixElements[c][k]) * (otherMatrix.matrixElements[k][d]));
}
resultMatrixArray[c][d] = sum;
sum = 0;
}
}
// Returning result array
return new Matrix(resultMatrixArray);
}
// Creating a matrix to a String
public String getPrintableMatrix() {
String result = "";
for (double[] row : matrixElements) {
for (double j : row) {
result += " " + j + " ";
}
result += " ";
}
return result;
}
}
__________________
Test.java
import java.util.*;
public class Test {
static int row1;
static int col1;
static int row2;
static int col2;
//Main method
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
if (!getRowsAndCols()) {
System.out.println("** Invalid Inputs **");
} else {
int operation;
//Reading the type of operation
System.out.print(" Select the operation to executed: 1. Add 2. Subtract 3. Multiply > ");
operation = sc.nextInt();
Matrix result;
//Matrix class objects
Matrix m1 = new Matrix(row1, col1);
Matrix m2 = new Matrix(row2, col2);
//Calling appropriate function
switch (operation) {
case 1:
//Adding
result = m1.add(m2);
System.out.println(" First Matrix: " + m1.getPrintableMatrix());
System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
System.out.println(" Result Matrix: " + result.getPrintableMatrix());
break;
case 2:
//Subtracting
result = m1.subtract(m2);
System.out.println(" First Matrix: " + m1.getPrintableMatrix());
System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
System.out.println(" Result Matrix: " + result.getPrintableMatrix());
break;
case 3:
//Multiplying
result = m1.dotProduct(m2);
System.out.println(" First Matrix: " + m1.getPrintableMatrix());
System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
System.out.println(" Result Matrix: " + result.getPrintableMatrix());
break;
default:
System.out.println(" Operation is not valid! ");
break;
}
}
System.out.print(" ");
}
private static boolean getRowsAndCols() {
//Reading the first matrix
System.out.print(" Input Matrix 1 dimensions (ROWS space COLUMNS): ");
row1 = sc.nextInt();
col1 = sc.nextInt();
//Reading the second matrix
System.out.print(" Input Matrix 2 dimensions (ROWS space COLUMNS): ");
row2 = sc.nextInt();
col2 = sc.nextInt();
if (!Matrix.isvalid(row1, col1, row2, col2)) {
return false;
}
return true;
}
}
__________________
Output:
Input Matrix 1 dimensions (ROWS space COLUMNS): 3 3
Input Matrix 2 dimensions (ROWS space COLUMNS): 3 3
Select the operation to executed: 1. Add 2. Subtract 3. Multiply
> 1
First Matrix:
-43.0 -45.0 24.0
-27.0 -80.0 73.0
-83.0 -74.0 -97.0
Second Matrix:
81.0 -8.0 -2.0
85.0 88.0 -100.0
32.0 -35.0 -17.0
Result Matrix:
38.0 -53.0 22.0
58.0 8.0 -27.0
-51.0 -109.0 -114.0
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.