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

Need Some Help Debugging This Code public class Vect { private int MAXSIZE = 99;

ID: 3666727 • Letter: N

Question

Need Some Help Debugging This Code

public class Vect
{
private 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

----------------------------------------------------------------------------------------------------------------------------

public class Matrix
{
private 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)
{
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 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

Explanation / Answer

Vect.java:

public class Vect
{
private 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)
{
this.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

Matrix.java:

public class Matrix
{
private 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 < r; i++)
{
// initialize matrix row
m[i] = new Vect(c);
}
}

// create array with dimensions rxc
// 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(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("");
}
}

Main.java:

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

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