Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Implementing Objects with specifications


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 March 2012 - 01:47 PM

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

Sponsor:

#2 bollµ   Members   -  Reputation: 354

Like
2Likes
Like

Posted 17 March 2012 - 11:22 PM

Why not:
class
Weapon {
private String name;
private int min_damage;
private int max_damage;
private int weight;

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

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

}

a WIP 2d game engine: https://code.google.com/p/modulusengine/

English is not my first language, so do feel free to correct me :)


#3 mickd   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 March 2012 - 04:15 AM

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.

#4 6510   Members   -  Reputation: 151

Like
0Likes
Like

Posted 18 March 2012 - 05:05 AM

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.

#5 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 March 2012 - 05:08 AM

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

#6 6510   Members   -  Reputation: 151

Like
1Likes
Like

Posted 18 March 2012 - 05:38 AM

Something like this:

public enum WeaponClass {
LONG_SWORD, SHORT_SWORD, MAGIC_SWORD;
}

public class Weapon {
private enum WeaponClass weaponClass;

public Weapon(WeaponClass weaponClass, int weight, int minDamage, int maxDamage) {
...
}
}

Only if you have use of it...

#7 NuLogic   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 March 2012 - 05:42 AM

Thank you very much :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS