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.
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.
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.
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
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.
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,
int num = new int; //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
Then, in your other class you can call this method to get the HP.
public int 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.
Sniper aSniper = new Sniper();
int a = aSniper.getHP();
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.
public class Soldiers extends Unit
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.
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
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.
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).