• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Design Problem

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, one more thing. You mention you get an error for accessing Matrix a.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[/qb]<hr></blockquote>
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic