Results 1 to 3 of 3
- 05-26-2011, 05:23 AM #1Member
- Join Date
- May 2011
- Rep Power
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.
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.
- 05-27-2011, 05:39 PM #3
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.
- By fiefa in forum New To JavaReplies: 1Last Post: 01-31-2011, 05:18 PM
- By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 05:29 AM
- By kirly in forum New To JavaReplies: 5Last Post: 02-05-2009, 06:59 AM
- By root_demon in forum New To JavaReplies: 7Last Post: 10-31-2008, 06:32 AM
- By Preethi in forum New To JavaReplies: 4Last Post: 06-14-2008, 04:29 AM