Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Need help creating an array method.

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help creating an array method.

    I need help creating an array method for my monster class. The monster is going to have multiple weapons and I need to figure out how to get attack and damage from all of them.

    /**
     * Write a description of class Monster here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.util.Random;
    public class Monster
    {
    String name;
    Backpack pack;
    public int initiative;
    public int hitPoints;
    Purse treasure;
    Weapon[] monsterWeapons;
    Armor monsterArmor;
     
    public Monster(String name, Backpack pack, int initiative, int hitPoints, Purse treasure,
    Weapon[] monsterWeapon, Armor monsterArmor){
        this.pack=pack;
        this.initiative=initiative;
        this.hitPoints=hitPoints;
        this.treasure=treasure;
        this.monsterWeapons=monsterWeapons;
        this.monsterArmor=monsterArmor;
    }
    public int rollForInitiative(){
    int total=0;
    Random rand= new Random ();
    total+=rand.nextInt(20)+1;
    return total;
    }
    public int[] rollWeaponAttacks(){
    int total=0;
    Random rand= new Random ();
    total+=rand.nextInt(20)+1;
    return total;
    }
    public int[] rollWeaponDamage(){
    int total=0;
    Random rand= new Random ();
    total+=rand.nextInt(20)+1;
    return total;
    }
    public int takeDamage(int hitPoints){
    int total=0;
    Random rand= new Random ();
    total+=rand.nextInt(20)+1;
    return total;
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Need help creating an array method.

    What is "an array method"? What does it do? What help do you need creating it?

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating an array method.

    Quote Originally Posted by GregBrannon View Post
    What is "an array method"? What does it do? What help do you need creating it?
    Basically I'm supposed to get a random number between 1-20 but I need it for multiple weapons. One could get 5, another 13, and a third could get 17, etc.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,223
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: Need help creating an array method.

    Before adding any new code to what is posted, you should compile it and fix the errors.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating an array method.

    Quote Originally Posted by Norm View Post
    Before adding any new code to what is posted, you should compile it and fix the errors.
    Yeah, I compiled and apparently it's incompatible. But that's the problem. I first need to figure out how to make it compatible.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,223
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: Need help creating an array method.

    If you need help fixing any errors, copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating an array method.

    It highlight "total" under the WeaponAttack and WeaponDamage methods and says "incompatible types".

  8. #8
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Need help creating an array method.

    Quote Originally Posted by DarkestLord View Post
    It highlight "total" under the WeaponAttack and WeaponDamage methods and says "incompatible types".
    You'll want to paste specific compile- or run-time error messages here. No one is going to want to follow along with you based on your description of gestures you make in your IDE.

    That being said, you want to inspect the method signatures and see what you are telling the compiler some of your methods return, and what you are actually returning.

    What is the purpose, in plain language, of something like "rollWeaponDamage()"? How will client code call this method and use the results? What would it expect?
    Last edited by jdv; October 7th, 2014 at 10:38 AM.

  9. #9
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating an array method.

    Sorry, I'm still pretty new at this. ^^; But...that's all it says. It just highlight "total" under those two and says "compatible types". I think it's because of the [] I added on those ints.

    Anyway, in this case, the monster is going to have two weapons. When I call the method "rollWeaponDamage" and "rollWeaponAttack", I'm going to need a integer between 1-20 for BOTH weapons, which I will display on a MonsterTest class. And I need to do this using an array.

  10. #10
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Need help creating an array method.

    Quote Originally Posted by DarkestLord View Post
    Sorry, I'm still pretty new at this. ^^; But...that's all it says. It just highlight "total" under those two and says "compatible types". I think it's because of the [] I added on those ints.
    And by doing this you changed the method signatures. An int is not the same as an array of ints. Look at the signature, which is where the method promises to return an array of ints. Now look at what you are returning.

    The caller can be changed to take an array of ints, or the callee can be changed to return an int. The calling code can save values from a method returning an int in a local array, if that is what you want to do.

    e.g., "someArrayOfInts[1] = methodThatReturnsAnInt()"

    My advice is to not use raw array types unless you know their sizes in advance, and your constructors or initializers make sure all array values are filled out. You can use a Collection like an ArrayList to manage dynamic array-like entries.

    Quote Originally Posted by DarkestLord View Post
    Anyway, in this case, the monster is going to have two weapons. When I call the method "rollWeaponDamage" and "rollWeaponAttack", I'm going to need a integer between 1-20 for BOTH weapons, which I will display on a MonsterTest class. And I need to do this using an array.
    Well, why not call the method twice when initializing a weapon state. Weapon should be a class, right? And it needs to be constructed such that it is "complete", since a Weapon instance should not have a damage without an attack value.

    So call methods that generate your (weighted or not) values for all the properties of the Weapon (or Race, or Armor, or ...) until those properties are set and the object is complete.

  11. #11
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help creating an array method.

    Yeah, calling it twice would be the simple solution but...this is an assignment and it asks for me to make an array method that allows me to use as many weapons as the user wants. The test would just require two.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,223
    Thanks
    53
    Thanked 2,385 Times in 2,339 Posts

    Default Re: Need help creating an array method.

    an array method
    What is an "array method"? Is it:
    A method that takes an array as a parameter
    and/or a method that returns an array?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating a method
    By jennocide in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2014, 01:27 AM
  2. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  3. Creating method to print array of integers
    By kferd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2014, 08:14 PM
  4. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  5. Need help with creating method for Array.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 30
    Last Post: March 24th, 2013, 06:12 PM