Results 1 to 3 of 3
  1. #1
    xenogenx is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Converting programs from function to object based programming

    Hey there, im fairly new to programming, currently in a high school programming class and I have a few questions. Right now my class is working on object based programming and we each have to take a program thats written sequentially or in functions and convert that to object based. The program I am converting is Asteroids I am using the source code from brainjar here . I am supposed to but it into different classes i.e one for the asteroids, spaceship, ufos etc.He said it would be fairly easy for me because its already in functions but the problem is I have no idea how to even begin, if anyone has any advice please post a comment I could use all the help I can.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First of all write down all the involved "nouns" involved such as the universe, the asteroids, a space ship -- those are the classes. Then their behaviors or actions -- those are their methods.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yeah, so the whole point of Object Oriented Programming (OOP) is to model something using objects (sounds obvious, but can be tricky to grasp at first). In the old days before OOP, one might use arrays of numbers and index values and strings and so forth to represent 'things' in the program. A spaceship might be a combination of a whole bunch of functions and bits of data spread all over the place.

    The object approach is different. You model a spaceship, and include the relevant attributes inside the model. So, a space ship model might consist of a position on the screen (a Point), an image sprite (BufferedImage or ImageIcon), a few ints or floats to represent speed or direction, and then a few methods which provide functionality to the ship (paint(), move(), blowUp(), shoot()).

    Rather than sprinkling these things all over 1 long program file, you divide your program into smaller parts. In the case of java, classes and methods. You would write a class for each object in the game (like Fubarable said). Then you could have some 'driver' or 'main' class which coordinates all of the objects. The driver class might have a collection of asteroids and other objects. Each object is self contained.

    This allows organized, modular code instead of a 12,000 line spaghetti nightmare, and makes it easier to focus on the logic of how the program should function instead of how it was written.

Similar Threads

  1. Converting an array in an object
    By fiefa in forum New To Java
    Replies: 1
    Last Post: 01-31-2011, 04:18 PM
  2. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  3. Clear Screen in Console based programs
    By kirly in forum New To Java
    Replies: 5
    Last Post: 02-05-2009, 05:59 AM
  4. stumpt. converting an object to a long int.
    By root_demon in forum New To Java
    Replies: 7
    Last Post: 10-31-2008, 05:32 AM
  5. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •