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

Need help with debugging. Also there is a bug where it requires Matrix and Vect

ID: 3668010 • Letter: N

Question

Need help with debugging. Also there is a bug where it requires Matrix and Vect to have variables changed to this.variable name.

Could you do that also? Thanks

public class Main {

public static void main(String[] args)
{
    // matrix multiplication requires that matrix1 to have the same number of rows as matrix2 has columns and vice versa
    int a = 3;
    int b = 2;
    float[] m1 = new float[]{1, 2, 3, 4, 5, 6};
    float[] m2 = new float[]{7, 8, 9, 10, 11, 12};

    // create test matrices
    Matrix matrix1 = new Matrix(a, b, m1);
    matrix1.printMatrix();
    Matrix matrix2 = new Matrix(b, a, m2);
    matrix1.printMatrix();
    Matrix matrix3 = new Matrix(a, a);
  
    // print matrices & their dimensions
    matrix1.printMatrix();
    matrix2.printMatrix();
  
    // multiply matrices
    matrix3 = matrix1.matrixMult(matrix2);
  
    // print matrix 3
    matrix3.printMatrix();
  
}

} // end class Main

public class Matrix {
private static int MAXSIZE = 99;
private Vect[] m = new Vect[99];
private int rows;
private int columns;

// create default array of size 1
public Matrix()
{
    m[0] = new Vect();
    rows = 1;
    columns = 1;
}

// create an empty rxc matrix
public Matrix(int r, int c)
{
    rows = r;
    columns = c;
    int j = 0;
    for (int i = 0; i < c; i++)
    {
      // initialize matrix row
      m[i] = new Vect(c);
    }
}

// create array with dimensions r c
// use r and c to parse args into an rxc matrix
public Matrix(int r, int c, float[] args)
{
    rows = r;
    columns = c;

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

      // loop through row, setting column values
      for (int j = 0; j < c; j++)
      {
        m[i].setValue(i, args[i*c + j]);
      }
    }
}

// returns number of rows in Matrix
public int getRows()
{
    return(rows);
}
  
// returns number of Columns in Matrix
public int getColumns()
{
    return(columns);
}

// sets value at (r, c) to value
public void setValue(int r, int c, float value)
{
    m[r].setValue(c, value);
}

// returns row i as a Vect
public Vect getRow(int r)
{
    return(m[r]);
}

// returns column i as Vect
public Vect getColumn(int c)
{
    Vect columnVect = new Vect(rows);
    for (int i = 0; i < rows; i++)
      columnVect.setValue(i, m[i].getVect(c));
    return(columnVect);
}

// returns product of Matrix multiplication
public Matrix matrixMult(Matrix m2)
{
    Matrix product = new Matrix(rows, rows);
    for (int i = 0; i < rows; i++)
    {
      for (int j = 0; j < columns; j++)
      {
        product.setValue(j, i, m[i].dotProduct(m2.getColumn(j)));
      }
    }
    return(product);
}

// print matrix
public void printMatrix()
{
    for (int i = 0; i < rows; i++)
      m[i].printVect();
    System.out.println("");
}
}

public class Vect {
private static int MAXSIZE = 99;
private float[] v = new float[MAXSIZE];
private int size;

// if no arguments, create a vector of length 1
public Vect()
{
    size = 1;
    this.v = new float[1];
    v[0] = 0;
}

// create a vector of length size
public Vect(int length)
{
    size = length;
    this.v = new float[size];
    for (int i = 0; i < size; i++)
    {    
      v[i] = 0;
    }
}

// make a Vect object from a vector of floats
public Vect(float[] args)
{
    size = args.length;
    this.v = new float[args.length];
    for (int i = 0; i < args.length; i++)
    {
      v[i] = args[i];
    }
}

// sets value at index i to f
public void setValue(int i, float f)
{
    v[i] = f;
}

// return value of index i
public float getVect(int i)
{
    return(v[i]);
}

// sets value at index i to a random number (to generate test data)
public void randVect(int i)
{
    setValue(i, (int)(Math.random() * 10));
}

// prints a Vect
public void printVect()
{
    System.out.print("{ ");
    for (int i = 0; i < v.length; i++)
      System.out.print("" + v[i] + " ");
    System.out.println("}");
}

// returns size of Vect
public int getSize()
{
    return(size);
}

// takes dot product of Vect and v2
public float dotProduct(Vect v2)
{
    // initialize f
    int f = 0;
  
    // dot product requires vectors of equal length
    if (this.getSize() == v2.getSize())
    {
      // dot product of 2 vectors is v[0]*v2[0] + v[1]*v2[1] + ... v[size]*v2[size]
      for (int i = 0; i < this.getSize()-1; i++)
      {
        f += v[i] * v2.getVect(i);
      }
    }
    else
    {
      System.out.println("Dot Product requires vectors of equal length.");
    }
    return(f);
}

} // end class vector

Explanation / Answer

/* changes done in Matrix.java only, to run the program */

//Matrix.java


package main;

public class Matrix {
private static int MAXSIZE = 99;
private Vect[] m = new Vect[99];
private int rows;
private int columns;

// create default array of size 1
public Matrix()
{
m[0] = new Vect();
rows = 1;
columns = 1;
}

// create an empty rxc matrix
public Matrix(int r, int c)
{
rows = r;
columns = c;
int j = 0;
for (int i = 0; i < c; i++)
{
// initialize matrix row
m[i] = new Vect(c);
}
}

// create array with dimensions r c
// use r and c to parse args into an rxc matrix
public Matrix(int r, int c, float[] args)
{
rows = r;
columns = c;
for (int i = 0; i < r; i++)
{
// loop through row, setting column values
m[i] = new Vect(c);
for (int j = 0; j < c; j++)
{
m[i].setValue(j,args[i*c + j]);
}
}
}

// returns number of rows in Matrix
public int getRows()
{
return(rows);
}
  
// returns number of Columns in Matrix
public int getColumns()
{
return(columns);
}

// sets value at (r, c) to value
public void setValue(int r, int c, float value)
{
m[r].setValue(c, value);
}

// returns row i as a Vect
public Vect getRow(int r)
{
return(m[r]);
}

// returns column i as Vect
public Vect getColumn(int c)
{
Vect columnVect = new Vect(rows);
for (int i = 0; i < rows; i++)
columnVect.setValue(i, m[i].getVect(c));
return(columnVect);
}

// returns product of Matrix multiplication
public Matrix matrixMult(Matrix m2)
{
Matrix product = new Matrix(rows, rows);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
product.setValue(j, i, m[i].dotProduct(m2.getColumn(j)));
}
}
return(product);
}

// print matrix
public void printMatrix()
{
for (int i = 0; i < rows; i++)
m[i].printVect();
System.out.println("");
}
}

//output

/*

run:
{ 1.0 2.0 }
{ 3.0 4.0 }
{ 5.0 6.0 }

{ 1.0 2.0 }
{ 3.0 4.0 }
{ 5.0 6.0 }

{ 1.0 2.0 }
{ 3.0 4.0 }
{ 5.0 6.0 }

{ 7.0 8.0 9.0 }
{ 10.0 11.0 12.0 }

{ 7.0 21.0 35.0 }
{ 8.0 24.0 40.0 }
{ 0.0 0.0 0.0 }

BUILD SUCCESSFUL (total time: 0 seconds)

*/

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