Question

I'm trying to implement a rest() method for my GameCharacter where if a random number between...

I'm trying to implement a rest() method for my GameCharacter where if a random number between 0-2 is generated then one or the other character rests and gains back about 5 to 10 health points. I don't know if I'm on the right track.

Here's my code so far and the rest() method is located at the bottom:

import java.util.Objects;
import java.util.Random;

/**
 * this class represents a class for role playing game.
 */
public class GameCharacter {

    private String mName;
    private int mLevel;
    private int mHealthPoints;
    private int mMagic;
    private int mGold;

    /**
     * Constructor that creates a new character given its name.
     * @param name The name of the character.
     */
    public GameCharacter(String name) {
        this(name, 1);
    }

    /**
     *
     * @param name
     * @param level
     */
    public GameCharacter(String name, int level) {
        mName = name;
        mLevel = level;
        mHealthPoints = 100 * mLevel;
        mMagic = 100 * mLevel;
        mGold = 100 * mLevel;
    }

    /**
     *
     * @return
     */
    public String getName() {

        return mName;
    }

    /**
     *
     * @return
     */
    public int getLevel() {

        return mLevel;
    }

    /**
     *
     * @return
     */
    public int getHealthPoints() {

        return mHealthPoints;
    }

    /**
     *
     * @return
     */
    public int getMagic() {

        return mMagic;
    }

    /**
     *
     * @return
     */
    public int getGold() {

        return mGold;
    }

    /**
     *
     * @param name
     */
    public void setName(String name) {

        mName = name;
    }

    /**
     *
     * @param o
     * @return
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        GameCharacter that = (GameCharacter) o;
        return mLevel == that.mLevel &&
                mHealthPoints == that.mHealthPoints &&
                mMagic == that.mMagic &&
                mGold == that.mGold &&
                mName.equals(that.mName);
    }

    /**
     *
     * @return
     */
    @Override
    public int hashCode() {

        return Objects.hash(mName, mLevel, mHealthPoints, mMagic, mGold);
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return "GameCharacter{" +
                "Name ='" + mName + '\'' +
                ", Level = " + mLevel +
                ", Health Points = " + mHealthPoints +
                ", Magic = " + mMagic +
                ", Gold = " + mGold +
                '}';
    }

    /**
     *
     * @param other
     */
    public void attack(GameCharacter other) {

        //this = this character(attacking)
        //other = second character (one being attacked)

        //Generate a random number from 0 to 9
        Random rng= new Random();
        int roll = rng.nextInt(10);

        //Calculate damage = roll * level
        int damage = roll * this.mLevel;

        // other character receives the damage
        other.mHealthPoints -= damage;
        //if other character's health goes negative,
        //make it zero
        if(other.mHealthPoints < 0)
            other.mHealthPoints = 0;
    }

    /**
     *
     * @param other
     */
    public void assist(GameCharacter other) {

        Random rng = new Random();
        int roll = rng.nextInt(5);

        switch(roll) {

                case 0:
                    //other character receives health
                    //this character loses health
                    other.mHealthPoints += 5 * this.mLevel;
                    this.mHealthPoints -= 5 * this.mLevel;
                    break;

                case 1:
                    other.mMagic += 5 * this.mLevel;
                    this.mMagic -= 5 * this.mLevel;
                    break;

                case 2:
                    other.mGold += 5 * this.mLevel;
                    this.mGold -= 5 * this.mLevel;
                    break;

                case 3:
                    other.mLevel++;
                    other.mGold += 100;
                    other.mHealthPoints += 100;
                    other.mMagic += 100;
                    this.mLevel--;
                    this.mGold -= 100;
                    this.mHealthPoints -= 100;
                    this.mMagic -= 100;

                case 4:
                    other.mHealthPoints += 100 * mLevel;
                    this.mHealthPoints += 100 * mLevel;
                    break;
            }
        }

        public void rest(GameCharacter other) {

            Random rng = new Random();
            int roll = rng.nextInt(2);

            if (this.mHealthPoints == 100 * mLevel || other.mHealthPoints == 100 * mLevel)
                mHealthPoints = 100 * mLevel;

            else if (this.mHealthPoints < 100 * mLevel || other.mHealthPoints < 100 * mLevel)
                mHealthPoints += 5;
        }
    }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Objects;

import java.util.Random;

/**

* this class represents a class for role playing game.

*/

public class GameCharacter {

      

    private String mName;

    private int mLevel;

    private int mHealthPoints;

    private int mMagic;

    private int mGold;

   

    /**

     * Constructor that creates a new character given its name.

     * @param name The name of the character.

     */

    public GameCharacter(String name) {

        this(name, 1);

    }

   

    /**

    *

    * @param name

    * @param level

    */

    public GameCharacter(String name, int level) {

       mName = name;

       mLevel = level;

       mHealthPoints = 100 * mLevel;

       mMagic = 100 * mLevel;

       mGold = 100 * mLevel;

    }

  

          /**

          *

          * @return

          */

       public String getName() {

      

             return mName;

       }

       /**

          *

          * @return

          */

       public int getLevel() {

            

             return mLevel;

       }

       /**

          *

          * @return

          */

       public int getHealthPoints() {

             return mHealthPoints;

       }

      

       /**

            *

            * @return

            */

           public int getMagic() {

               return mMagic;

           }

