CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2013
    Posts
    1

    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 .

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Class organization and communicating with variables?

    Quote Originally Posted by bustthenotes View Post
    In which case, are methods not intended to be data only?
    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.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured