[Show all top banners]

Slackdemic

More by Slackdemic
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java Programming
[VIEWED 781 TIMES]
SAVE! for ease of future access.
Posted on 10-20-06 7:16 PM     Reply [Subscribe]
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
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 30 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
मन भित्र को पत्रै पत्र!
Why Americans reverse park?
They are openly permitting undocumented immigrants to participate in federal elections in Arizona now.
Driver license help ASAP sathiharu
ढ्याउ गर्दा दसैँको खसी गनाउच
TPS Reregistration and EAD Approval Timeline.......
lost $3500 on penny stocks !!!
nrn citizenship
Changing job after i-140 approval
Is this a progressive step?
Nepalese Students Face Deportation over Pro-Palestine Protest
Trasiting through Istanbul, Turkey
Nepali **fake** Veterans. Be aware!!
whats wrong living with your parents ?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters