GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Class Design Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Design Problem" Watch "Class Design Problem" New topic
Author

Class Design Problem

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
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

Joined: Feb 18, 2004
Posts: 68
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

Joined: Feb 18, 2004
Posts: 68
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

Joined: Feb 18, 2004
Posts: 68
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

Joined: May 15, 2001
Posts: 673
[/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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Design Problem