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

Debugging Java Can you please help me debug this program? ###MAIN### // for debu

ID: 3689427 • Letter: D

Question

Debugging Java

Can you please help me debug this program?

###MAIN###

// for debugging, here's an online calculator:
// http://www.bluebit.gr/matrix-calculator/matrix_multiplication.aspx

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

###MATRIX###

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("");
}
}

###VECTOR###

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

The code is compiling correctly. First, compile Vect class. Next, the Matrix class. Finally, the Main class. However, during execution, it gives the NullPointerException at the code:

m[i].setValue(i, args[i*c + j]);

in the Matrix class.

The reason is you are not creating the array with dimensions r and c before trying to set the values. Modified code:

public Matrix(int r, int c, float[] args)

   {

     rows = r;

     columns = c;

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

     {

         m[i]=new Vect(r);

      // loop through row, setting column values

       for (int j = 0; j < c; j++)

       {

         //m[i]=new Vect(c);

         m[i].setValue(i, args[i*c + j]);

       }

     }

   }

Next, it gives a java.lang.ArrayIndexOutOfBoundsException: 2
   at Cryptography.Vect.setValue(Vect.java:41)
   at Cryptography.Matrix.<init>(Matrix.java:44)

In the Vect class the corresponding line of code is:

public void setValue(int i, float f)
{
v[i] = f;
}

The calling code in Matrix class is :

m[i].setValue(i, args[i*c + j]);

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