Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Dynamic ArrayList "Matrix"

$
0
0
I'm working on a program to create an Matrix class that handles two-dimensional array-needs "dynamically, but uses Java one-dimensional ArrayList class to implement the Matrix class.
I have started the program, but Im a little stuck on the method(s): public Matrix(3 parameters), getValue, and displayMatrix.
For getValue, I've tried the same equation for setValue, but it doesn't work and I need ideas on how to revise the public Matrix method and displayMatrix. Thanks!
import java.util.ArrayList;
public class TextLab08 {

	   public static void main(String args[])
		{
			System.out.println("\nTextLab08 STUDENT VERSION\n");
			
			Matrix m1 = new Matrix();
			m1.displayMatrix("Matrix m1 Default Display");
			System.out.println();
		
			Matrix m2 = new Matrix(3,5);
			m2.displayMatrix("Matrix m2 3 X 5 Display");
			System.out.println();
			int count = 100;
			for (int r = 0; r < m2.getRows(); r++)
			{
				for (int c = 0; c < m2.getCols(); c++)
				{
					m2.setValue(r,c,count);
					count++;
				}
			}		
			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()
		{
			ArrayList<Integer> list = new ArrayList<Integer>();
			int r=0;
			int c=0;
			
		}
		public Matrix (int r, int c)
		{
			
			numRows=r;
			numCols=c;
			listSize= numRows*numCols;
			
		}
		public Matrix (int r, int c, int value)
		{
			for(r=0;r<list.size();r++)
			{
				r=numRows;
			}
			for(c=0;c<list.size();c++)
			{
				c=numCols;
			}
			listSize = numRows*numCols;
			
			
		}
		public int getRows()
		{
			return numRows;
		}
		public int getCols()
		{
			return numCols;
		}
		public int getSize()
		{
			return listSize;
		}
		public int getValue(int r,int c)
		{
			
		}
		public void setValue(int r,int c, int value)
		{
			list.set((numCols* r)+ c, value);
		}
		public void displayMatrix(String str)
		{
			
		}
		
		
		
		
		
	}

	



This is how the output should look:
TextLab08 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

Viewing all articles
Browse latest Browse all 51036

Trending Articles