February 6th, 2013, 08:16 PM
Class organization and communicating with variables?
Hey guys, I'm really new to programming, so please forgive me if my terms aren't accurate, and if this is a silly question. I'm also not sure what information you guys need exactly to answer my question, so I'll try and be specific.
At this point, I feel like I have a handle on the most basic elements of Java (control flow statements, arrays, creating objects exc...) but I think I'm missing fundamentals on how Java is meant to work, and how it's supposed to be organized.
So yesterday I wanted to try and utilize what I've learned so far by creating a very basic turn based, text based, role playing game battle. I quickly realized I don't have much of a handle on when to create a separate class, when to make a separate method, and how to efficiently have those classes communicate.
The way I tried to organize my classes was this: I set up 3 classes. One class for enemies, one class for the player character, and the main class for the battle.
I got stuck really early on. I wanted the program to start with a message saying "You have engaged insert enemy name in battle."
So in my enemies class, I had a method set up for the enemy, with a string that contained it's name (which is "slime").
So in my battle message, I tried to get it to show that string, "You have engaged slime in battle."
I could only get the string to display, if I had the string outside of the method, and directly within the scope of the enemies class. I tried everything I could think of, and did some research (without knowing exactly what I was trying to look up), to get the battle class to show the slime string from within the method.
Now I'm wondering if I'm misunderstanding the point of a method. Would the method itself need to execute code to display the string? In which case, are methods not intended to be data only?
If that's the case, how would I need to organize classes for a larger role playing game, with many enemies? Would each enemy then have to be it's own class? Essentially, prior to now, I've been thinking of methods as sub-classes.
Ugh, sorry for the lengthy post. Maybe a better question is - where should a newbie go to learn Java from the ground up? I've been using youtube tutorials, and just got "thinking in Java" by Bruce Eckel (which as it turns out, is slightly over my head in places).
Thanks for any help you can give me .
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!