Allan Didier

Game Character Overloaded

Goals

Students will demonstrate their understanding of method overloading by modifying their game character program by overloading methods.

Skills

  • Method overloading

Program Details

Modify your game character program to include some overloaded methods to enhance your game character class. Duplicate your Game Character class and Main Program files from the last assignment and then just modify the new ones. This way you have a copy of the old program and the new program.

  1. Game Character Class
    1. Five Private Data Variables
      1. String Name
      2. String Attribute (race, class, position, address, etc.)
      3. Integer Statistic (health, intelligence, age, grade, etc.)
      4. Static Final Integer MAX_STATISTIC.
        1. Set this to your maximum Statistic number when declaring the variable.
        2. This will have no setter method as it is unchangeable.
      5. Static Final Integer MIN_STATISTIC.
        1. Set this to your minimum Statistic number when declaring the variable.
        2. This will have no setter method as it is unchangeable.
    2. Methods
      1. Two Constructor methods
        1. One with three parameters (similar to prior)
          1. Passes in all three values as parameters.
          2. Sets the values for the three variables.
        2. One with no parameters (new)
          1. Asks the user to input the values for the three variables
          2. Sets the values according to the user input.
          3. Watch when scanning integers. You might need to put scan.next() after reading an integer to read the Enter keystroke.
      2. Five Accessor methods (getters)
        1. Must be declared Public.
        2. One for each of the above data variables, including the max and min variables.
        3. Pass in no parameters.
        4. Return the appropriate variable.
      3. Five Mutator methods (setters)
        1. Three regular Setters that set the variables of the character
          1. Must be declared public void.
          2. Passes in the new value as the parameter.
          3. Changes the class variable to the new variable.
          4. Outputs to the console that the variable has been changed from the original to the new value.
        2. Overloaded setStatistic method
          1. Instead of an integer being passed in, it passes in a string value.
          2. If the string is “max” (ignore case), the Statistic is set to MAX_STATISTIC.
          3. If the string in “min” (ignore case), the Statistic is set to MIN_STATISTIC.
          4. Outputs to the console that the variable has been changed from the original to the new value. You can but don’t have to report a message if your max/min static should cause another message, like your character has died because their health was set to zero.
          5. Report to the console if the neither max nor min is passed in (an error), but don’t change the Statistic’s value.
        3. Change Statistic method
          1. Must be declared Public void.
          2. Passes in an integer value to change the Statistic by.
          3. Increments or decrements the Statistic by the value.
          4. Reports to the console that the Statistic has been modified and by how much.
          5. Checks to see if the statistic is beyond a maximum and minimum threshold value and will report if it is. If the statistic goes beyond the max/min values, it is set to the max/min value and is reported as such. For example: if the character’s health drops to or below zero, the program should report that the character has died and set their health to zero, not a negative number.
      4. A toString method
        1. Returns a String value with all of the attributes of the character, including the max and min variables.
        2. This string will be used to print the character. Make the output easy to read and understand.
  2. Main Program Class:
    1. Object Instantiation and Initialization
      1. Create at least 2 objects (characters) from the above class.
      2. One object is initialized using the parameter-less constructor. The other is initialized by passing the parameters in directly.
    2. Object modification
      1. Re-set all three attributes of one object using the regular Setter methods.
      2. Re-set one character’s Statistic to the maximum using the overloaded setStatistic method.
      3. Re-set one character’s Statistic to the minimum using the overloaded setStatistic method.
      4. Call the Change Statistic method three times
        1. Increment or decrement one character’s Statistic, but not outside of the threshold
        2. Increment the one character’s Statistic to a value above of the maximum threshold limit. This should trigger an output from the Change Statistic method.
        3. Decrement the other character’s Statistic to a value below the minimum threshold limit. This should trigger an output from the Change Statistic method.
      5. Enter the data directly in your program. You don’t need to ask the user to input the data.
    3. Output: Output the following information to the console:
      1. The initial values of both characters. Use the toString method to do this.
      2. The changes that were made to each character. Should be done automatically from the setter methods.
      3. The final values of both characters Use the toString method to do this.
      4. Your Change Statistic from above should output when the character Statistic is modified too much. Your main program does not need to do this directly.

Resources

Java Textbook

Chapter 4: Writing Classes

Chapter 7: Object Oriented Design

CS Awesome Website

Bill Barnum's AP Computer Science A videos

AP College Board test prep videos