PDA

View Full Version : Question about Java [Solved]



Yarram
2010-03-29, 08:08 PM
Note: Please Don't give me any source code, as that would be cheating.

I'm writing game for uni, without a GUI, which involves moving around an XY axis and eating food, while dodging stationary enemies that will kill you if you move within one square of them. Despite the fact that I can make the program perform perfectly, down to the last character, the universities marking system won't accept my assignment due to using different ways of achieving the same goal to what they want.
I'm not going to post my source, because that's cheating, but I will describe my problem.
I'm required to have 6 classes, Main, Game, UI, Player, Enemy, Food. Food, Player and Enemy objects all have X and Y variables.
I'm required to set these variables within the Game() constructor, which would be fine, but is stuffing me up, because I'm also not allowed to use arguments.
Is there a way of either creating an object within another object, and then consistently accessing it with other methods that the original object contains, or is there a way of calling upon the inside objects from within main(), or is there a way of accessing objects without requiring arguments?

Further Note: I'm not allowed to use "static," "inheritance," or any complex library methods because they're liek: "No you can NOT use prior knowledge to complete this assignment!"

valadil
2010-03-29, 09:06 PM
Could your food, player, and enemy classes have setX and setY public methods that can alter the private variables? It sounds to me like that's what they're trying to teach unless I'm misunderstanding the requirements.

Yarram
2010-03-29, 09:14 PM
Edit: Whoa. I totally misread what you just said.
The problem wasn't the methods, it was creating objects within another object, and then I realised where I could do it.
I was trying to create new objects inside the constructor (Which would disappear when the constructor method ended), but I just realised that I could create them at the same time as creating the variables for the class. :smallbiggrin::smallredface:
Thank goodness that's done. Huzzah. Now all I need to do is wait till the end of the semester for the next project =P.