           /**

            *

            * @return

            */

           public int getGold() {

               return mGold;

           }

          

           /**

            *

            * @param name

            */

           public void setName(String name) {

               mName = name;

           }

           /**

            *

            * @param o

            * @return

            */

           @Override

           public boolean equals(Object o) {

               if (this == o) return true;

               if (o == null || getClass() != o.getClass()) return false;

               GameCharacter that = (GameCharacter) o;

               return mLevel == that.mLevel &&

                       mHealthPoints == that.mHealthPoints &&

                       mMagic == that.mMagic &&

                       mGold == that.mGold &&

                       mName.equals(that.mName);

           }

          

           /**

            *

            * @return

            */

           @Override

           public int hashCode() {

               return Objects.hash(mName, mLevel, mHealthPoints, mMagic, mGold);

           }

           /**

            *

            * @return

            */

           @Override

           public String toString() {

               return "GameCharacter{" +

                       "Name ='" + mName + '\'' +

                       ", Level = " + mLevel +

                       ", Health Points = " + mHealthPoints +

                       ", Magic = " + mMagic +

                       ", Gold = " + mGold +

                       '}';

           }

          

           /**

            *

            * @param other

            */

           public void attack(GameCharacter other) {

               //this = this character(attacking)

               //other = second character (one being attacked)

               //Generate a random number from 0 to 9

               Random rng= new Random();

               int roll = rng.nextInt(10);

               //Calculate damage = roll * level

               int damage = roll * this.mLevel;

               // other character receives the damage

               other.mHealthPoints -= damage;

               //if other character's health goes negative,

             //make it zero

               if(other.mHealthPoints < 0)

                   other.mHealthPoints = 0;

           }

          

           /**

            *

            * @param other

            */

           public void assist(GameCharacter other) {

               Random rng = new Random();

               int roll = rng.nextInt(5);

               switch(roll) {

                       case 0:

                           //other character receives health

                           //this character loses health

                           other.mHealthPoints += 5 * this.mLevel;

                           this.mHealthPoints -= 5 * this.mLevel;

                           break;

                       case 1:

                           other.mMagic += 5 * this.mLevel;

                           this.mMagic -= 5 * this.mLevel;

                           break;

                       case 2:

                           other.mGold += 5 * this.mLevel;

                           this.mGold -= 5 * this.mLevel;

                           break;

                       case 3:

                           other.mLevel++;

                           other.mGold += 100;

                           other.mHealthPoints += 100;

                           other.mMagic += 100;

                           this.mLevel--;

                           this.mGold -= 100;

                           this.mHealthPoints -= 100;

                           this.mMagic -= 100;

                       case 4:

                           other.mHealthPoints += 100 * mLevel;

                           this.mHealthPoints += 100 * mLevel;

                           break;

                   }

               }

          

           /**

            * method that generates a random integer, roll, between [0,2) denoting the player

            * and random integer, points, between [5,10] denoting the health points to be added

            * if roll = 0, this character rests and points are added to this character's health points

            * if roll = 1, other character rests and points are added to other character's health points

            * @param other

            */

           public void rest(GameCharacter other) {

            Random rng = new Random();

            int roll = rng.nextInt(2); // generates a random integer between [0,2)

            int points = rng.nextInt(6)+5; // generates points between [5,10]

           

            if(roll == 0) // if roll = 0, add points to this character's mHealthPoints i.e this character rests

            {

            mHealthPoints += points;

            }else // if roll = 1, add points to other character's mHealthPoints i.e other character rests

            other.mHealthPoints += points;

        }

}

Add a comment
Know the answer?
Add Answer to:
I'm trying to implement a rest() method for my GameCharacter where if a random number between...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey guys I am having trouble getting my table to work with a java GUI if...

    Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed...

    Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID                        as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE                      as in block-letter M DOUBLE                   as in block-letters U, M, A LEFT_DOT                as in block-letters F, S CENTER_DOT          as in block-letter...

  • Can someone tell me why this isnt compiling. The language is java Ive attached my 2...

    Can someone tell me why this isnt compiling. The language is java Ive attached my 2 files im getting my one and only error on line 24 of my monster file. the error says: error cannot find symbol return weapon.getName ^ File 1. Monster.java import java.util.Random; class Monster{ private String name; private int health; private Weapon weapon;    public Monster(String monsterName, int monsterHealth, Weapon monsterWeapon){ name = monsterName; health = monsterHealth; weapon = monsterWeapon;    }    public String getName(){...

  • PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test...

    PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test case for the below prompt using the provided java programs Use the setString() function in MyCustomStringInterface to set the value to “Peter Piper picked a peck of pickled peppers.”. Then test to see if reverseNCharacters() function returns the reversed string when the characters are reversed in groups of 4 and padding is disabled (in this case, “etePiP r repkcipa decep fo kcip delkpep srep.”)....

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • I need the following code to keep the current output but also include somthing similar to...

    I need the following code to keep the current output but also include somthing similar to this but with the correct details from the code. Truck Details: Skoda 100 Nathan Roy 150.5 3200 Details of Vehicle 1: Honda, 5 cd, owned by Peter England Details of Vehicle 2: Skoda, 100 cd, owned by Nathan Roy, 150.5 lbs load, 3200 tow --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- class Person {     private String name;        public Person()     {        name="None";     }       ...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT