1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Questions on Object Oriented Programming

  1. Dec 4, 2012 #1
    I don't think I fully understand what "object oriented" actually means. Anyway, is it useful for a physics major hoping to get into grad. school to know object oriented programming? I have yet to take two compulsory computational physics courses where we will cover several numerical methods for solving equations (non-linear, odes, pdes, integrals, schrödinger etc.), monte carlo methods, genetic algorithms and molecular dynamics. Would it be a waste of credits to take object oriented programming beforehand?
     
  2. jcsd
  3. Dec 4, 2012 #2
    I can answer this question from a game development point of view, but no more. If you have done Java, "classes" are objects. An object is a way to visualize certain elements of your code. In a FPS game, an object could be your character, your gun, the game map etc.
    In a circuit model, an object could be a resistor, a capacitor, a transistor etc. The object is a chunk of code that distinguishes it from other objects by its unique name as well its unique methods that allows it to interact with other objects in the program.

    The advantage of objects is that each object has its own set of methods, and those methods can be controlled selectively by the invocation of the object's class name. In addition, objects can be duplicated with ease, and they can also be destroyed with ease. Objects also make planning the program much easier because they destroy a barrier between the abstraction of code and the intuition of the program. This is extremely important.

    An object in the sense of the computer science definition, I should clarify, refers not to a single object in the regular sense of the word, but rather to a species of objects (in the regular sense of the word). So the "player" class in a blackjack program is a general template for the players of the game. Each player of the game is an "instance" of the more general "player" class. Each instance can interact with other instances of the player object, or even with instances of other objects.

    BiP
     
  4. Dec 4, 2012 #3
    Thank you, BiP! Now could you give some insight as to whether or not it would be an advantage when taking computational physics courses? Would taking a course on object oriented programming be a waste of credits or would it actually improve my programming skills, regarding the implementation of numerical methods?
     
  5. Dec 5, 2012 #4
    I don't know much about computational physics. To be honest, I cannot think of any programming courses that are taught other than object oriented programming. Perhaps this is because I am young, so I learned only the new programming languages, all of which have OOP. Old languages didn't pack the feature of OOP.

    If you are comparing OOP with a non-OOP course, I don't really know what to say because I can't imagine programming without objects. I would say take OOP, since it is so fundamental to good technique, and is not too hard to pick up.

    Bt OOP can also easily be self-taught. In fact, programming in general is *mostly* a self-taught skill; you just need the books (or videos).

    But if you compare OOP with a math course, take the math course and self-learn the OOP.

    Just to clarify, have you done programming before? What languages, if any?

    BiP
     
  6. Dec 5, 2012 #5
    I've programmed some basic methods in C. Newton-Raphson, bisection, finite difference, romberg integration, matrix inversion, etc... I have also several numerical methods programmed in MATLAB. Maybe I've been doing OOP all along and I don't even know because I didn't know what the term exactly means haha.
     
  7. Dec 5, 2012 #6

    Ibix

    User Avatar
    Science Advisor

    If you are using C, you haven't done object oriented programming. C++ includes object orientation.

    The advantage to object orientation is that it lets you organise your data in a natural way, as opposed to the way that is convenient in the language. For example, a simple simulation of the solar system needs you to store the x, y and z positions, x, y and z velocities, masses, and names of all the planets. In C, that would be something like:
    Code (Text):

    int main(void) {
        double x[9],y[9],z[9];
        double vx[9],vy[9],vz[9];
        double M[9];
        char *name[9];
        //....
    }
     
    Note that you have arranged all of the x values in one data structure, all of the y values in another structure, and so on. That is completely orthogonal to the "natural" way to think about the simulation, which is nine objects. In C++, you could define a class:
    Code (Text):

    class Planet {
        double x,y,z;
        double vx,vy,vz;
        double M;
        char *name;
    };
     
    and create nine instances of it (I know, I know, arrays of objects on the stack, but I don't want to get into malloc/new):
    Code (Text):

    int main(void) {
        Planet planets[9];
        // ....
    }
     
    Now all of your data about one planet is wrapped together, and you just create an array of 9 Planets - a much more natural way to think about the model. Object orientation also lets you define functions inside the a class (called "methods") that let you manipulate the data in the class - you'd probably want to define a "move" method updates the position and velocity of the Planet, and probably others. Also, you can declare that the variables defined in the class can only be manipulated by the methods of that class - anything outside the class must call a method called something like getX() to get the x-coordinate. Then, if you suddenly realise that everything would have been easier if you'd done it all in spherical polar coordinates, you know that you do not need to look beyond the bounds of the class for dependencies - nothing outside ever knew how the guts worked. Object orientation also provides an extremely powerful way to extend classes - for example, you can define a Spaceship class as "just like a Planet, but with a few extra attributes and a slightly different way of moving".

    That's a very quick overview of object oriented programming. It is a very useful tool for constructing complicated programs by breaking the task down into manageable chunks and formally specifying the way that the chunks will talk to one another.

    It is not, I think, really useful for numerical methods. For all their complexity, numerical methods boil down to traversing arrays of numbers, doing mathematical operations on them, and doing it fast. They don't need to be split up into separate black-boxes, and would suffer performance penalties in doing so - unacceptable inside a do-this-a-billion-times loop. While I would very much recommend learning object oriented programming if you are ever intending to develop anything with complicated data structures, the nuts and bolts of numerical computing is about the only area I can think of that wouldn't benefit from it.
     
  8. Dec 5, 2012 #7

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Why such a narrow focus? An OO programming class probably won't help you do better in a numerical methods class. Your numerical methods classes are going to be focused on algorithms, and possibly some down and dirty stuff such as how to tailor an algorithm to better take advantage of cache memory.

    That doesn't mean it's not a good idea to take such a class. The mindset should be that taking classes such as this as technical electives will make you a better and more employable scientist or engineer. From far too many years of experience, the ability to program properly is a skill that most scientists and engineers have not learned.

    To answer your narrow focus question, there is a computer science class that will help you improve your skills with regard to numerical methods. That's the data structures & algorithms class offered by almost every computer science department, typically at the sophomore level. This class typically has "Introduction to Programming" as a prerequisite, and that intro to programming class nowadays is most likely going to use an object oriented language.
     
  9. Dec 5, 2012 #8
    I have such a "narrow focus" because I have only certain amount of credits for free electives and I would rather use them on courses more related to physics (atomic force microscopy, intro nuclear and particle physics, molecular modelling, that sort of stuff which isn't compulsory for the major). If I can understand computational physics without needing to take a course on OOP then I'll do it that way. Courses on algorithms you mention sound interesting, though. I'll try to look into that. Thank you.
     
  10. Dec 5, 2012 #9
    Thank you, Ibix! You answered my question and even more. I'll take this into account when registering my courses for the next semester.
     
  11. Dec 7, 2012 #10

    chiro

    User Avatar
    Science Advisor

    Hey cesaruelas.

    From a memory point of view objects are just data structures.

    You can embed structures within structures and this is what inheritance does.

    The structures can contain not just actual data (like integers, floats, and so on) but also pointers and these pointers can point to functions as well as data objects.

    This is basically what a class instance looks like in memory: it's just a struct object in C with a lot of pointers alongside the data to accomplish all the Object Oriented Stuff that the compiler and the language provide.

    Accessing the elements is just looking up the right memory address for the start of the instance and then moving to the offset for the particular element of the structure.

    The linguistic perspective is different and C++ books cover this in depth but all it really is in memory is a struct and this is just a standard C-based procedural programming construct.
     
  12. Dec 7, 2012 #11

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    That was true for programming back in about 1960, but as a methodology for developing numerical software it passed its sell-by date round about 1980.

    (It's probably all you need to know to pass a "cook-book" first course in numerical methods, but that's beside the point).

    As D.H. said, the key to writing efficient large-scale software that runs on multiprocessing hardware is what is covered in a typical "data structures and (non-numerical) algorithms" course, not the (small number of) lines of code that do the clever arithmetic.

    You can take whatever view you like about OOP as a philosophy for software development, but for any modern programming language the libraries that implemnt those "data structures and algorithms" will almost certainly have an interface that is designed on OOP principles - so in real-world programming, "resistance to OOP is futile".
     
  13. Dec 8, 2012 #12

    Ibix

    User Avatar
    Science Advisor

    Yes. In among all of my post, I only answered half the question, and the less important half at that.

    You do not need object oriented programming techniques to understand how (say) a numerical integrator works (which is what I said). And if you want to write numerical integrators, you can probably get away without object oriented programming. However, you will need to understand (at least) the basics to do more or less anything else, including actually using the numerical integrator (which I should have said). It will almost certainly be written for you by someone who wrapped it in an object oriented package.

    I used to use numpy quite a lot. It's a numerical library for the python programming language, and a case in point. Its Fast Fourier Transform (FFT) capability is based (I think) on the FFTW library, which is written in C (i.e., non-object oriented). But python is object oriented, and there is almost nothing FFT-related that you could do without understanding object orientation. You can, if you like, write C code and link it against the FFTW library - but (from experience!) it'll take four times as long and be much harder than doing in python.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Questions on Object Oriented Programming
Loading...