Skip to main content
GameDev.net gamedev.net
🔒 Locked

Implementing Objects with specifications

Started by NuLogic Mar 17, 2012 at 7:47 PM 5 replies 1.6k views
Original Post
NuLogic
NuLogic
Hello, I have class Weapon:

class Weapon {
private String name;
private int min_damage;
private int max_damage;
private int weight;

public Weapon(String name, int weight) {
this.name = name;
this.weight = weight;
}

public int getMinDamage() { return min_damage; }
public int getMaxDamage() { return max_damage; }

public void setMinDamage(int x) { min_damage = x; }
public void setMaxDamage(int x) { max_damage = x; }

}


and I want at beginning of the program to create three weapons like longSword, shortSword, saber, each of it would have parameters min_damage, max_damage, name and weight, so how I need to create them? I am thinking about two ways, first:

Weapon longSword = new Weapon("Long Sword", 10);
longSword.setMinDamage(5);
longSword.setMaxDamage(10);


second:
class longSword extends Weapon{
// same actions like upper
}


but I realise, that both methods are wrong, maybe you have any advice for java programming newbie? Thank you in advance
SiddharthBhat
SiddharthBhat
[color=#000088]Why not:
class[color=#000000] [color=#660066]Weapon[color=#000000] [color=#666600]{
[color=#000000] [color=#000088]private[color=#000000] [color=#660066]String[color=#000000] name[color=#666600];
[color=#000000] [color=#000088]private[color=#000000] [color=#000088]int[color=#000000] min_damage[color=#666600];
[color=#000000] [color=#000088]private[color=#000000] [color=#000088]int[color=#000000] max_damage[color=#666600];
[color=#000000] [color=#000088]private[color=#000000] [color=#000088]int[color=#000000] weight[color=#666600];
[color=#000000]
[color=#000088]public[color=#000000] [color=#660066]Weapon[color=#666600]([color=#660066]String[color=#000000] name[color=#666600],[color=#000000] [color=#000088]int[color=#000000] weight, int min_damage, int max_damage[color=#666600])[color=#000000] [color=#666600]{
[color=#000000] [color=#000088]this[color=#666600].[color=#000000]name [color=#666600]=[color=#000000] name[color=#666600];
[color=#000000] [color=#000088]this[color=#666600].[color=#000000]weight [color=#666600]=[color=#000000] weight[color=#666600];
this.min_damage = min_damage;
this.max_damage = max_damage;
[color=#000000] [color=#666600]}
[color=#000000]
[color=#000088]public[color=#000000] [color=#000088]int[color=#000000] getMinDamage[color=#666600]()[color=#000000] [color=#666600]{[color=#000000] [color=#000088]return[color=#000000] min_damage[color=#666600];[color=#000000] [color=#666600]}
[color=#000000] [color=#000088]public[color=#000000] [color=#000088]int[color=#000000] getMaxDamage[color=#666600]()[color=#000000] [color=#666600]{[color=#000000] [color=#000088]return[color=#000000] max_damage[color=#666600];[color=#000000] [color=#666600]}
[color=#000000] [color=#000000]
[color=#666600]}
mickd
mickd
I would go for the first method, but with the modified class bollu suggested.

Are you planning to store the stats of the weapons in a text file? or a database? or an xml file? Seems the first method would work nicely with any of those.
6510
6510
If different swords don't need different attributes or behaviour, do not add separate Java classes for them.
For keeping them apart, for rendering for example, you could use enums instead of strings.
NuLogic
NuLogic
Thank you bollu for fix, mickd, thisis my other question, I noticed, that most of people store data in xml files, but my game will be really small, it will only containt 3 weapons and 3 armors also 2 players (human and computer), so maybe there is a way just to implement them all dinamically while game starting just with Weapon longSword = new Weapon("Long Sword", 10); without storing data somewhere? 6510, abilities of the swords are the same, different only stats, same with armors, maybe you can give any example? I have been searching for it, but I did not find any simple ones. Because it is just my first java game at all and it will be simple for the first time. After finishing I will bring it here as object to learn
6510
6510
Something like this:

public enum WeaponClass {
LONG_SWORD, SHORT_SWORD, MAGIC_SWORD;
}

public class Weapon {
private enum WeaponClass weaponClass;

[color=#000088]public[color=#000000] [color=#660066]Weapon[color=#666600](WeaponClass weaponClass[color=#666600],[color=#000000] [color=#000088]int[color=#000000] weight, int minDamage, int maxDamage[color=#666600])[color=#000000] {
...
}
}

Only if you have use of it...
NuLogic
NuLogic
Thank you very much

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.