Reputation: 41
Heres my code that displays i get no errors at all and it works but i have a little output issue with my thing
// Lab13TEXT03st.java
// This is the student starting version of the Lab13 assignment.
// Testing <main> methods are provided for the 80-point and 100-point versions.
// This means that this version will not compile as provided.
import java.util.ArrayList;
public class Lab13TEXT03st
{
public static void main(String[] args)
{
System.out.println("\nLAB26A 80-POINT VERSION\n");
Matrix m2 = new Matrix(3, 5, 100);
//m2.displayMatrix("Matrix m2 3 X 5 Display");
System.out.println();
int count = 100;
System.out.println("Matrix m1 Default Display"); //
System.out.println("Matrix has no elements"); //
System.out.println("");
int pos = 0;
for (int r = 0; r < m2.getRows(); r++)
{
//System.out.println("r = " + r); //
for (int c = 0; c < m2.getCols(); c++)
{
m2.setValue(r,c,count, pos);
pos++;
count++;
//System.out.println("r = " + r + " c = " + c + " and count = " + count); //
}
//System.out.println(""); //
}
m2.displayMatrix("Matrix m2 3 x 5 Consecutive Integers Display");
System.out.println();
Matrix m3 = new Matrix(3,3,100);
m3.displayMatrix("Matrix m3 3 X 3 Initialized to 100 Display");
System.out.println();
}
}
class Matrix {
private ArrayList list; // one-dimensional array stores matrix values
private int listSize; // total number of elements in the matrix
private int numRows; // number of rows in the matrix
private int numCols; // number of cols in the matrix
public Matrix(int r, int c, int value){
list = new ArrayList();
numRows = r;
numCols = c;
listSize = r * c;
//for(int i = 0; i < listSize; i++)
// list.add(new Integer(value));
}
public int getRows(){
return numRows;
}
public int getCols(){
return numCols;
}
public int getSize()
{
return listSize;
}
public int getValue(int r, int c)
{
int rowLength = getRows();
int colLength = getCols();
//System.out.println("r in get = " + r);
int position = (r * colLength) + c;
//int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
//System.out.print("Pos get = " + position + "; ");
return ((Integer)list.get(position)).intValue();
}
public void setValue(int r, int c, int value, int pos)
{
int rowLength = getRows();
//int colLength = getCols();
//int position = ((r + 1) * rowLength) - rowLength + c; // one to multi-dimensional array index conversion
//System.out.println("Pos set = " + position + "; ");
list.add(pos,new Integer(value));
}
public void displayMatrix(String str)
{
System.out.println(str);
for(int j = 0; j < getRows(); j++){
for(int i = 0; i < getCols(); i++){
System.out.print(getValue(j, i) + " ");
}
System.out.println("");
}
}
}
My output is wrong and i need some help please
I get no errors... here is my output
LAB26A 80-POINT VERSION
Matrix m1 Default Display
Matrix has no elements
Matrix m2 3 x 5 Consecutive Integers Display
100 101 102 103 104
105 106 107 108 109
110 111 112 113 114
Matrix m3 3 X 3 Initialized to 100 Display
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Matrix.getValue(Lab13TEXT03st.java:128)
at Matrix.displayMatrix(Lab13TEXT03st.java:147)
at Lab13TEXT03st.main(Lab13TEXT03st.java:49)
Process completed.
It needs to output like this though:
LAB13TEXT03 80-POINT VERSION
Matrix m1 Default Display
Matrix has no elements
Matrix m2 3 X 5 Display
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Matrix m2 3 X 5 Consecutive Integers Display
100 101 102 103 104
105 106 107 108 109
110 111 112 113 114
Matrix m3 3 X 3 Initialized to 100 Display
100 100 100
100 100 100
100 100 100
Please help
Upvotes: 1
Views: 486
Reputation: 235994
The second matrix (m3
) fails because you haven't initialized it yet, as you did with m2
. You never wrote something along the lines m3.setValue(...)
in the code. Alternatively, for initializing the matrix with a default value, you need to uncomment these lines in the constructor of Matrix
:
for(int i = 0; i < listSize; i++)
list.add(new Integer(value));
Upvotes: 2