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. Character Data Text File
    1. Create a text file that your main program will read to initialize your character objects.
    2. The text file should have the Name, Attribute, and Statistic for each Game Character for your main program. 
    3. The file should have information for at least 3 different Game Characters.
    4. See the GameCharacterOverloadedData.txt file for an example of how to format the information. Basically, put the information for each character on one line with a space between each piece of information. 
    5. You can create the file directly in IntelliJ using the File-New-File command. Place the file in your SRC folder so that you can find it easily. Name it with the extension .txt since it is a text only file. 
    6. The max and min statistic variables are set in the class and do not need to be a part of the file. 
  3. Main Program Class:
    1. Scan the file with the Game Character information.
      1. See above for creation of the file.
      2. Import the java.io.* library to read from files. Add import java.io.* to the top of your code (along with import java.util.Scanner).
      3. If the data file is in the SRC folder, the file name needs to include src/ to the filename. Ex: “src/data.txt”. 
      4. Create a new File object that points to you character file.
        Ex: myFile = new File (“src/data.txt”);
      5. Set the scanner to scan the file.
        Ex: Scanner scan = new Scanner (myFile);
      6. Scan the information the same way you would scan from the console: next(), nextLine(), nextInt(), etc.
      7. Close the scanner when done at the end of the main program.
      8. Make sure the main program will throw an error if things go wrong rather than just crash.
        1.  If you write code incorrectly and it crashes your code during runtime, Java tries to tell you what happened by throwing error messages at you. ArithemeticException errors show up when you try to divide by zero. ArrayIndexOutOfBounds errors show up when try to access elements outside of array. We’ll talk more later about errors, but scanning of files doesn’t throw errors unless you tell Java to throw them. Your program will crash, but you won’t know why and Java won’t tell you why.
        2. By default, scanning from a file will just crash your program if Java can’t find the file (the file name is wrong or the file is not found in that folder).
        3. add the command throws IOException to the declaration of your main program.
          Ex: public static void main(String[] args) throws IOException
        4. We do a little input error checking in this class, but not much. Error checking is no longer a part of the AP exam, but it was a few years ago. Professionals do error checking all the time using try/catch statements to report to the user that they input something incorrectly. Try/catch statements are no longer a part of the AP exam, so we don’t cover them. They are useful to know and use, though.
    2. Object Instantiation and Initialization
      1. Create at least 4 objects (characters) from the above class.
      2. One object is initialized using the parameter-less constructor.
      3. The other three are initialized by passing the parameters in directly after scanning the file with the information. Put the file scanning and object creation in a loop to simplify your code. 
    3. 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. When running your mutator methods, enter the data directly in your main program. You don’t need to ask the user to input the data for the mutator methods. It takes a lot more code to get user input and it is not necessary for this part. 
    4. 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. This 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

Bill Barnum's AP Computer Science A videos

AP College Board test prep videos