Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP concept

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
0
0
CA
I have studied java for about a year now (comp sci course) and am still a bit fuzzy on when to use an object. I understand when to use objects if they relate to the real world, for example a truck object with four tire object etc.. My problem is when to apply the notion of objects when using abstract things like formulae or functions. I think my problem lies in the fact that I have ten years of programming experience with procedural and functional type languages (difficult to teach an old dog new tricks). I am also an engineer with an excellant math background, so a mathematical example would help. <br><br>Take for instance root finding of a polynomial function, I understand how to do this in java, but I am not sure if the way that I would do it is OOP. Is the function an object? <br><br>I would appreciate any help, thank you. <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Dear fenris,<br><br>If no one else will jump in here then post the java function that provides the solution you mentioned, i.e., &quot;root finding of a polynomial function&quot;.<br><br>After I see exactly what you are getting at maybe I can offer some help.<br><br>-pete
 
what you really want is an object that does mathematical calulations.&nbsp;&nbsp;&nbsp;OO is a bit hard to explain halfway across the world at a keyboard but here goes.<br><br><b>Lecturer: An object has a set of attributes and an methods.<br><font color=red>Translation: An object has stuff and does stuff.</font></b><br><br>here's a bit of code I haven't tested it so it probably needs tidying, also maths is not my strong point.<br><FONT FACE=monospace><br><i><br>/** Class MyMath performs some customised mathematical <br>&nbsp;*&nbsp;&nbsp;functions<br>&nbsp;*/</i><br>public class MyMath extends java.lang.Math {<br>&nbsp;&nbsp;&nbsp;&nbsp;/* MyMath is a child of Math therefore has access to all of Math's methods*/<br>&nbsp;&nbsp;&nbsp;&nbsp;public MyMath(){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//nothing to initialise<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;/** Finds the roots of quadratic equations.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;Note: will cause error if there are imaginary <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;roots<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br>&nbsp;&nbsp;&nbsp;&nbsp;public float[] quadRoot(float a, float b, float c){<br>&nbsp;&nbsp;&nbsp;&nbsp;/* this is a method of the <b>class</b> MyMath*/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float result[] = new float[2];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* sqrt is a method of Math (I hope :)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if this class was not a child of Math i could <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;have used java.lang.Math.sqrt()*/<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result[0]=(-b+sqrt(b*b-4*a*c))/(2*a);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result[1]=(-b-sqrt(b*b-4*a*c))/(2*a);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String args[]){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float [] holdRes = new float[2];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* runme is an intsantiated <b>object</b> of <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>class</b> MyMath */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyMath runme = new MyMath();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* the following call calls runme's quadRoot <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>method</b> */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;holdres=runme.quadRoot(1.0, 2.0, 4.0); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(holdRes[0] + &quot;,&quot; + holdRes[1]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br></font><br>clear as mud? the concepts take a while to get used to but if you look at it from a design perspective it's a bit easier.
 
&nbsp;&nbsp;Do we model light as waves or particles? It depends on what works best in a particular situation.<br>&nbsp;&nbsp;Objects have state (attributes) and behaviour (methods). What do we do about the two extreme cases - all state and no behaviour, or your case - all behaviour and no state?<br>&nbsp;&nbsp;Some programming issues make more sense in a procedural model. In an OO world, these are called 'functors' - make an object just to have a function or method available.<br>&nbsp;&nbsp;java.lang.Math is an example - it provides static methods but no state ('random' is a minor exception - there is internal state, but it is not visible to the outside).<br>&nbsp;&nbsp;So always think about how state and behaviour fit together, but accept that some cases do not.<br>
 
Thank you very much for the help, I think I have a better grasp of OO concept as it pretains to math. Obviously it will take time and practice for me to develop a clear understanding of how OO works. <br><br>BTW, is OOP faster then say and equivalent program that is run in a procedural langauge?<br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
OOP (obviously) is designed for working with real-world models. That, you've already stated. But it seems to me that you are trying to use it for something it's not intended- and that is functional programming. You're rather experienced in functional languages, and rather new to Java, so you're using Java to attack problems that might be best suited for another functional language. The question is not only &quot;when to use an object&quot;; the question is also, &quot;when not to use an object.&quot; You can pervert objects so that they basically run functions, or just create one object whose methods are not changing the object but rather returning function results. This is not object-oriented programming; this is rather functional programming masquerading as OOP. &quot;If the only tool you have is a hammer, then every tool looks like a nail.&quot; If all you have is OOP, then you will attack every problem froom an OOP standpoint, and get frustrated when it doesn't seem to click. The reason it doesn't click is because it shouldn't- you shouldn't be using Java for the sole purpose of root finding of a polynomial function. If that's all you need to do, then pick a different language (if you have a more diverse toolkit, which you obviously do, pick the best tool for the job). However, if you have objects that need the root of a poly function, that's a different story. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
Liam, <br>Take my example of polynomial root finding, it may not have been a good example, but would you not be able to think of the polynomial as an object? It has a state that is defined by a particular location along it's length. And it's behavior is defined by it's equation. That is probably stretching it. <br><br>Anyway the main reason that I want to learn java is for it's portability and networking capabilities. I have a bit of experience with other languages, but I don't have the fascination with them as I do with java. I wrote my thesis in VB becuase at the time I was starting my thesis I was also starting to learn java. I thought that it would be best to write my thesis in VB and later translate it to java. My thesis involved genetic algorithms and I know that they can be setup to run in a distributed manor. VB doesn't have this capability. <br><br>In java my goals are to learn network programming, then distributed computation, and finally GUI building and graphics.<br><br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top