[Show all top banners]

Slackdemic
Replies to this thread:

More by Slackdemic
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 Java Programming Help
[VIEWED 1718 TIMES]
SAVE! for ease of future access.
Posted on 10-20-06 8:12 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
 
Posted on 10-20-06 9:57 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Slackdemic bro, do you know about P threads? I have been working on the following program. Could you shed some lights on ?


Given two matrices A and B, where A is a matrix with M rows and K columns and
matrix B contains K rows and N columns, the matrix product of A and B is
matrix C, where C contains M rows and N columns. The entry in matrix C for
row i column j (C_i,j) is the sum of the products of the elements for row i in
matrix A and column j in matrix B. That is:

__K___
C_i,j = / A_i,n X B_n,j
/_______
n = 1

For example, if A were a 3-by-2 matrix and B were a 2-by-3 matrix, element
C_3,1 would be the sum of A_3,1 and B_2,1.

For this project, calculate each element C_i,j in a separate worker thread.
This will involve createing M X N worker threads. The main - or parent -
thread will initialize the matrices A and B and allocate sufficient memory
for matrix C, which will hold the product of matrices A and B. These matrices
will be declared as global data so that each worker thread has access to A, B
and C. Matrices A and B can be initialized statically, as shown below:

#define M 3
#define K 2
#define N 3

int A [M][K] = { {1,4}, {2,5}, {3, 6} };
int B [K][N] = { {8,7,6}, {5,4,3} };
int C [M][N];

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

Following is the code, that I have to modify. Instead of defining the matrix size as 5, Matrices A and B should be initialized statically as given above. If you have time could you please look into it ? I modified the following code but somehow answer is not coming right.

In UNIX, to compile the following program mat.c

cc mat.c -o mat -p.thread
./mat 1




#include
#include
#include

#define SIZE 5 /* Size of matrices */
int N; /* number of threads */

int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];

void fill_matrix(int m[SIZE][SIZE])
{
int i, j, n = 0;
for (i=0; i for (j=0; j m[i][j] = n++;
}

void print_matrix(int m[SIZE][SIZE])
{
int i, j = 0;
for (i=0; i printf("\n\t| ");
for (j=0; j printf("%2d ", m[i][j]);
printf("|");
}
}


void* mmult (void* slice)
{
int s = (int)slice;
int from = (s * SIZE)/N; /* note that this 'slicing' works fine */
int to = ((s+1) * SIZE)/N; /* even if SIZE is not divisible by N */
int i,j,k;

printf("computing slice %d (from row %d to %d)\n", s, from, to-1);
for (i=from; i for (j=0; j C[i][j]=0;
for (k=0; k C[i][j] += A[i][k]*B[k][j];
}

printf("finished slice %d\n", s);
return 0;
}

int main(int argc, char *argv[])
{
pthread_t *thread;
int i;

if (argc!=2) {
printf("Usage: %s number_of_threads\n",argv[0]);
exit(-1);
}

N=atoi(argv[1]);
fill_matrix(A);
fill_matrix(B);
thread = malloc(N*sizeof(pthread_t));

for (i=1; i if (pthread_create (&thread[i], NULL, mmult, (void*)i) != 0 ) {
perror("Can't create thread");
exit(-1);
}
}

/* master thread is thread 0 so: */
mmult(0);

for (i=1; i
printf("\n\n");
print_matrix(A);
printf("\n\n\t * \n");
print_matrix(B);
printf("\n\n\t = \n");
print_matrix(C);
printf("\n\n");

return 0;

}
 
Posted on 10-20-06 10:01 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Since this form allows HTML tags, it's not displayin the #include part properly.

So here i type again

#include #include #include
 
Posted on 10-20-06 10:01 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

damn !

#include pthread.h
#include stdlib.h
#include stdio.h
 
Posted on 10-24-06 8:58 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Nas,

I am sorry, I didn't check this thread and also I don't think, I can be of some help. I am a novice myself.

Sorry. :(
 
Posted on 10-24-06 9:06 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Thank you for your reply bro.
 
Posted on 10-24-06 10:52 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

nas,
2 questions to you.

1. void* mmult (void* slice)
what r ya trying to do here? mmult(void) means that you cannot pass any arguments. or am i getting it wrong? and then you case the so called argument into (int).
does java allow passing void argument and then accepting and casting ..
hmm.. sth new for me. there

2. for (i=1; i if (pthread_create (&thread[i], NULL, mmult, (void*)i) != 0 )
for needs 3 limits. start, range and end. i only see two.
pthread_create takes 4 arguments; either i see only 3 or if you call mmult as one argument then your code would not even compile

if it does then then the slice is some garbage in the register. i cannot even comprehend how this code would run let alone compile.

=====================================================
as always
what do i know (O:
 
Posted on 10-24-06 10:55 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

definitely c does not allow passing void argument and then casting it.
[ i do not know why i even mentioned java.. prolly my mind was full of it (O:]
sowwie...
 


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 200 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration
What are your first memories of when Nepal Television Began?
निगुरो थाहा छ ??
ChatSansar.com Naya Nepal Chat
TPS Re-registration case still pending ..
Basnet or Basnyat ??
Sajha has turned into MAGATs nest
NRN card pros and cons?
Do nepalese really need TPS?
कता जादै छ नेपाली समाज ??
Will MAGA really start shooting people?
Democrats are so sure Trump will win
मन भित्र को पत्रै पत्र!
Top 10 Anti-vaxxers Who Got Owned by COVID
I regret not marrying a girl at least for green card. do you think TPS will remain for a long time?
emergency donation needed
TPS Work Permit/How long your took?
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
Breathe in. Breathe out.
3 most corrupt politicians in the world
Nas and The Bokas: Coming to a Night Club near you
Mr. Dipak Gyawali-ji Talk is Cheap. US sends $ 200 million to Nepal every year.
Harvard Nepali Students Association Blame Israel for hamas terrorist attacks
TPS Update : Jajarkot earthquake
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