Posted by: Slackdemic October 20, 2006
Java Programming
Login in to Rate this Post:     0       ?        
I have been trying to do the following Matrix problem: DESCRIPTION Your assignment is to write a class for a Matrix Object and a Class that will test the Matrix Class. Using the Matrix class a user should be able to add and subtract two matrix objects. The user should also be able to do scalar multiplication on one matrix and determine if the matrix is a magic square. MATRIX CLASS DESCRIPTION The Matrix Class should construct a matrix of size n. The constructor should initialize all elements to 0. The constructor should populate the matrix with digits from 1 to n2 in the matrix at random locations based on a Boolean parameter. Digits may not be repeated. Methods should include add, subtract, scalarMultiply and isMagic. Also include a toString method to print the matrix object. MATRIX TEST CLASS DESCRIPTION The matrix test class will need to test all operations of the matrix class. Output should demonstrate that operations were successful. Example using matrix of size 3 10 9 7 2 8 1 8 1 6 12 8 13 = 9 3 6 + 3 5 7 8 16 7 4 7 5 4 9 2 -6 7 -5 2 8 1 8 1 6 6 -2 -1 = 9 3 6 - 3 5 7 0 -2 3 4 7 5 4 9 2 8 32 4 2 8 1 36 12 24 = 9 3 6 * 4 16 28 20 4 7 5 2 8 1 9 3 6 Is not a magic square 4 7 5 8 1 6 3 5 7 Is a magic square 4 9 2 OBJECTIVES 􀂃Demonstrate ability to declare a class and use it to create an object 􀂃Demonstrate ability to declare methods in a class to implement the class behaviors 􀂃Demonstrate ability to declare instance variables to implement class attributes 􀂃Demonstrate ability to use a constructor to ensure an object’s data is initialized when the object is created 􀂃Demonstrate ability to use problem-solving techniques 􀂃Demonstrate ability to use a random Number Generator. 􀂃Demonstrate ability to allow objects to interact. 􀂃Demonstrate the ability to utilize a multidimensional array This is what I have done so far. Seeking some support to move further. import java.util.Random; public class Matrix { private int size; private int [][] mXm; public Matrix(int s, boolean p) { size = s; mXm = new int [size][size]; if (p) populate(); } public void populate(){ Random rand = new Random(); int count = 1; int r, c; while (count <= Math.pow(size,2)){ r = rand.nextInt(size); c = rand.nextInt(size); if (mXm[r][c] == 0){ mXm[r][c] = count; count++; } } } public String toString(){ String square = ""; for (int r = 0; r < mXm.length; r++){ for (int c = 0; c < mXm[r].length; c++) square += String.valueOf(mXm[r][c]) + " "; square +="\n"; } return square; } } public class MatrixTest { public static void main(String args[]) { Matrix m = new Matrix (3, false); System.out.println(m); } }// end of class Methods to add: public void setValue(int[][] newValue)... public int[][] getValue() ... public void? add(Matrix anotherOne) ... public void? subtract(Matrix anotherOne) ... the last two are specified in the assignment, while the frist two (setValue and getValue) would let you set a Matrix object to a specific set of values, so that you can reproduce the tests in the assignment documentation
Read Full Discussion Thread for this article