473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

help with making object method

I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script.

function showDims(input,method) {
// Depending on 'method' is 1 then shows an alert showing the
dimensions of
// 'input' and writes to the webpage if method is 2.
var Dims = dims(input); // Get dimensions
if (method == 1) {alert("Rows: " + Dims[0] + "\n" + "Columns: " +
Dims[1]);}
if (method == 2) {
document.write("Rows: " + Dims[0] + "<br>");
document.write("Columns: " + Dims[1] + "<br>");
}
}
function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;
this.showDims(method) = showDims(this.M,method);
}
test = [
[1,2,3],
[4,5,6],
[7,8,9]];
Test = new Matrix(test);
document.write(Test.rows+"x"+Test.columns+"<br>");
Test.showDims(1);

Jul 23 '05 #1
7 1720
Jc
greenflame wrote:
I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script. <snip> function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;
this.showDims(method) = showDims(this.M,method);
} <snip> Test.showDims(1);


You should be seeing a javascript error regarding method not being
defined when you run that script. Ensure you look for javascript errors
when you get unexpected results, they can usually point you in the
right direction.

Try (untested):

this.showDims = function(method) { showDims(this.M, method); }

Jul 23 '05 #2
greenflame wrote:
I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script.

function showDims(input,method) {
// Depending on 'method' is 1 then shows an alert showing the
dimensions of
Don't let posted code auto-wrap, do it yourself at about 70 characters
(makes cut'n paste for playing much easier...) :-)
// 'input' and writes to the webpage if method is 2.
var Dims = dims(input); // Get dimensions
You don't define the dims() method anywhere that I can see. Probably
should have something like:

var Dims = [ input.rows, input.columns ];

Or maybe create a 'dims' method for your matrix object. But anyway,
when you call the function this way, 'input' is no longer a Matrix
object and has become just a plain array, so before the above line, insert:

input = new Matrix(input);
if (method == 1) {alert("Rows: " + Dims[0] + "\n" + "Columns: " +
Dims[1]);}
if (method == 2) {
document.write("Rows: " + Dims[0] + "<br>");
document.write("Columns: " + Dims[1] + "<br>");
}
}
function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;
this.showDims(method) = showDims(this.M,method);
Here you haven't quite got the syntax right:

this.showDims = function (method) { showDims(this.M, method);}

Alternatively, you could make the showDims method local to the Matrix
object (so you could delete the one above):

this.showDims = function (method) {
if (method == 1) {
alert('Rows: ' + this.rows + '\nColumns: ' + this.columns);
} else if (method == 2) {
document.write('Rows: ' + this.rows + '<br>');
document.write('Columns: ' + this.columns + '<br>');
}
};
}
test = [
[1,2,3],
[4,5,6],
[7,8,9]];
Test = new Matrix(test);
document.write(Test.rows+"x"+Test.columns+"<br>");
Test.showDims(1);


As a general comment, I would keep the two showDim methods completely
separate and have 'getDims' return an array of rows, columns then deal
with the output separately - consider your function may be called from
inside some other DOCTYPE where neither of your current syntaxes are
supported.

'dims' can become a property of the matrix:

...
this.dims = [ this.rows, this.columns ];
...

and you could get it as:

alert( Test.dims ) // gives '3,3' in your example.
document.write(...Test.dims[0]+' rows' ... Test.dims[1]+' columns'...

But that hardly seems worth the effort since you already have properties
for rows and columns.

Here's the full thing:

<script type="text/javascript">

// Don't need this if keep method local
function showDims(input,method) {

// Initialise input as a Matrix
input = new Matrix(input);
var Dims = [ input.rows, input.columns]; // Get dimensions

// Use an else here so skip second test if first true
if (method == 1) {
alert("Rows: " + Dims[0] + "\n" + "Columns: " + Dims[1]);
} else if (method == 2) {
document.write("Rows: " + Dims[0] + "<br>");
document.write("Columns: " + Dims[1] + "<br>");
}
}

function Matrix(matrixarray) {
this.M = matrixarray;
this.rows = this.M.length;
this.columns = this.M[0].length;

// Use the following line if keeping showDims external
// this.showDims = function (method) { showDims(this.M, method);}

// Delete the following declaration if using external
this.showDims = function (method) {
if (method == 1) {
alert('Rows: ' + this.rows + '\nColumns: ' + this.columns);
} else if (method == 2) {
document.write('Rows: ' + this.rows + '<br>');
document.write('Columns: ' + this.columns + '<br>');
}
};

}

test = [
[1,2,3],
[4,5,6],
[7,8,9]];

Test = new Matrix(test);
// Test = new Matrix( [ [1,2,3], [4,5,6], [7,8,9] ] );
document.write(Test.rows+"x"+Test.columns+"<br>");
Test.showDims(1);
Test.showDims(2);

</script>

--
Rob
Jul 23 '05 #3
ok. Thanks that helps. I will try it when i get the chance. :)

