February 6th, 2013, 09: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 .
February 9th, 2013, 01:49 PM
Re: Class organization and communicating with variables?
No because in that case a class would be just like an ordinary data record. In Object Oriented programming (the programming paradigm of Java) a method is supposed to do something. That could mean it accepts or returns data but it's definately wrong approach to use methods as mere data holders.
Originally Posted by bustthenotes
The problem is that in many Java tutorials when a class is introduced it's immediately littered with zillions of setter/getter methods. It's easy to get the impression this is what methods are for. But, as I said, the main usage of metods in OO is not to faciliate data shuffling in and out of objects, it's to do something.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!