Recently, well around 2 weeks ago, I started learning java for an internship that I have received. Due to my experience in C++, C, Python and Ruby I have been able to pick up Java fairly quickly. I also have a heavy competitive gaming background, so I figured one of the best ways to help me learn about using a strictly class driven language would be to try to make a text based game.
I have just started it so I do not have a lot complete but I have run into a couple things that I would like cleared up.
First being Nested Class's:
If I have a Character class that for now consist of a Character name, a private int Char_health and 3 objects of a different class called level_class (Such a creative name I know ^^). For visual purposes I will post below.
public class Character {
private String char_name = null;
level_class char_attack = new level_class("Attack");
level_class char_strength = new level_class("Strength");
level_class char_defence = new level_class("Defence");
private int char_health = 10;
public Character(String Name){
char_name = Name;
}
}
Level Class:
public class level_class {
private String Skill;
private int lvl;
private int xp;
private int xp_to_go;
public level_class(String skill_name){
Skill = skill_name;
lvl = 1;
xp = 0;
get_xp_to_go(lvl);
}
public int get_lvl(){
return lvl;
}
public int get_xp(){
return xp;
}
public int get_xptogo(){
return xp_to_go;
}
void xp_gained( int xp_gain){
if ( lvl == 50){
xp += xp_gain;
}
else {
if ( xp_gain < xp_to_go){
xp += xp_gain;
xp_to_go -= xp_gain;
}
else if (xp_gain > xp_to_go){
int temp = xp_gain - xp_to_go;
xp += xp_to_go;
lvl += 1;
System.out.println("Congrats you just reached level" + lvl + " in your" + Skill +" skill");
get_xp_to_go(lvl);
xp_gained(temp);
}
else {
xp += xp_to_go;
lvl += 1;
System.out.println("Congrats you just reached level" + lvl + " in your" + Skill +" skill");
}
}
}
private void get_xp_to_go( int level){
switch(level){
case 1: xp_to_go = 83; break;
case 2: xp_to_go = 91; break;
case 3: xp_to_go = 102; break;
case 4: xp_to_go = 112; break;
case 5: xp_to_go = 124; break;
case 6: xp_to_go = 138; break;
case 7: xp_to_go = 151; break;
case 8: xp_to_go = 168; break;
case 9: xp_to_go = 185; break;
case 10: xp_to_go = 204; break;
case 11: xp_to_go = 226; break;
case 12: xp_to_go = 249; break;
case 13: xp_to_go = 274; break;
case 14: xp_to_go = 304; break;
case 15: xp_to_go = 335; break;
case 16: xp_to_go = 369; break;
case 17: xp_to_go = 408; break;
case 18: xp_to_go = 450; break;
case 19: xp_to_go = 497; break;
case 20: xp_to_go = 548; break;
case 21: xp_to_go = 606; break;
case 22: xp_to_go = 667; break;
case 23: xp_to_go = 737; break;
case 24: xp_to_go = 814; break;
case 25: xp_to_go = 898; break;
case 26: xp_to_go = 990; break;
case 27: xp_to_go = 1094; break;
case 28: xp_to_go = 1207; break;
case 29: xp_to_go = 1332; break;
case 30: xp_to_go = 1470; break;
case 31: xp_to_go = 1623; break;
case 32: xp_to_go = 1791; break;
case 33: xp_to_go = 1977; break;
case 34: xp_to_go = 2182; break;
case 35: xp_to_go = 2409; break;
case 36: xp_to_go = 2658; break;
case 37: xp_to_go = 2935; break;
case 38: xp_to_go = 3240; break;
case 39: xp_to_go = 3576; break;
case 40: xp_to_go = 3947; break;
case 41: xp_to_go = 4358; break;
case 42: xp_to_go = 4810; break;
case 43: xp_to_go = 5310; break;
case 44: xp_to_go = 5863; break;
case 45: xp_to_go = 6471; break;
case 46: xp_to_go = 7144; break;
case 47: xp_to_go = 7887; break;
case 48: xp_to_go = 8707; break;
case 49: xp_to_go = 9612; break;
case 50: xp_to_go = 0; break;
}
}
}
Right now its all very basic and will most likely change as I continue to work on it.
Since I can not see any other class aside from Character using level_class should I nest it? Does that make it easier to access certain values?
On top of that if I want to access variables within an object in an object do I have to make get functions in each class?
Like so...
public void get_skill_stats( level_class char_attack){
System.out.println("Your" + char_strength.get_Skill() + " lvl is " + char_strength.get_lvl());
System.out.println("Your" + char_strength.get_Skill() + " xp is " + char_strength.get_xp());
System.out.println("Your" + char_strength.get_Skill() + " xp_to_go is " + char_strength.get_xptogo());
}
or is there a much easier way of accessing them
Summary:
1: What is the benefit from nesting a class versus having it in a different file in the same package.
2: What is the easiest way/syntax to grab this values if lets say I was calling the Character class from a different file called Main.
Btw, as I said I am still learning Java so if I am doing something a wrong way and there is a much easier/clean one then please let me know, Thanks!


Sign In
Create Account


Back to top









