Search Results

Search found 9 results on 1 pages for 'arvanem'.

Page 1/1 | 1 

  • Java, filling arrays, and inheritance

    - by Arvanem
    Hi folks, I think I'm running into an inheritance conceptual wall with my Java arrays. I'm kind of new to Java so please tell me if I have things upside down. In essence, I want to do three things: Create a runnersArray with the attributes of my Runners class. Fill my runnersArray using my GenerateObjects method of my GenerateObjects class. Access the contents of my filled runnersArray in my Evaluating method of my Evaluating class. The problem seems to be that runnersArray is not visible to the methods in steps 2 and 3 above, but their classes (due to design reasons) cannot inherit or extend Runners class. Thanks in advance for any suggestions. Here are some code snippets showing what I'm trying to do: public class Runners extends Party { Runners[] runnersArray = new Runners[5]; } and public class GenerateObject extends /* certain parent class */ { public GenerateObject (int arrayNum) { runnersArray[arrayNum] = /* certain Runners attributes */; } } and public class Evaluating extends /*certain parent class*/ { public Evaluating (int arrayNum) { System.out.println(/* String cast attribute of runnersArray[arrayNum]*/; } }

    Read the article

  • Object oriented design of game in Java: How to handle a party of NPCs?

    - by Arvanem
    Hi folks, I'm making a very simple 2D RPG in Java. My goal is to do this in as simple code as possible. Stripped down to basics, my class structure at the moment is like this: Physical objects have an x and y dimension. Roaming objects are physical objects that can move(). Humanoid objects are roaming objects that have inventories of GameItems. The Player is a singleton humanoid object that can hire up to 4 NPC Humanoids to join his or her party, and do other actions, such as fight non-humanoid objects. NPC Humanoids can be hired by the Player object to join his or her party, and once hired can fight for the Player. So far I have given the Player class a "party" ArrayList of NPC Humanoids, and the NPC Humanoids class a "hired" Boolean. However, my fight method is clunky, using an if to check the party size before implementing combat, e.g. public class Player extends Humanoids { private ArrayList<Humanoids> party; // GETTERS AND SETTERS for party here //... public void fightEnemy(Enemy eneObj) { if (this.getParty().size() == 0) // Do combat without party issues else if (this.getParty().size() == 1) // Do combat with party of 1 else if (this.getParty().size() == 2) // Do combat with party of 2 // etc. My question is, thinking in object oriented design, am I on the right track to do this in as simple code as possible? Is there a better way?

    Read the article

  • Java and dynamic variables

    - by Arvanem
    Hi folks, I am wondering whether it is possible to make dynamic variables in Java. In other words, variables that change depending on my instructions. FYI, I am making a trading program. A given merchant will have an array of items for sale for various prices. The dynamism I am calling for comes in because each category of items for sale has its own properties. For example, a book item has two properties: int pages, and boolean hardCover. In contrast, a bookmark item has one property, String pattern. Here are skeleton snippets of code so you can see what I am trying to do: public class Merchants extends /* certain parent class */ { // only 10 items for sale to begin with Stock[] itemsForSale = new Stock[10]; // Array holding Merchants public static Merchants[] merchantsArray = new Merchants[maxArrayLength]; // method to fill array of stock goes here } and public class Stock { int stockPrice; int stockQuantity; String stockType; // e.g. book and bookmark // Dynamic variables here, but they should only be invoked depending on stockType int pages; boolean hardCover; String pattern; }

    Read the article

  • Which is better Java programming practice for looping up to an int value: a converted for-each loop

    - by Arvanem
    Hi folks, Given the need to loop up to an arbitrary int value, is it better programming practice to convert the value into an array and for-each the array, or just use a traditional for loop? FYI, I am calculating the number of 5 and 6 results ("hits") in multiple throws of 6-sided dice. My arbitrary int value is the dicePool which represents the number of multiple throws. As I understand it, there are two options: Convert the dicePool into an array and for-each the array: public int calcHits(int dicePool) { int[] dp = new int[dicePool]; for (Integer a : dp) { // call throwDice method } } Use a traditional for loop. public int calcHits(int dicePool) { for (int i = 0; i < dicePool; i++) { // call throwDice method } } I apologise for the poor presentation of the code above (for some reason the code button on the Ask Question page is not doing what it should). My view is that option 1 is clumsy code and involves unnecessary creation of an array, even though the for-each loop is more efficient than the traditional for loop in Option 2. Thanks in advance for any suggestions you might have.

    Read the article

  • How to handle optional variables of an object in Java?

    - by Arvanem
    Hi folks, For my trading program, I have a Merchant class. A given Merchant object may or may not have a particular special quality or bundle of special qualities. For example, one Merchant object may have the Stockbroker quality, another Merchant may have the Financial Services and Stockbroker qualities, and another Merchant may have no special qualities at all. My initial thought was to create a HashMap and a Qualities class as follows: Map<Qualities, Boolean> merchantQualities = new HashMap<Qualities, Boolean>(); The only problem is, there are at least 50 possible special qualities for Merchants, such that it would be extremely tiresome to subclass all the qualities from the Quality class. Is there a better way of coding for these optional special qualities and representing them in the Merchants class than a HashMap and subclassing a Qualities class?

    Read the article

  • Does Java have dynamic variables for class members?

    - by Arvanem
    Hi folks, I am wondering whether it is possible to make dynamic variables in Java. In other words, variables that change depending on my instructions. FYI, I am making a trading program. A given merchant will have an array of items for sale for various prices. The dynamism I am calling for comes in because each category of items for sale has its own properties. For example, a book item has two properties: int pages, and boolean hardCover. In contrast, a bookmark item has one property, String pattern. Here are skeleton snippets of code so you can see what I am trying to do: public class Merchants extends /* certain parent class */ { // only 10 items for sale to begin with Stock[] itemsForSale = new Stock[10]; // Array holding Merchants public static Merchants[] merchantsArray = new Merchants[maxArrayLength]; // method to fill array of stock goes here } and public class Stock { int stockPrice; int stockQuantity; String stockType; // e.g. book and bookmark // Dynamic variables here, but they should only be invoked depending on stockType int pages; boolean hardCover; String pattern; }

    Read the article

  • Any simple Java way to pass parts of a hashmap to another hashmap?

    - by Arvanem
    Hi folks, For my trading program, a Merchant object has a qualities HashMap of enumerated Qualities with Boolean values. public class Merchants { private Map<Qualities, Boolean> qualities = new HashMap<Qualities, Boolean>(); I would like to give each Merchant object a further ratedQualities HashMap of True Qualities with Byte or Integer values representing the Rating of those Qualities. For example, a given Merchant could have a "1" Rating associated with their True Stockbroking Quality. The problem is Java does not let me do this easily. The following code demonstrates my intentions but doesn't compile: private Map<qualities-TRUE, Byte> ratedQualities = new HashMap<qualities-TRUE, Byte>(); According to this link text one solution is to create a Wrapper for the qualities HashMap. Is this the only solution or is there a better way? Thanks in advance for your help.

    Read the article

  • Java OO design confusion: how to handle actions modified by states modified by actions...

    - by Arvanem
    Hi folks, Given an entity, whose action is potentially modified by states (of the entity and other entities) in turn potentially modified by other actions (of the entity and other entities) , what is the best way to code or design to handle the potential existence of the modifiers? Speaking metaphorically, I am coding a Java application representing a piano. As you know a piano has keys (which, when pressed, emit sound) and pedals (which, when pressed, modify the keys' sounds). My base class structure is as follows: Entity (for keys and pedals) State (this holds each entity's states, e.g. name such as "soft pedal", and boolean "Pressed"), Action (this holds each entity's actions, e.g. play sound when pressed, or modify others sounds). By composition, the Entity class has a copy of each of State and Action inside it. e.g.: public class Entity { State entityState = new State(); Action entityAction = new Action(); Thus I have coded a "C-Sharp" key Entity. When I "press" that entity (set its "Pressed" state to true), its action plays a "C-Sharp" sound and then sets its "Pressed" state to false. At the same time, if the "C-Sharp" key entity is not "tuned", its sound deviates from "C-Sharp". Meanwhile I have coded a "soft pedal" Entity. When that entity is "pressed", no sound plays but its action is to make softer the sound of the "C-Sharp" and other key entities. I have also coded a "sustain pedal" Entity. When that entity is "pressed", no sound plays but its action is to enable reverberation of the sound of the "C-Sharp" and other key entities. Both the "soft" and "sustain pedals" can be pressed at the same time with the result that keys entities become both softened and reverberating. In short, I do not understand how to make this simultaneous series of states and actions modify each other in a sensible OO way. I am wary of coding a massive series of "if" statements or "switches". Thanks in advance for any help or links you can offer.

    Read the article

  • Which is better Java programming practice: stacking enums and enum constructors, or subclassing?

    - by Arvanem
    Hi folks, Given a finite number of items which differ in kind, is it better to represent them with stacked enums and enum constructors, or to subclass them? Or is there a better approach altogether? To give you some context, in my small RPG program (which ironically is supposed to be simple), a character has different kinds of items in his or her inventory. Items differ based on their type and use and effect. For example, one item of inventory is a spell scroll called Gremlin that adjusts the Utility attribute. Another item might be a sword called Mort that is used in combat and inflicts damage. In my RPG code, I now have tried two ways of representing inventory items. One way was subclassing (for example, InventoryItem - Spell - AdjustingAttributes; InventoryItem - Weapon - Sword) and instantiating each subclass when needed, and assigning values such as names like Gremlin and Mort. The other way was by stacking enums and enum constructors. For example, I created enums for itemCategory and itemSpellTypes and itemWeaponTypes, and the InventoryItem enum was like this: public enum InventoryItem { GREMLIN(itemType.SPELL, itemSpellTypes.ATTRIBUTE, Attribute.UTILITY), MORT(itemType.WEAPON, itemWeaponTypes.SWORD, 30); InventoryItem(itemType typeOfItem, itemSpellTypes spellType, Attribute attAdjusted) { // snip, enum logic here } InventoryItem(itemType typeOfItem, itemWeaponTypes weaponType, int dmg) { // snip, enum logic here } // and so on, for all the permutations of items. } Is there a better Java programming practice than these two approaches? Or if these are the only ways, which of the two is better? Thanks in advance for your suggestions.

    Read the article

1