Jump to content
  • Advertisement
Sign in to follow this  
NuLogic

Implementing Objects with specifications

This topic is 2403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Advertisement
[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]}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!