Jul 23 '05 #4
So how do I test if a variable say test is a matrix object or not?

Jul 23 '05 #5
greenflame wrote:
So how do I test if a variable say test is a matrix object or not?


Your constructor is:

function Matrix(matrixarray) {
...
}

So you can test to see if the constructor was 'Matrix':

alert(
'typeof Test: ' + typeof Test
+ '\nIs Test a Matrix? ' + ( Matrix == Test.constructor )
);

Will return true if Test is a 'Matrix', false otherwise.
--
Rob
Jul 23 '05 #6
With the array object I can make a new array by simply saying:

test = new Array(//length goes here//);

and then I can say like:

test[//index goes here//] = //what it equals goes here//;

I can also define an array with:

test = [//elements go here//];

I want to know if I can do a similar thing with my matrix object and if
so then how?

Jul 23 '05 #7
ASM
greenflame wrote:
With the array object I can make a new array by simply saying:

test = new Array(//length goes here//);

and then I can say like:

test[//index goes here//] = //what it equals goes here//;

I can also define an array with:

test = [//elements go here//];

I want to know if I can do a similar thing with my matrix object and if
so then how?


on my idea a matrix is an arrangement (or array) of
number of rows arrays of number of cols elements

matrix(rows,cols) = matrix(4,3) = 4 arrays each with 3 elements

<script type="text/javascript">

// ==== exo 1 ====
test = new Array(3);
test[0] = new Array('1','2','3');
test[1] = new Array('4','5','6');
test[2] = new Array('7','8','9');

test_Q = test.length * test[0].length;

alert('test : quantity = '+test_Q+'\nelmts = '+test);

// ==== exo 2 ====
matrix_a = 'a;1;2;3,b;4;5;6,c;7;8;9';
matrix_a = matrix_a.split(',');
matrix_a_w = matrix_a.length;
for(var i=0;i<matrix_a.length;i++)
matrix_a[i] = matrix_a[i].split(';');
matrix_a_h = matrix_a[0].length;
matrix_a_Q = matrix_a_w * matrix_a_h;

alert('matrix_a : quantity = '+matrix_a_Q+'\nelmts = '+matrix_a);

// ==== exo 3 ====
function newMatrix(rows,cols) {
var matrix = new Array();
for(var i=0;i<rows;i++) matrix[i] = new Array(cols);
return matrix;
}

var N = newMatrix(4,5);
var Q = N.length * N[0].length;
alert('new matrix : quantity = '+Q+'\nelmts = ['+N+']');

// ==== exo 4 ====
function Matrix(matrixarray,rows) {
var M = matrixarray.split(',');
var Cols = M.length/rows;
// alert(Cols);
var A = '<pre>Matrix =\n<u>';
for(var i=0;i<rows;i++) {
A += '| ';
var x = i*Cols;
for(var j=0;j<Cols;j++) A += M[(x+j)*1]+' | ';
A += '\n'
}
A += '<\/u><\/pre>';
document.write(A);
}
Matrix('01,02,03,04,05,06,07,08,09,10,11,12',4);
</script>

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Michael McCracken | last post by:
Hi, I have a problem with unittest.TestCase that I could really use some help with. I have a class File in module File. The important thing about File for this discussion is that it's simple -...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
10
by: Joe | last post by:
Hi, Tried using the FindControl() but no luck in finidng this damn textbox having id txtFirstName. Can someone help me with this method? This is what I have been doing but it doesn't work, ...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
5
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.