Forum

> > Stranded II > General > j(ava)Stranded, the O-O approach
Forums overviewStranded II overviewGeneral overviewLog in to reply

English j(ava)Stranded, the O-O approach

35 replies
Page
To the start Previous 1 2 Next To the start

old Re: j(ava)Stranded, the O-O approach

palomino
User Off Offline

Quote
Master_Aricitic has written
I checked shortly after writing that (or during) and found that python is object-oriented and extensible, so it is similar to Java.

Hello world in Java:
1
2
3
4
5
6
7
class smthjava
{  
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}
(I don't know that, I just copied it)

Hello world in Python:
1
print "Hello World!"


My favorite comparison

old re: Master_Aricitic

Master_Aricitic
User Off Offline

Quote
Master_Aricitic has written
I recently discovered that I have a tendency to try and 'code perfect,' which has gotten me in trouble in class on numerous occasions... Basically, I reinvent my wheel each time I learn something new about 'wheels' in general, or I try to add the ability to move up irregular surfaces before I can even move along roads...
NOW, I am trying to relearn my practice and JUST WRITE SOMETHING THAT WORKS!!! so I can actually give everything something to test and try...

In my experience it is best to start with the absolute most basic aspect of the problem, create a solution, and prove that it is a solution. Using your 'inventing the wheel' metaphore this means discovering what the most basic thing a 'wheel' needs to do, then create a solution for that. Once you have a solution prove that it works. In game programming, specifically recreating this game what you should do is this: start with a basic program interface that you know works, the jMonkeyEngine for example, and make it show terrain. Once you've done that in the way 'they' (the JMonkeyEngine programmers) expect you to do it you should try and separate the methods and fields you used to create the terrain into a separate class; once you've proven that can work then you can continue on (i.e. creating a player).
Basically do this: do the absolutely most basic thing you need to do the way they want you to do it, then see if it is even possible to do that the way you want to do it. If it is then continue on from there. : A wheel needs to roll, find a shape that rolls (a circle), find a way to attach the shape to something that carries other objects (a cart, etc).

... re: re: Master_Aricitic
Master_Aricitic has written
blah, blah, blah...

OMG, you're a genius! Once I tried it their way, and proved it would work, and then 'piece by piece' ripped the code out of the 'single class' implementation and put it into a 'multi class' implementation, after fixing a few NullPointerExceptions it worked! I can't believe I didn't think of that myself! I mean, seriously, 'do it the way they tell/show you to' and THEN figure out how to do it your way from there? Who would have thought of that?

... re: re: re: Master_Aricitic
Master_Aricitic has written
Two blah, or not too blah...

Your sarcasm is tangible, but I'm glad I could help...

old re: Sunny Autumn

Master_Aricitic
User Off Offline

Quote
Sunny Autumn has written
Are you... talking to yourself?..


Yeah, I was talking to myself. I was making fun of my silliness. I had been trying to do everything at once and none of it was working. Once I stopped doing that and did the 'tutorials' exactly the way they were shown (or as exactly as I could stand), I proved to myself that it could work. After that it was just a matter of adjusting it to work the way I wanted... Simple, yet, I couldn't figure it out until I'd frustrated myself to the point of nearly quitting for a while... /dur
Well, I'm 'back on track' and would be going strong if I weren't ill....
Peace.

old Re: j(ava)Stranded, the O-O approach

bezmolvie
User Off Offline

Quote
Sunny Autumn, are you comparing Python to Java? ...I don't even... how...

Anyway, admittedly I've been working on my own little game for the past month (Project Title as is: "Miner's Folly") in my spare time and I've gotten what I've done to work. But, programming wise, it looks kind of similar to what you're doing (No, I did not steal your ideas *shifty eyes*). It's possible that we may be able to exchange solutions to problems and then adapt them. However, I am heavily an amateur, and you'd probably see that with a glance at my code.

Looking back at my writing: "Meh"

old re: Bloodshot

Master_Aricitic
User Off Offline

Quote
Bloodshot has written
Sunny Autumn, are you comparing Python to Java? ...I don't even... how...

Anyway, admittedly I've been working on my own little game for the past month (Project Title as is: "Miner's Folly") in my spare time and I've gotten what I've done to work. But, programming wise, it looks kind of similar to what you're doing (No, I did not steal your ideas *shifty eyes*). It's possible that we may be able to exchange solutions to problems and then adapt them. However, I am heavily an amateur, and you'd probably see that with a glance at my code.

Looking back at my writing: "Meh"


I'd love to take a look at your game. I would love to comment you code and suggest ideas. Honestly, I've found that I am much better at helping others with their things than doing my own... but, then, until recently, I've never really honestly tried to do something on my own.
Anyway, yes, I would love to look at your game and the coding for your game. If you want me to comment on it I'd like a better idea of what you're trying to do first. And, with regards to 'not stealing my ideas', don't worry, it's 'impossible' to steal my ideas, for, while they may not be entirely unique, their way of implementation, technique and approach are. So, while any ol' Joe could come up with my ideas they'd never think to do it the same way I would. (Or so my self belief tells me...)
e-mail me at - Email address (only visible with login) -; my email registered here, I believe, is different, but I'm trying to have my 'real name' out less now... (my 'common' email is <first name><last name>@gmail.com, so, yeah, kinda easy to tell who I am beyond what most people comfortably put on display...)

old Re: j(ava)Stranded, the O-O approach

DannyDeth
User Off Offline

Quote
Java has a very yucky syntax in my honest opinion, it looks like a bit of Lua mixed with C and run through a blender if you had to ask me.

But I follow your project with great interest, and I wish you the best of luck

old re: Sunny Autumn

Master_Aricitic
User Off Offline

Quote
Sunny Autumn has written
Started learning Java. With the right tut and tools it's a really easy and versatile language. How could I not pay attention to it?


Java is really easy to overlook, unless there is a specific reason to learn it (such as a class or something). There is a lot of bad talk going around about Java, and it DOES have problems. The thing that people seem to ignore though is that all programming languages have problems, some are built to fix the problems of another (i.e. Java for C/C++). Java, being a managed language (meaning it isn't purely native, and has components that are managed for the programmer, i.e. memory [C++ you have to explicitly tell to remove something from memory]), runs slower than pure native languages. Still, in recent years and editions it is beginning to run at near-native speeds.
Some languages are interpreted, also commonly called scripting languages. JavaScript is an (obvious) example; Delphi, Pearl, Python and Lisp are other examples. Interpreted languages run slower than near-native and certainly slower than native languages because they have to be parsed before they can be run. Similar to Java, interpreted languages use something like a virtual machine to run, as they have to be interpreted and converted into machine code to be used. Unlike Java they cannot and are not directly converted into machine code (recent versions of Java will actually automatically compile into machine code, rather than run within the Virtual Machine). This is true of Lisp, though I am unsure of the statements validity of Python, Pearl and Delphi (as I haven't studied those languages).
Anyone who wants to do serious programming needs to learn a minimum of Java level language, anyone who wants to do 'professional' programming needs to learn C++... (I began learning C++ in one semester of a two+ semester course, but transferred to a different college before finishing...) While higher level languages (i.e. scripting) are easier to use and understand, they are harder, and thus slower, to run. Lower level languages (i.e. C++, assembly) are compiled to machine code (a sequence of 0's and 1's) and thus run natively (as fast as possible-assuming efficient programming).

re: DannyDeth
Firstly, I am curious as to the language you use. Secondly, as I've learned MASM, ASM, a little C++, and Java (and have seen Python, LUA, and others) I can understand what you're saying, but I prefer Java style syntax over C++, and certainly ASM...
Again, I'm curious what language you use.

old Re: j(ava)Stranded, the O-O approach

DannyDeth
User Off Offline

Quote
I mostly use C, then Python, then Ruby and then Lua. I am also kinda familiar with FORTRAN and I know BASIC in and out but find the language to basic ( pun not intended ) for most use. The only form of BASIc that is still remotely usable for me is BlitzBasic and BlitzMax, which aren't really BASIC anymore

EDIT: Oh yeah, I am also kinda familiar with 8086 ASM, it's the only kind I lbothered learning

old Re: j(ava)Stranded, the O-O approach

Lee
Moderator Off Offline

Quote
Master_Aricitic has written
Some languages are interpreted, also commonly called scripting languages. JavaScript is an (obvious) example; Delphi, Pearl, Python and Lisp are other examples. Interpreted languages run slower than near-native and certainly slower than native languages because they have to be parsed before they can be run. Similar to Java, interpreted languages use something like a virtual machine to run, as they have to be interpreted and converted into machine code to be used. Unlike Java they cannot and are not directly converted into machine code (recent versions of Java will actually automatically compile into machine code, rather than run within the Virtual Machine). This is true of Lisp, though I am unsure of the statements validity of Python, Pearl and Delphi (as I haven't studied those languages).


That's actually not quite the reason that interpreted languages are slow. True, they have to be parsed from source, but that is only a one-time overhead and is usually relatively insignificant. Modern interpreters usually use several "stacks" to keep track of the program space. Java for example uses an indexable heap-like structure that acts nominally as a stack with either (byte)strings (all object signatures and references are serialized as strings, which is a rather clumsy way of doing it to be perfectly honest) or bitstrings of varying sizes for large integers or references. Similarly, Python dynamically allocates blocks of memory with the heap at the bottom and the stack on top, which contains references to the absolute location of these python objects in the CPython interpreter (a nonprimitive object occupies quite a lot of memory and a single lookup can amount to hundreds of reference lookups, which makes it comparably slower than both Java's and Lua's implementation). Because a program running in a managed environment requires many services from its host (GC, dynamic memory, Java's threading scheduler and Python's GIL comes to mind immediately), a significant amount of overhead accompanies each cycle of interpretation, which is the reason why interpreted languages are usually slower than native code.

Now since interpreted source is usually represented in binary trees, this structuredness allows for inline optimization of the logic of the program without explicit instructions from the programmer (GCC at default optimization level will usually strip out unreachable code, take out empty loops, optimize arithmetic, etc). At an interpreted level, as the interpreter is itself native code, this cannot be done (except for bytecode optimization). A native application however can emit machine code and inject it into its own heap, so interpreters can inline some of the instructions as machine code. But due to the complexity of object orientation, this is only done on primitive types (Java's JIT cannot optimize reference manipulations save for Integer, Float, and other composite types that autoboxes to primitives). I believe this is what you meant by Java being capable of producing machine code. This is not exclusive to Java however, other managed environment have also been known to JIT part of the bytecode into machine code (LLVM is one of the most sophisticated platforms out there, CPython also does a little bit if you ask it to, LuaJIT, a few Javascript engines, and MS's CLR which popularized this idea in the beginning of the millennium)

PS: Java's VM is an interpreter in the full sense of that word. If you really want an easy way to learn the limitations of java, recode a non-optimizing version of the JVM in java itself. It's actually not as difficult as you would believe it to be as the entire specification of the JVM is open.

And while we're on the topic of Java, it's tradition for developers to make fun of the languages that he uses, so here's my go at it.

Often in computational analysis, we will encounter rate equations for which there are no easy solutions. The traditional way of finding unique solutions for such DEs is to use Euler's method for successive approximation, which can implemented as such:

1
2
3
4
5
def euler(dy, yn, tmax, h, tn=0):
	yn += h*dy(tn,yn)
	return yn if tn >= tmax else euler(dy, yn, tmax, h, tn+h)

print "Approximation for y' = y (e^t): %s"%euler(lambda t,y:y, 1., 2., 0.1)

But coming from java, where we had to use an ugly hackaround of initializing anonymous interfaces (Hell, even C# had delegate methods...)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class EulerSolve {
	public interface DifferentialEquation{
		abstract double function(double t, double y);
	}
	
	public static double approximate(DifferentialEquation dy, double yn, double tn, double tmax, double h){
		yn = yn+h*dy.function(tn,yn);
		if (tn < tmax){
			return approximate(dy, yn, tn+h, tmax, h);
		}else{
			return yn;
		}
	}
	
	public static void main(String[] args){
		
		double eulerApproximation = EulerSolve.approximate(new EulerSolve.DifferentialEquation() {
			@Override
			public double function(double t, double y) {
				return y;
			}
		}, 1., 0., 2.0, 0.1);
		
		System.out.printf("Approximation for y' = y (e^t): %f", eulerApproximation);
	}
}

I had always thought that "OO Interfaces" was the only right way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class EulerSolve(object):
	class DifferentialEquation(object):
		def function(self, t, y): pass;
	@classmethod
	def approximate(cls, dy, yn, tn, tmax, h):
		yn += h*dy.function(tn, yn);
		if (tn < tmax):
			return cls.approximate(dy, yn, tn+h, tmax, h);
		else:
			return yn;

class dyEqualsY(EulerSolve.DifferentialEquation):
	def function(self, t, y): return y;

def main():
	diffEq = dyEqualsY();
	approximation = EulerSolve.approximate(diffEq, 1., 0, 2., 0.1);
	
	print "Approximation for y' = y (e^t): %f"%approximation;

if __name__ == "__main__":
	main()

I was satisfied with my handywork (a little bitter about the lack of PythonDoc support) so I just stopped there, being none the wiser about how to program in python.

A friend of mine took a look at the code and looked at me as I were crazy. I asked him what was up, but he just kept staring at me as if in disbelief. He then wrote two brief lines of code so elegant and concise that my mind was completely blown to pieces...

What did he write?

1
2
3
4
easy_install euler

# Lee you're a f*cking moron.
import euler

old re: Lee

Master_Aricitic
User Off Offline

Quote
First of all, I once read that the fastest way of getting the correct answer to something is post the 'wrong' answer and wait for people to correct you. (viciously and flame were used in the original text, but don't apply here). Today, you've proved that to be correct.
I knew that at least part of what I was writing was wrong, as I haven't learned Python, Delphy, etc, but I was trying to state the basic information.
Really, by interpreted, I meant things like Lisp, if Python isn't 'like' Lisp then I was clearly wrong. Period. End of story...

Anyway:
Lee has written
1
2
3
4
easy_install euler

# Lee you're a f*cking moron.
import euler

So... There is a Euler function already defined for Python? Otherwise, in so far as I can tell, that code would have done nothing, and the 'import' would be meaningless. Am I wrong? (More importantly) why?

And, with regards to Java compiling to machine code, I first heard this from one of two Computer Science professors at the college I go to. I recently 'confirmed' the statement by reading it elsewhere (Note: reading <information> on internet != <information> = true, nor does it necessarily validate <information>). I have not personally confirmed this, nor have I really researched into it, as I don't need it yet (I'm still trying to create an abstract layer(s) between the Player 'class' and the methods to move the model...

RE: Everyone;
I know and acknowledge that anything I say can be wrong. I will listen to a person's response and reason for my statement being wrong so long as they don't use the false reason: "Because <non-validated object/entity/reason>." (and think they are right). If what they say makes sense to me and either I validate it on my own, or believe its validity initially, I may abandon my initial belief/statement in favor of the new information. Weblinks help, original sources are even better.
Lee's case is an example of the instance where I appreciate the information.

Finally, Lee, as you seem to be an experienced programmer, in what way would you do the following: I want to eventually create a parser to read the original script files and convert them into classes compatible with my version. Would you A) write the parser first and create the classes as necessary (possibly combining parts into interfaces/abstract superclasses), B) Write the classes to be extended/filled first and then write the parser, or C) other? Anyone else who has an answer, feel free to give it.
Me? Thus far I'm doing (A).

old Re: j(ava)Stranded, the O-O approach

palomino
User Off Offline

Quote
Master_Aricitic has written
Anyway:
Lee has written
1
2
3
4
easy_install euler

# Lee you're a f*cking moron.
import euler

So... There is a Euler function already defined for Python? Otherwise, in so far as I can tell, that code would have done nothing, and the 'import' would be meaningless. Am I wrong? (More importantly) why?

You're completely right. If my VERY poor knowledge of Python doesn't mistaken, by typing "import" you are importing an already defined module. Let's say for example "import pickle", which is used for I/O operations. So you are right.
edited 1×, last 20.03.11 08:48:13 am

old re: JMonkeyEngine Alpha 3

Master_Aricitic
User Off Offline

Quote
I continue to struggle to either learn JMonkeyEngine or get (force) myself to sit down and code a few lines-did this work, no-code a few lines-did This work, no... JMonkeyEngine, while mostly stable, is still in Alpha, meaning pre-beta, meaning pre-stable-release.
Before I opened my big mouth I had meant to create a 2D version to get my basic logic working, and THEN work on a 3D version. Because JMonkeyEngine is still alpha, and because I can't find any stable commercial Java game engines, I am going to go back to my original plan and create a 2D version akin to a game called UnReal World (I'd create a link to it but I litterally JUST got internet back and am unsure how long it will be up-they [the cable company] are 'swapping' servers). Once I get everything satisfactory I'll 'port' it over to a 3D engine-assuming I can find one at the time-and that will be that.
Anyway, I'll still release the 2D version here as I accomplish sutable suitable landmarks.
Again, I apologizes for having to do this run around, but I simply cannot bring myself to brute-force an engine that is still in Alpha...
To the start Previous 1 2 Next To the start
Log in to replyGeneral overviewStranded II overviewForums overview