Problem: Write a class called DoubleMatrix in which you declare a 2-dim. array o
ID: 3666183 • Letter: P
Question
Problem: Write a class called DoubleMatrix in which you declare a 2-dim. array of doubles (I'm calling it doubMatrix) as a private instance variable. Include the following constructors or instance methods (NO static METHODS in the DoubleMatrix class):
constructor with an int for the first dimension size, an int for the second dimension size, and a double for the upperLimit value that will call the makeDoubMatrix private instance method (see below) Be sure to check if each parameter > 0 (if not, the best way to handle for now is to change the out-of-range parameter to 1)
another constructor with a 2-dim. array of doubles as its parameter. Before you assign to the instance doubMatrix variable, check if parameter isn't null, if the length >0, AND if each row has the same length as row 0 (you'll need a loop to check this), otherwise, assign a new 1 X 1 2-dim. array to the array instance variable.
public method to return the size of the first dimension (how many "rows") called getDim1Size (NOT an instance variable in this class, but use the length of doubMatrix!)
public method to return the size of the second dimension (how many "columns" in one row) called getDim2Size (NOT an instance variable in this class, but use the length of doubMatrix[0]!)
private method (makeDoubMatrix) that has the same parameters and does the same thing as getDoubMatrix in Prog. HW #2 (also uses the appropriate MyRandom method), but assigns the new 2-dim. array to the instance doubMatrix variable and has NO RETURN VALUE.
*public method (I'm calling it addMatrix) that has ONLY ONE PARAMETER for a DoubleMatrix to add this doubMatrix (not changing this doubMatrix) and the parameter's doubMatrix and return a new DoubleMatrix that contains the result of adding. Make sure you check if the dimensions of this doubMatrix and the parameter's doubMatrix are the same (if not, return a new DoubleMatrix calling the first constructor passing 1, 1).
optional method: return one element of this' doubMatrix, passing row & column indices (make sure the row & column are in the array bounds)
*public method (I'm calling it getTransposedMatrix, NO PARAMETERS) that creates the transposition of this doubMatrix (not changing this doubMatrix), and returns the result in a new DoubleMatrix object
*public method (I'm calling it multiplyMatrix) that has ONLY ONE PARAMETER for a DoubleMatrix to multiply this doubMatrix (left operand) by the parameter's doubMatrix and return a new DoubleMatrix. Make sure you check if the 2nd dimension of this doubMatrix is the same as the parameter's doubMatrix's 1st dimension (if not, return a new DoubleMatrix calling the first constructor passing 1, 1).
public method (I'm calling it printMatrix, one String parameter, no other parameters) that displays the String parameter, then the doubMatrix to the screen
*You MUST call the 2nd constructor at least ONCE in this class (passing a 2-dim. array). The methods with this * indicate those would be good for this. Points off if you don't call the 2nd constructor at all (and call it correctly)!
NOTES: The ONLY method in the DoubleMatrix class that has a 2-dim. array parameter will be the 2nd constructor (NOT IN ANY OTHER METHOD). The ONLY method in the DoubleMatrix class with 2 int parameters are the first constructor AND the makeDoubMatrix method!!!
Write another class for main, call it Program3 that is in a separate file and class than DoubleMatrix. Declare in main 3 DoubleMatrix object variables (I'm calling them doubMatObj1, doubMatObj2, and doubMatObj3), and CHANGE main from Prog. HW#2 to do the following, calling ONLY DoubleMatrix instance methods (except to get the 2 random dimensions) IN THIS EXACT ORDER:
Get the first and second dimensions the same way as in Prog. HW#2 (using the Math class' random method)
Instantiate a DoubleMatrix passing the first and second dimensions you just assigned and assign it to doubMatObj1
Instantiate a DoubleMatrix passing the return value of doubMatObj1's getDim1 and getDim2 and assign it to doubMatObj2
Call addMatrix on doubMatObj1 passing doubMatObj2, assigning the return value to doubMatObj3 Call printMatrix on doubMatObj1 passing "First Matrix Object"
Call printMatrix on doubMatObj2 passing "Second Matrix Object"
Call printMatrix on doubMatObj3 passing "Result of Adding Matrix Objects"
Call getTransposedMatrix on doubMatObj2, and assign return value back to doubMatObj2
Call printMatrix on doubMatObj2 passing "Result of Transposing Matrix Object"
Call multiplyMatrix on doubMatObj1 passing doubMatObj2, assigning the return value to doubMatObj3
Call printMatrix on doubMatObj3 passing "Result of Multiplying Matrix Objects"
============================here is the HW2============================
import java.text.*;
public class Assignment2 {
public static void main(String[] args) {
double doubMatrix1[][];
double doubMatrix2[][];
double doubMatrix3[][];
int min = 3, max = 10;//random numbers between 3 and 10
int a = (int) (Math.random() * (max - min + 1)) + min;// get pseudo-randomly generated integers between MIN and MAX
int b = (int) (Math.random() * (max - min + 1)) + min;
doubMatrix1 = getDoubMatrix(a, b);
doubMatrix2 = getDoubMatrix(a, b);
System.out.println("First Matrix:");//print all the matrix and result
printDoubMatrix(doubMatrix1);
System.out.println("Second Matrix:");
printDoubMatrix(doubMatrix2);
doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2);
System.out.println("Result of Adding :");
printDoubMatrix(doubMatrix3);
double doubMatrix4[][] = transposeMatrix(doubMatrix2);
System.out.println("Second Matrix Transposed :");
printDoubMatrix(doubMatrix4);
double doubMatrix5[][] = multiplyMatrices(doubMatrix1, doubMatrix2);
System.out.println("Result of Multiplying : ");
printDoubMatrix(doubMatrix5);
}
public static double[][] getDoubMatrix(int a, int b) { //get 2 random Matrix
double array[][] = new double[a][b];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = (Math.random() * (100.0));
}
}
return array;
}
public static double[][] addMatrices(double array1[][], double array2[][]) {
double result[][];
if(array1.length != array2.length){//check the size, not same retuen 0x0
result = new double[0][0];
return result;
}
else{
result = new double[array1.length][array1[0].length];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
result[i][j] = array1[i][j] + array2[i][j];
}
}
return result;
}
}
public static void printDoubMatrix(double array1[][]) { // print Matrix
DecimalFormat decFor = new DecimalFormat("0.0");
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
System.out.print(String.format("%7s", decFor.format(array1[i][j]))+" ");
}
System.out.println(" ");
}
}
public static double[][] transposeMatrix(double array2[][]) { // to get the transpose of Matrix2
double result[][];
result = new double[array2[0].length][array2.length];
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
result[j][i] = array2[i][j];
}
}
return result;
}
public static double[][] multiplyMatrices(double[][] doubMat1, double[][] doubMat2) {
if (doubMat1[0].length != doubMat2.length) {
return new double[0][0];//check the size ,if not same return 0x0
}
double[][] result = new double[doubMat1.length][doubMat2[0].length];//if the size is same,doing the multiply
for (int i = 0; i < doubMat1.length; i++) {
for (int j = 0; j < doubMat2[0].length; j++) {
for (int k = 0; k < doubMat1[0].length; k++) {
result[i][j] += (doubMat1[i][k] * doubMat2[k][j]);
}
}
}
return result;
}
}
Explanation / Answer
//program3.java
package assignment2;
public class program3 {
public static void main(String[] args) {
int min = 3, max = 10;//random numbers between 3 and 10
int a = (int) (Math.random() * (max - min + 1)) + min;// get pseudo-randomly generated integers between MIN and MAX
int b = (int) (Math.random() * (max - min + 1)) + min;
try{
DoubleMatrix doubMatObj1=new DoubleMatrix(a,b,100.0);
DoubleMatrix doubMatObj2=new DoubleMatrix(doubMatObj1.getDim1Size(),doubMatObj1.getDim1Size(),100.0);
doubMatObj1.printMatrix("First Matrix Object");
doubMatObj2.printMatrix("Second Matrix Object");
DoubleMatrix doubMatObj3=doubMatObj1.addMatrix(doubMatObj2);
doubMatObj3.printMatrix("Result of Adding Matrix Objects");
doubMatObj2= doubMatObj2.getTransposedMatrix();
doubMatObj2.printMatrix("Result of Transposing Matrix Object");
doubMatObj3= doubMatObj1.multiplyMatrices(doubMatObj2);
doubMatObj3.printMatrix("Result of Multiplying Matrix Objects");
}
catch(Exception e)
{
}
}
}
/////////////////////////////////////////////////////////////////////
//DoubleMatrix.java
package assignment2;
import java.text.DecimalFormat;
public class DoubleMatrix {
private double doubMatrix[][];
/*
constructor with an int for the first dimension size, an int for the second dimension size,
and a double for the upperLimit value that will call the makeDoubMatrix private
instance method (see below) Be sure to check if each parameter > 0
(if not, the best way to handle for now is to change the out-of-range parameter to 1)
*/
public DoubleMatrix(int size1, int size2, double upperLimit) throws Exception
{
if(size1>0 && size2 >0 && upperLimit>0)
makeDoubMatrix(size1,size2,upperLimit) ;
else
throw new Exception("OutOfRangeException");
}
/*
another constructor with a 2-dim. array of doubles as its parameter.
Before you assign to the instance doubMatrix variable, check if parameter isn't null,
if the length >0, AND if each row has the same length as row 0 (you'll need a loop to check this),
otherwise, assign a new 1 X 1 2-dim. array to the array instance variable.
*/
public DoubleMatrix(double [][] array)
{
if(array.length>0)
{
doubMatrix=array;
}
else
{
makeDoubMatrix(1,1,100.0) ;
}
}
/*
public method to return the size of the first dimension (how many "rows")
called getDim1Size (NOT an instance variable in this class, but use the length of doubMatrix!)
*/
public int getDim1Size()
{
return doubMatrix.length;
}
/*
public method to return the size of the second dimension (how many "columns" in one row)
called getDim2Size (NOT an instance variable in this class, but use the length of doubMatrix[0]!)
*/
public int getDim2Size()
{
return doubMatrix[0].length;
}
private void makeDoubMatrix(int size1, int size2, double upperLimit)
{
doubMatrix= new double[size1][size2];
for (int i = 0; i < doubMatrix.length; i++) {
for (int j = 0; j < doubMatrix[i].length; j++) {
doubMatrix[i][j] = (Math.random() * (upperLimit));
}
}
}
/*
*public method (I'm calling it addMatrix) that has ONLY ONE PARAMETER
for a DoubleMatrix to add this doubMatrix (not changing this doubMatrix)
and the parameter's doubMatrix and return a new DoubleMatrix that contains
the result of adding. Make sure you check if the dimensions of this doubMatrix
and the parameter's doubMatrix are the same (if not, return a new DoubleMatrix
calling the first constructor passing 1, 1).
*/
public DoubleMatrix addMatrix(DoubleMatrix DMatrix) throws Exception {
DoubleMatrix result;
if(doubMatrix.length != DMatrix.getDim1Size()){//check the size, not same retuen 0x0
result = new DoubleMatrix(1,1,100.0);
return result;
}
else{
result = new DoubleMatrix(doubMatrix.length,doubMatrix[0].length,100.0);
for (int i = 0; i < doubMatrix.length; i++) {
for (int j = 0; j < doubMatrix[i].length; j++) {
result.doubMatrix[i][j] = doubMatrix[i][j] +DMatrix.doubMatrix[i][j];
}
}
return result;
}
}
/*
*public method (I'm calling it getTransposedMatrix, NO PARAMETERS) that creates
the transposition of this doubMatrix (not changing this doubMatrix), and returns
the result in a new DoubleMatrix object
*/
public DoubleMatrix getTransposedMatrix() throws Exception {
DoubleMatrix result;
result = new DoubleMatrix(doubMatrix[0].length,doubMatrix.length,100.0);
for (int i = 0; i < doubMatrix.length; i++) {
for (int j = 0; j < doubMatrix[i].length; j++) {
result.doubMatrix[j][i] = doubMatrix[i][j];
}
}
return result;
}
/*
*public method (I'm calling it multiplyMatrix) that has ONLY ONE PARAMETER for a
DoubleMatrix to multiply this doubMatrix (left operand) by the parameter's doubMatrix
and return a new DoubleMatrix. Make sure you check if the 2nd dimension of this doubMatrix
is the same as the parameter's doubMatrix's 1st dimension (if not, return a new DoubleMatrix
calling the first constructor passing 1, 1).
*/
public DoubleMatrix multiplyMatrices(DoubleMatrix DMatrix) throws Exception {
if (doubMatrix[0].length != DMatrix.getDim1Size()) {
return new DoubleMatrix(1,1,100.0);//check the size ,if not same return 0x0
}
DoubleMatrix result = new DoubleMatrix(doubMatrix.length,DMatrix.getDim2Size(),100.0);//if the size is same,doing the multiply
for (int i = 0; i < doubMatrix.length; i++) {
for (int j = 0; j < DMatrix.getDim2Size(); j++) {
for (int k = 0; k < doubMatrix[0].length; k++) {
result.doubMatrix[i][j] += (doubMatrix[i][k] * DMatrix.doubMatrix[k][j]);
}
}
}
return result;
}
/*
public method (I'm calling it printMatrix, one String parameter, no other parameters)
that displays the String parameter, then the doubMatrix to the screen
*You MUST call the 2nd constructor at least ONCE in this class (passing a 2-dim. array).
The methods with this * indicate those would be good for this. Points off if you don't call
the 2nd constructor at all (and call it correctly)!
*/
public void printMatrix(String msg) { // print Matrix
System.out.println(msg);
DecimalFormat decFor = new DecimalFormat("0.0");
for (int i = 0; i < doubMatrix.length; i++) {
for (int j = 0; j < doubMatrix[i].length; j++) {
System.out.print(String.format("%7s", decFor.format(doubMatrix[i][j]))+" ");
}
System.out.println(" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.