Results 1 to 5 of 5
  1. #1
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default Multiple Questions

    My Java teacher isn't much of a teacher, he is more of a baby-sitter. Therefore, I am forced to learn Java by a book and what I can scavenge off other people (Only 2 other students know Java well).

    So, to help me learn, I've been attempting to write a big program that will help me learn Java by putting it into something I can relate to. I have decided that will be a battlefield program: two sides with certain numbers of units fighting it out.

    My first question:
    How would I tell Java to create multiple instances of the same class, but each with a unique, but predictable name? (soldier1, soldier2, soldier3). So, If i want 25 soldiers, they would each have a name, soldier0 though soldier24.

    Next:
    For simplicity, Lets say I have 2 classes: Soldier and Sniper. Each one has its own health, armor, attack, accuracy, etc. When I try to use those variables in the "battlefield" class (where all the math and prompting and such will take place), it cannot find them. How do I use a non-local variable? Example: Soldier has health 4. Sniper has health 2. Each of those are defined in their own classes. In Battlefield class, I want to display the difference. So, I try math.abs(SoldierHP - SniperHP), but Eclipse cannot find SoldierHP and SniperHP.

    Next:
    I have four types of units (Soldiers, Commanders, Artillery, and Aircraft), and each will be able to under-go specialization. So, 4 types times 4 specializations is a total of 16 different types of units. Which would be the best way to create all of these? Methods inside of the "base unit"? Enumerated Types? Just a different class for each?

    Thats all I can think of for now...
    If any of these could be answered, that'd be of great help.
    Thank you,
    ~Kaito

    EDIT:
    A couple more questions that I cant seem to get a clear answer on...

    Public Static Void main (String[] args)
    that line is the main method for each of my school projects.
    What does each of those keywords (Public, Static, Void, String[], Args) mean? I cant get a clear answer
    Last edited by Kaito; 10-07-2009 at 06:16 AM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    To answer your first question, arrays! ARRAYS!

    So, let's say you have 25 ints. You can use soldiers, too, but for my example I'll use ints.

    Java Code:
    int num[] = new int[25]; //Array of 25 ints
    //runs through each int (0 to 24)
    //num.length returns the size (25) of the array.
    for(int i = 0; i < num.length; i++) 
    {
    num[i] = i; //Assigns them to the value of i
    }
    For your next question, I'm not sure exactly what you're asking. If you have a class Sniper, create an accessor method within it to obtain the HP. For example,

    Java Code:
    public int getHP()
    {
    return HP;
    }
    Then, in your other class you can call this method to get the HP.
    Java Code:
    Sniper aSniper = new Sniper();
    int a = aSniper.getHP();
    As for your next question, try using inheritance. You may want to create a class called Unit. Then, Soldiers, Commanders, Artillery, and Aircraft will extend Unit, therefore inheriting methods.
    Java Code:
    public class Soldiers extends Unit
    {
     public Soldiers()
     {
      super();
     }
    }
    Public means another class has access to it, while private would indicate it is hidden from other classes. Protected is also used in a similar way.
    Static means it can be called without creating a reference to that class. For example, when you call Math.abs(), that is static because you do not have a line like Math math = new Math();
    Void is the a return type that means it does not return anything

    String[] Args Honestly, i never learned what this means. I just accept it as what it is, for now. Though, it is an array of Strings called Args... I guess.


    I was unclear exactly what you were asking for some of your questions, but hopefully I was of some help.
    Last edited by AndrewM16921; 10-07-2009 at 06:33 AM.

  3. #3
    Kaito is offline Member
    Join Date
    Sep 2009
    Posts
    10
    Rep Power
    0

    Default

    Awesome, this is EXACTLY what I was looking for. Thank you very much.
    I'm still trying to get my head to think correctly, but that will come with time. Thank you

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Glad I could help. ^_^

    Also, when you get the hang of arrays you may want to look into ArrayList (java.util.ArrayList). It works similar to arrays, but with some more functionality.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    More specifically, use arrays when you know in advance how many items you need to store and that number won't change. Use ArrayLists when you don't know how many items you need to store, or the number of items will change (addition or deletion).

Similar Threads

  1. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 05:01 PM
  2. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 02:13 AM
  3. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 09:08 PM
  4. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 07:21 PM
  5. questions
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-28-2007, 12:18 AM

Posting Permissions

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