More Java Help!
Quaunaut
The longest seven days in history... Join Date: 2003-03-21 Member: 14759Members, Constellation, Reinforced - Shadow
in Off-Topic
<div class="IPBDescription">Ohnoes!</div> Okay. So Java. Lalalala.
See, I'm still doing this text RPG. But I'm wanting to reference things from a class to create another class: Essentially, two classes will add together their values to create the final value. Example:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->class person {
int age, height;
}
class shoes {
int age, height;
}
class last {
int age, height;
age = person.age + shoes.age; // This is the part that won't work
height = person.height + shoes.height; // This too
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
If you don't understand, ask. But I hope this shows what I'm trying to do.
See, I'm still doing this text RPG. But I'm wanting to reference things from a class to create another class: Essentially, two classes will add together their values to create the final value. Example:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->class person {
int age, height;
}
class shoes {
int age, height;
}
class last {
int age, height;
age = person.age + shoes.age; // This is the part that won't work
height = person.height + shoes.height; // This too
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
If you don't understand, ask. But I hope this shows what I'm trying to do.
Comments
See, I'm still doing this text RPG. But I'm wanting to reference things from a class to create another class: Essentially, two classes will add together their values to create the final value. Example:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->class person {
int age, height;
}
class shoes {
int age, height;
}
class last {
int age, height;
age = person.age + shoes.age; // This is the part that won't work
height = person.height + shoes.height; // This too
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
If you don't understand, ask. But I hope this shows what I'm trying to do. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Are the int variables all non-static, as in the code example you gave? If so, making a call to person.age would give problems. There's a difference between an object of type person having a variable of data that applies to it, vs. having a variable of data shared statically amongst all objects of type person that you have instantiated.
If you want the value to belong to the class, but not be object-specific, you need to use the keyword "static." It's what lets you use classname.whatever instead of nameOfSomeObject.whatever. Example: the Math class has plenty of static things, like an absolute value function abs(). You can call it with Math.abs(integerValueHere); you don't need to bother doing "Math myMathObject = new Math(); to create an object of type math, and then myMathObject.abs() to use it.
Making an int static is done by adding static in front of it, like
public static int whatever;
or
private static int whatever;
I am also a nub at Java but I always made my variables static....
and shouldn't we be poking him to be ussing get/set also? <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
I am also a nub at Java but I always made my variables static....
and shouldn't we be poking him to be ussing get/set also? <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Making get/set methods to work with variables is the more professional way to do them, and has many advantages, but it's not required. The way Quaunaut is using is still possible, though it'll make bugfixing harder if he starts making the program complicated <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
It's often very useful to make objects with non-static variables. Let's say I make a class called Automobile. It's got an int variable called doors. I don't want that to be static. I want to be able to make one automobile object have 2 doors, and another automobile object have 4. It's not an equal quality shared amongst all Automobiles, and if I change it for one Automobile object, I don't want to simultaneously change it for all the others too.
Ah, wait a minute...you're saying you want the <i>call</i> to be non-specific, but you <i>do</i> intend on having the different person objects have different values in an int variable?
In that case, don't do what I told you earlier about making them static. Sorry.
Ah, wait a minute...you're saying you want the <i>call</i> to be non-specific, but you <i>do</i> intend on having the different person objects have different values in an int variable?
In that case, don't do what I told you earlier about making them static. Sorry. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
I know. I know what static means XD
So....any way to do this?
person x = new Person();
x.age = 10;
thats the object way, you make an instance then set a variable of it to that amount. you could pass a person to the last's instance creation.
i assume youre doing some work in the main function. within main you can instanciate all three things, and pass the references to people and shoes for the last to make use of.
this may be beyond you, and i can explain more when i;m not in class later...
Their public.
But how would I reference to another classes' property's for any object?
But is there a way I could *not* be object specific, and bring up that object's properties? D:
/me runs off for the moment, likely to come back in an hour or less
int strength;
int dexterity;
int agility;
int intelligence;
int wisdom;
int charisma;
int hp;
int mp;
int cuhp;
int cump;
String name;
String weaponname;
basepcnpc() {
}
basepcnpc(int s, int d, int a, int i, int w, int c, int h, int m, String n){
strength=s;
dexterity=d;
agility=a;
intelligence=i;
wisdom=w;
charisma=c;
hp=h;
mp=m;
name=n;
}
}
class weapon {
int strength = 3;
int weapondamage;
String weaponattribute;
}
class pcnpc {
String combatant1 = "player";
int str, dex, agi, Int, wis, cha, xhp, xmp, chp, cmp;
int getStr() {
int a = player.strength + weapon.strength;
return a;
}
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
This is the exact context I'm using it in. Everything else is later- I'm just trying to get pcnpc to add the strength value of weapon with the one of basepcnpc.
In the case of player, it's not the name of a class you've made (or at least not one you've shown us), and it's not the name of an object you've created (or at least you haven't shown us where you did create it). Kinda hard to call player.strength when player doesn't exist <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
class pcnpc {
String combatant1 = "player";
int str, dex, agi, Int, wis, cha, xhp, xmp, chp, cmp;
int getStr() {
int a = combatant1.strength + weapon.strength;
return a;
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
And have the player.strength and weapon.strength referring to whatever object I choose, dynamic on say, combatant1?
I understand, but it's string is "player", and I wish that would be displayed there. Could I do that at all?
Yeah, thats where the name is from. This is easy for me, I guess.
And I have instances of them. I'm having trouble adding them together to make the properties for pcnpc.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->import javax.swing.JOptionPane;
class basepcnpc {
int strength;
int dexterity;
int agility;
int intelligence;
int wisdom;
int charisma;
int hp;
int mp;
int cuhp;
int cump;
String name;
String weaponname;
basepcnpc() {
}
basepcnpc(int s, int d, int a, int i, int w, int c, int h, int m, String n){
strength=s;
dexterity=d;
agility=a;
intelligence=i;
wisdom=w;
charisma=c;
hp=h;
mp=m;
name=n;
}
}
class weapon {
int strength = 3;
int weapondamage;
String weaponattribute;
}
class pcnpc {
String combatant1 = "player";
int str, dex, agi, Int, wis, cha, xhp, xmp, chp, cmp;
int getStr() {
int a = player.strength + weapon.strength;
return a;
}
}
class rpg {
public static void main(String args[]) {
/*sdaiwchm*/weapon sword = new weapon();
weapon axe = new weapon();
weapon club = new weapon();
basepcnpc player = new basepcnpc();
basepcnpc imp = new basepcnpc(3,1,5,2,1,1,45,0,"imp");
imp.weaponname = "Claw";
// Character creation system.
for(int choicepoints=30;choicepoints>0;choicepoints--){
String Choices;
int choices;
Choices = JOptionPane.showInputDialog("Choose what to put your attribute points into?"+
"\n 1. Strength : "+player.strength+
"\n 2. Dexterity : "+player.dexterity+
"\n 3. Agility : "+player.agility+
"\n 4. Intelligence : "+player.intelligence+
"\n 5. Wisdom : "+player.wisdom+
"\n 6. Charisma : "+player.charisma+
"\n Choose by number. You have "+choicepoints+" points left to put into attributes."+
"\n Just remember to put at least a few points into everything, or you may find your character...limited.");
choices = Integer.parseInt(Choices);
if(choices==1)player.strength++;
if(choices==2)player.dexterity++;
if(choices==3)player.agility++;
if(choices==4)player.intelligence++;
if(choices==5)player.wisdom++;
if(choices==6)player.charisma++;
player.weaponname = JOptionPane.showInputDialog("What kind of weapon do you use?\n 1. Sword \n 2. Club \n 3. Axe");
if(player.weaponname=="1")player.weaponname= "Sword";
if(player.weaponname=="2")player.weaponname= "Club";
if(player.weaponname=="3")player.weaponname = "Axe";
}
player.hp=(player.strength*5)+20;
player.mp=(player.intelligence*5)+10;
System.out.println(player.name+"'s Character Sheet:");
System.out.println();
System.out.println("Health: "+player.hp);
System.out.println("Mana: "+player.mp);
System.out.println();
System.out.println("Strength: "+player.strength);
System.out.println("Dexterity: "+player.dexterity);
System.out.println("Agility: "+player.agility);
System.out.println("Intelligence: "+player.intelligence);
System.out.println("Wisdom: "+player.wisdom);
System.out.println("Charisma: "+player.charisma);
System.out.println("An Imp spawns behind you!");
for(;player.hp!=0||imp.hp!=0;){
String Battlechoice =JOptionPane.showInputDialog("The Imp Spawned Behind you. What do you do?\n 1. Attack \n 2. Lightning \n 3. Heal");
int battlechoice;
battlechoice = Integer.parseInt(Battlechoice);
if(battlechoice==1);
if(battlechoice==2);
if(battlechoice==3);
String combatant1 = "player";
}
System.exit(0);
}
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
This is all of it: The battle system does nothing, atm.
I love Java. (Gonna move onto animation next. Eeeek.)
PlayerCharacter (Weapon w, Hat h, Shield s, Etc e)
{
myWeapon = w;
myHat = h;
myShield = s;
myEtc = e;
}
The PlayerCharacter class would contain methods to change these values as necessary Also, if the player always starts with certain items ( or certain items based on class, these could be constructors as well)
PlayerCharacter(PlayerClass pc)
{
if(pc.class == "wizard")
{
myWeapon = new Weapon(//Whatever value creates a wand);
myArmor = new Armor(//Robes);
myEtc = // Initialize other objects with the default values;
}
else if(pc.class == "warrior")
{
myWeapon = new Weapon(// Sword);
// Etcera, etcera, (Note: == does not work when comparing strings, use equals() method
}
}
equipWeapon(Weapon w)
{
// Remove equipped weapon if weapon is not bare hands, add to inventory
if(myWeapon.valuethatindicatesweapontype != valueofbarehands)
myInventory.add(myWeapon);
myWeapon = w;
// If w was in inventory, it must be removed, giving each item a unique value on creation with a static int stored in the item's class would be good for finding and removing it from the inventory
}
I think it would be a good idea for the PlayerCharacter class to contain Equipped Items, Inventory, Environment (Having it be able to reference it's own location in the game environment could be useful.)
public class BasicCharacterTemplate
{
int strength;
int dexterity;
int agility;
int intelligence;
int wisdom;
int charisma;
int hitpoints;
int magicpoints;
int maxiumhitpoints;
int maxiummagicpoints;
String myName;
int attackStrength;
public BasicCharacterTemplate(int str, int dex, int agi, int intel, int wis, int cha, int hp, int mp, int maxhp, int maxmp, int name)
{
// initialize all of the class variables
attackStrength = calculateAttackStr();
}
// Basic formula for calculating attack power, can be overriden in other classes
public int calculateAttackStr();
{
return myStrength();
}
public int myStrength()
{
return strength;
}
// Methods to change variables and return variables, generally most the above variables would be declared private and would have public methods that return them, this is to prevent the accidental modification of important variables
}
// PlayerCharacter is based on BasicCharacterTemplate
public class PlayerCharacter extends BasicCharacterTemplate
{
Weapon myWeapon;
Inventory myInventory;
Shield myShield;
Helm myHelm;
Etcera myEtc;
public PlayerCharacter(int str, int dex, int agi, int intel, int wis, int cha, int hp, int mp, int maxhp, int maxmp, int name, Weapon w, Inventory i, Shield s, Helm h, Etcera etc)
{
// Initializes the variables from the BasicCharacterTemplate with call to super class
super(int str, int dex, int agi, int intel, int wis, int cha, int hp, int mp, int maxhp, int maxmp, int name)
// Initializes the variables unique to PlayerCharacter
myInventory = i;
myShield = s;
myHelm = h;
myEtc = e;
myWeapon = w;
}
// Example method equipping a weapon, is private because it would be called by another method telling it what weapon to equip. (Ex: Game reads Player input telling it to equip weapon x from player inventory if x is in the inventory, it is removeds and equipWeapon is called with the weapon as a parameter.
private void equipWeapon(Weapon w)
{
// isHands would be whatever method you choose to check if weapon in use is bare hands ! means that if isHands returns false, then proceed with action
enclosed in if statement, add would be an Inventory method that adds items to inventory
if(!myWeapon.isHands())
myInventory.add(myWeapon);
myWeapon = w;
}
// Overrides basic calculateAttackStrength method in BasicCharacterTemplate, weapon damage is added in
public int calculateAttackStrength()
{
// myStrength is a method that from BasicCharacterTemplate that returns value of Str, AttackPower is a method from the Weapon Class that returns attack power
return myStrength() + myWeapon.AttackPower()
}
// Manages the combat situation
public class CombatSimulator
{
// A method that retuns the damage dealt by an attack, a helper method to a public method, (Ex: Player hits monster, call Monster.changeHitPointValue(calculateDamage(player.calculateAttackStrength(), monster.calculateDefenseStrength)
private int calculateDamage(int attackerPower, int defensePower)
{
return attackerPower - defensePower;
}
}
All sorts of additional variables could be added in to add flavor, but for simplicities sake these are just basic examples. (Ex: calculateDamage(attackPower, attackElement, defensePower, defenseElement))
Also, it would help make your code more readable if you followed proper method, variable and class name guidelines.
Class Names: PlayerCharacter, Mouse, MouseHouse
Variable and Method Names: playerCharacter, mouse, mouseHouse
And remember, comments are good, and lengths of the names of methods, classes and variables don't effect the speed at which a compiled program runs.