posted 12 years ago

Dear all,

I have a Matrix Program which I need to add, multiply, subtract.

I have a Matrix Class of the following

public class Matrix{

double [][]elements;

public add(Matrix x);

public subtract(Matrix x);

}

I have a MatrixMain program which is reponsible for showing the menu for the above operations. eg(1) Add, (2) Multiply, (3)Subtract (4)Exit

I have problem making my program Object Oriented. This is wat I did

public class MatrixMain{

public static void main(String args[])

{

Matrix a = new a();

......

i = showMenu();

switch(i)

{

case 1: option1();//add

case 2: option2();//multiply

.....

}

public static option1()

{

//Do adding here....

//Get error for accessing Matrix a here

}

}

is there a way to make my program OO? Thanks alot

I have a Matrix Program which I need to add, multiply, subtract.

I have a Matrix Class of the following

public class Matrix{

double [][]elements;

public add(Matrix x);

public subtract(Matrix x);

}

I have a MatrixMain program which is reponsible for showing the menu for the above operations. eg(1) Add, (2) Multiply, (3)Subtract (4)Exit

I have problem making my program Object Oriented. This is wat I did

public class MatrixMain{

public static void main(String args[])

{

Matrix a = new a();

......

i = showMenu();

switch(i)

{

case 1: option1();//add

case 2: option2();//multiply

.....

}

public static option1()

{

//Do adding here....

//Get error for accessing Matrix a here

}

}

is there a way to make my program OO? Thanks alot

posted 12 years ago

Well, if it's written in Java (provided it compiles and runs), it's object oriented by definition. Except for the primitive data types, everything in Java is an object.

What I did notice about your code is that you define a class, MatrixMain, and then create an object of type Matrix using:

Matrix a = new a();

What you need at this line is:

Matrix a = new Matrix();

What I did notice about your code is that you define a class, MatrixMain, and then create an object of type Matrix using:

Matrix a = new a();

What you need at this line is:

Matrix a = new Matrix();

M. Gagnon

Ranch Hand

Posts: 68

posted 12 years ago

Ah, one more thing. You mention you get an error for accessing Matrix a.

You have declared Matrix a within the main() method. To access Matrix a in the option1() method, you will have to pass it as a parameter or declare it as an instance variable of the class and make it a static instance variable at that.

Originally posted by Timothy Leong:

public class MatrixMain{

public static void main(String args[])

{

Matrix a = new a();

......

}

public static option1()

{

//Do adding here....

//Get error for accessing Matrix a here

}

}

You have declared Matrix a within the main() method. To access Matrix a in the option1() method, you will have to pass it as a parameter or declare it as an instance variable of the class and make it a static instance variable at that.

M. Gagnon

Ranch Hand

Posts: 68

posted 12 years ago

Just as a bit of good object-oriented design advice, it is probably best to define the add(), subtract() and multiply() methods in the Matrix class. An object should encapsulate the data it's representing as well as the methods used to manipulate that data.

I really should have carefully read your post before replying. Sorry about that.

I really should have carefully read your post before replying. Sorry about that.

posted 12 years ago

[/qb]<hr></blockquote>

This will not compile because your methods do not have a return type.

Why are you doing the adding, subtracting e.t.c in the MatrixMain class ?surely the Matrix class should handle the Maths

[ June 08, 2005: Message edited by: Nigel Browne ]

This will not compile because your methods do not have a return type.

Originally posted by Timothy Leong:

Why are you doing the adding, subtracting e.t.c in the MatrixMain class ?surely the Matrix class should handle the Maths

[ June 08, 2005: Message edited by: Nigel Browne ]